Improve isAdjacent function
This also fixes a bug where an enemy unit might get selected multiple times. The AI now checks if it's already adjacent to the closest unit, to prevent it from unnecessarily moving.
This commit is contained in:
parent
17913b7adb
commit
beeea8f92a
1 changed files with 31 additions and 11 deletions
|
@ -95,6 +95,7 @@ public class GameScreen implements Screen {
|
||||||
for(int i = currentUnitIndex; i < units.size(); i++) {
|
for(int i = currentUnitIndex; i < units.size(); i++) {
|
||||||
if(units.get(i).team == currentTurnTeam) {
|
if(units.get(i).team == currentTurnTeam) {
|
||||||
currentUnit = units.get(i);
|
currentUnit = units.get(i);
|
||||||
|
currentUnitIndex = i;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,7 +162,22 @@ public class GameScreen implements Screen {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isAdjacent(Unit a, Unit b) {
|
boolean isAdjacent(Unit a, Unit b) {
|
||||||
return Vector2.dst(a.positionX, a.positionY, b.positionX, b.positionY) <= 1.0;
|
ArrayList<Vector2> adjacentPositions = new ArrayList<>();
|
||||||
|
adjacentPositions.add(new Vector2(0, -1));
|
||||||
|
adjacentPositions.add(new Vector2(0, 1));
|
||||||
|
adjacentPositions.add(new Vector2(-1, 0));
|
||||||
|
adjacentPositions.add(new Vector2(1, 0));
|
||||||
|
adjacentPositions.add(new Vector2(-1, -1));
|
||||||
|
adjacentPositions.add(new Vector2(-1, 1));
|
||||||
|
adjacentPositions.add(new Vector2(1, -1));
|
||||||
|
adjacentPositions.add(new Vector2(1, 1));
|
||||||
|
for (Vector2 position : adjacentPositions) {
|
||||||
|
Vector2 newPosition = new Vector2(a.positionX + position.x, a.positionY + position.y);
|
||||||
|
if(b.positionX == newPosition.x && b.positionY == newPosition.y)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<UnitAction> getAvailableActionsForPosition(int x, int y) {
|
List<UnitAction> getAvailableActionsForPosition(int x, int y) {
|
||||||
|
@ -316,6 +332,10 @@ public class GameScreen implements Screen {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we're already next to them, why move?
|
||||||
|
if(isAdjacent(currentUnit, closestUnit)) {
|
||||||
|
currentlyAwaitingAuxilaryAction = true;
|
||||||
|
} else {
|
||||||
// now that we have the closest unit, let's path to it.
|
// now that we have the closest unit, let's path to it.
|
||||||
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision());
|
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision());
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
|
@ -328,6 +348,7 @@ public class GameScreen implements Screen {
|
||||||
moveUnitTo((int) determinedPos.x, (int) determinedPos.y);
|
moveUnitTo((int) determinedPos.x, (int) determinedPos.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(currentlyTakingAction) {
|
if(currentlyTakingAction) {
|
||||||
if(!currentMovementList.isEmpty()) {
|
if(!currentMovementList.isEmpty()) {
|
||||||
|
@ -362,7 +383,6 @@ public class GameScreen implements Screen {
|
||||||
} else {
|
} else {
|
||||||
advanceTurn();
|
advanceTurn();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||||
|
|
||||||
|
|
Reference in a new issue