1
Fork 0

Add collision points to the pathing algorithm

Currently, this is only other units.
This commit is contained in:
Joshua Goins 2022-05-01 17:30:08 -04:00
parent 5eb5e45f3f
commit 628a06d097
2 changed files with 34 additions and 10 deletions

View file

@ -20,7 +20,7 @@ public class AStar {
private static final int maxOpenNodes = 200;
public static ArrayList<Vector2> path(Vector2 start, Vector2 end) {
public static ArrayList<Vector2> path(Vector2 start, Vector2 end, List<Vector2> 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) {

View file

@ -112,11 +112,26 @@ public class GameScreen implements Screen {
}
void moveUnitTo(int x, int y) {
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y));
Collections.reverse(path);
ArrayList<Vector2> 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<Vector2> getCollision() {
ArrayList<Vector2> 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<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY));
Collections.reverse(path);
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision());
if (path != null) {
Collections.reverse(path);
List<Vector2> 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));