I'm creating a skill tree calculator for a game. I wrote a method that checks whether or not it can subtract a point from the skill and then proceeds to do so. Everything is working as it should, however it's very slow when this method is executed in rapid succession (i.e. when 5 points are subtract at the same time).
我正在为游戏创建一个技能树计算器。我写了一个方法,检查它是否可以从技能中减去一个点然后继续这样做。一切都按预期工作,但是当这种方法快速连续执行时(即同时减去5个点),速度非常慢。
Can anyone help me with this and more important explain why certain things are faster and what to avoid?
任何人都可以帮助我,更重要的是解释为什么某些事情会更快以及应该避免什么?
Here is the method:
这是方法:
public void cmdSubtractPoint(int index, SkillTree tree) {
boolean subtract = false;
if (this.blueTree.get(index).getInvestedPoints() != 0) {
//Only subtract a point if the skill isn't empty
if (index == 0 || index == 1) {
if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) - 1) < 5)
&& ((this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() +
this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() + this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() - 1) < 10)
&& ((this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() + this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() + this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() - 1) < 15)
&& ((this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else {
subtract = true;
}
} else if (index == 2 || index == 3) {
if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() - 1) < 10)
&& ((this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() + this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() + this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() - 1) < 15)
&& ((this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else {
subtract = true;
}
} else if (index == 4 || index == 5) {
if ((((this.blueTree.get(0).getInvestedPoints() + this.blueTree.get(1).getInvestedPoints()) + this.blueTree.get(2).getInvestedPoints() + this.blueTree.get(3).getInvestedPoints() + this.blueTree.get(4).getInvestedPoints() + this.blueTree.get(5).getInvestedPoints() - 1) < 15)
&& ((this.blueTree.get(6).getInvestedPoints()) != 0)) {
this.showToast();
} else {
subtract = true;
}
} else if (index == 6) {
subtract = true;
}
if (subtract) {
this.pointsInBlueTree -= 1;
this.blueTree.get(index).subtractPoint();
}
}
}
1 个解决方案
#1
0
If reduce value calculation to one time and use switch, you will obtain code similar this:
如果将值计算减少到一次并使用开关,您将获得类似的代码:
public void cmdSubtractPoint(int index, SkillTree tree) {
boolean subtract = false;
if (this.blueTree.get(index).getInvestedPoints() != 0) {
// Only subtract a point if the skill isn't empty
int temp01 = this.blueTree.get(0).getInvestedPoints() + tree.blueTree.get(1).getInvestedPoints();
int temp23 = this.blueTree.get(2).getInvestedPoints() + tree.blueTree.get(3).getInvestedPoints();
int temp45 = this.blueTree.get(4).getInvestedPoints() + tree.blueTree.get(5).getInvestedPoints();
int temp6 = this.blueTree.get(6).getInvestedPoints();
switch (index) {
case 0:
case 1:
if (((temp01 ) < 6) && ((temp23 + temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23) < 11) && ((temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 2:
case 3:
if (((temp01 + temp23) < 11) && ((temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 4:
case 5:
if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 6:
subtract = true;
break;
}
if (subtract) {
this.pointsInBlueTree -= 1;
this.blueTree.get(index).subtractPoint();
}
}
}
#1
0
If reduce value calculation to one time and use switch, you will obtain code similar this:
如果将值计算减少到一次并使用开关,您将获得类似的代码:
public void cmdSubtractPoint(int index, SkillTree tree) {
boolean subtract = false;
if (this.blueTree.get(index).getInvestedPoints() != 0) {
// Only subtract a point if the skill isn't empty
int temp01 = this.blueTree.get(0).getInvestedPoints() + tree.blueTree.get(1).getInvestedPoints();
int temp23 = this.blueTree.get(2).getInvestedPoints() + tree.blueTree.get(3).getInvestedPoints();
int temp45 = this.blueTree.get(4).getInvestedPoints() + tree.blueTree.get(5).getInvestedPoints();
int temp6 = this.blueTree.get(6).getInvestedPoints();
switch (index) {
case 0:
case 1:
if (((temp01 ) < 6) && ((temp23 + temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23) < 11) && ((temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 2:
case 3:
if (((temp01 + temp23) < 11) && ((temp45 + temp6) != 0)) {
this.showToast();
} else if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 4:
case 5:
if (((temp01 + temp23 + temp45) < 16) && (temp6 != 0)) {
this.showToast();
} else {
subtract = true;
}
break;
case 6:
subtract = true;
break;
}
if (subtract) {
this.pointsInBlueTree -= 1;
this.blueTree.get(index).subtractPoint();
}
}
}