1
Fork 0

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:
Joshua Goins 2022-05-01 19:28:24 -04:00
parent 17913b7adb
commit beeea8f92a

View file

@ -95,6 +95,7 @@ public class GameScreen implements Screen {
for(int i = currentUnitIndex; i < units.size(); i++) {
if(units.get(i).team == currentTurnTeam) {
currentUnit = units.get(i);
currentUnitIndex = i;
return;
}
}
@ -161,7 +162,22 @@ public class GameScreen implements Screen {
}
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) {
@ -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.
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) closestUnit.positionX, (float) closestUnit.positionY), getCollision());
if (path != null) {
@ -328,6 +348,7 @@ public class GameScreen implements Screen {
moveUnitTo((int) determinedPos.x, (int) determinedPos.y);
}
}
}
if(currentlyTakingAction) {
if(!currentMovementList.isEmpty()) {
@ -362,7 +383,6 @@ public class GameScreen implements Screen {
} else {
advanceTurn();
}
} else {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);