关于上一篇V0.2游戏的一个bug
防止敌人坦克重叠运动
public boolean isTouchOtherEnemy() {
boolean b = false;
for (int i = 0; i < ets.size(); i++) {
EnemyTank tank = ets.get(i);
if (tank != this) {
if (this.x >= tank.x && this.x <= tank.x + 30 && this.y >= tank.y && this.y <= tank.y + 30) {
return true;
}
if (this.x + 30 >= tank.x && this.x + 30 <= tank.x + 30 && this.y >= tank.y && this.y <= tank.y + 30) {
return true;
}
if (this.x >= tank.x && this.x <= tank.x + 30 && this.y + 30 >= tank.y && this.y + 30 <= tank.y + 30) {
return true;
}
if (this.x + 30 >= tank.x && this.x + 30 <= tank.x + 30 && this.y + 30 >= tank.y
&& this.y + 30 <= tank.y + 30) {
return true;
}
}
}
return b;
}
分析了4种 坦克互相intersects情况,但是上述代码导致只要发生一次cover,就无法再次分开;
原因是:坦克cover后不管方向是什么都会停止运动,故 isTouchOtherEnemy要建立在坦克的方向才来判断是否重叠。
举例:如果坦克1和坦克二有碰撞,这是二者都需要等待,然后二者都会进行随机方向,假设他们的方向改变了,按照新的方向运动不会有重合,但是由于isTouchOtherEnemy函数任然得到Touch 为True的结果,所以相遇的坦克不会分开,即便是改变成合理的方向。
故:上面这种isTouchOtherEnemy函数错误,应该在某个方向上判断是否重合
public boolean isTouchOtherEnemy() {
boolean b = false;
switch (this.direction) {
case 0:// UP
for (int i = 0; i < ets.size(); i++) {
EnemyTank et = ets.get(i);
if (et != this) {
if (et.direction == 0 || et.direction == 1) {
if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(1);
return true;
}
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(2);
return true;
}
}
if (et.direction == 2 || et.direction == 3) {
if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(3);
return true;
}
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(4);
return true;
}
}
}
}
break;
case 1:// DOWN
for (int i = 0; i < ets.size(); i++) {
EnemyTank et = ets.get(i);
if (et != this) {
if (et.direction == 0 || et.direction == 1) {
if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
System.out.println(5);
return true;
}
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
&& this.y + 30 <= et.y + 30) {
System.out.println(6);
return true;
}
}
if (et.direction == 2 || et.direction == 3) {
if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
System.out.println(7);
return true;
}
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
&& this.y + 30 <= et.y + 30) {
System.out.println(8);
return true;
}
}
}
}
break;
case 2:// LEFT
for (int i = 0; i < ets.size(); i++) {
EnemyTank et = ets.get(i);
if (et != this) {
if (et.direction == 0 || et.direction == 1) {
if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(9);
return true;
}
if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
System.out.println(10);
return true;
}
}
if (et.direction == 2 || et.direction == 3) {
if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(11);
return true;
}
if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {
System.out.println(12);
return true;
}
}
}
}
break;
case 3:// RIGHT
for (int i = 0; i < ets.size(); i++) {
EnemyTank et = ets.get(i);
if (et != this) {
if (et.direction == 0 || et.direction == 1) {
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(13);
return true;
}
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
&& this.y + 30 <= et.y + 30) {
System.out.println(14);
return true;
}
}
if (et.direction == 2 || et.direction == 3) {
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {
System.out.println(15);
return true;
}
if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y
&& this.y + 30 <= et.y + 30) {
System.out.println(16);
return true;
}
}
}
}
break;
}
return b;
}