Add collision points to the pathing algorithm
Currently, this is only other units.
This commit is contained in:
parent
5eb5e45f3f
commit
628a06d097
2 changed files with 34 additions and 10 deletions
|
@ -20,7 +20,7 @@ public class AStar {
|
||||||
|
|
||||||
private static final int maxOpenNodes = 200;
|
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();
|
Node startNode = new Node();
|
||||||
startNode.position = start;
|
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.position = new Vector2(currentNode.position.x + position.x, currentNode.position.y + position.y);
|
||||||
newNode.parent = currentNode;
|
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) {
|
for(Node child : children) {
|
||||||
|
|
|
@ -112,11 +112,26 @@ public class GameScreen implements Screen {
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveUnitTo(int x, int y) {
|
void moveUnitTo(int x, int y) {
|
||||||
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y));
|
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision());
|
||||||
Collections.reverse(path);
|
|
||||||
|
|
||||||
currentMovementList = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
// don't allow invalid movements
|
||||||
currentlyTakingAction = true;
|
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
|
@Override
|
||||||
|
@ -153,11 +168,11 @@ public class GameScreen implements Screen {
|
||||||
int newFixedX = (int) (newX / 32.0);
|
int newFixedX = (int) (newX / 32.0);
|
||||||
int newFixedY = (int) (newY / 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));
|
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision());
|
||||||
Collections.reverse(path);
|
if (path != null) {
|
||||||
List<Vector2> realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size()));
|
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
|
// 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++) {
|
for (int i = 1; i < realPath.size(); i++) {
|
||||||
Vector2 previousPosition = realPath.get(Math.max(i - 1, 0));
|
Vector2 previousPosition = realPath.get(Math.max(i - 1, 0));
|
||||||
|
|
Reference in a new issue