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++) {
|
||||
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,16 +332,21 @@ public class GameScreen implements Screen {
|
|||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
Collections.reverse(path);
|
||||
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
||||
// 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) {
|
||||
Collections.reverse(path);
|
||||
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
||||
|
||||
// we want to grab the second to last position, since that's the "max distance" we can go, and it makes no sense
|
||||
// for units to go shorter
|
||||
Vector2 determinedPos = realPath.get(realPath.size() - 2);
|
||||
moveUnitTo((int) determinedPos.x, (int) determinedPos.y);
|
||||
// we want to grab the second to last position, since that's the "max distance" we can go, and it makes no sense
|
||||
// for units to go shorter
|
||||
Vector2 determinedPos = realPath.get(realPath.size() - 2);
|
||||
moveUnitTo((int) determinedPos.x, (int) determinedPos.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -362,7 +383,6 @@ public class GameScreen implements Screen {
|
|||
} else {
|
||||
advanceTurn();
|
||||
}
|
||||
|
||||
} else {
|
||||
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||
|
||||
|
|
Reference in a new issue