From beeea8f92a2a332163dd59cd294f8e69a95fffbe Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 1 May 2022 19:28:24 -0400 Subject: [PATCH] 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. --- .../watersymbol/screens/GameScreen.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index 4297cc1..b8a2989 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -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 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 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 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 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 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 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);