diff --git a/core/src/com/redstrate/watersymbol/AStar.java b/core/src/com/redstrate/watersymbol/AStar.java index 11847f6..2c4760d 100644 --- a/core/src/com/redstrate/watersymbol/AStar.java +++ b/core/src/com/redstrate/watersymbol/AStar.java @@ -20,7 +20,7 @@ public class AStar { private static final int maxOpenNodes = 200; - public static ArrayList path(Vector2 start, Vector2 end) { + public static ArrayList path(Vector2 start, Vector2 end, List collisionPoints) { Node startNode = new Node(); startNode.position = start; @@ -72,7 +72,16 @@ public class AStar { newNode.position = new Vector2(currentNode.position.x + position.x, currentNode.position.y + position.y); newNode.parent = currentNode; - children.add(newNode); + boolean shouldIgnore = false; + for(Vector2 collisionPosition : collisionPoints) { + if(collisionPosition.epsilonEquals(newNode.position)) { + shouldIgnore = true; + } + } + + if(!shouldIgnore) { + children.add(newNode); + } } for(Node child : children) { diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index 90399f5..c0eaf64 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -112,11 +112,26 @@ public class GameScreen implements Screen { } void moveUnitTo(int x, int y) { - ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y)); - Collections.reverse(path); + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision()); - currentMovementList = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); - currentlyTakingAction = true; + // don't allow invalid movements + if(path != null) { + Collections.reverse(path); + + currentMovementList = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); + currentlyTakingAction = true; + } + } + + List getCollision() { + ArrayList collisions = new ArrayList<>(); + for(Unit unit : units) { + if(unit != currentUnit) { + collisions.add(new Vector2(unit.positionX, unit.positionY)); + } + } + + return collisions; } @Override @@ -153,11 +168,11 @@ public class GameScreen implements Screen { int newFixedX = (int) (newX / 32.0); int newFixedY = (int) (newY / 32.0); - ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY)); - Collections.reverse(path); - List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision()); + if (path != null) { + Collections.reverse(path); + List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); - if (realPath != null) { // we start at index 1 since there's no point in drawing an arrow over the player for (int i = 1; i < realPath.size(); i++) { Vector2 previousPosition = realPath.get(Math.max(i - 1, 0));