1
Fork 0

Let the enemy units move to the closest player units automatically

This commit is contained in:
Joshua Goins 2022-05-01 19:19:15 -04:00
parent 9dfce4e601
commit c8dcd1e08c
2 changed files with 46 additions and 17 deletions

View file

@ -74,7 +74,7 @@ public class AStar {
boolean shouldIgnore = false;
for(Vector2 collisionPosition : collisionPoints) {
if(collisionPosition.epsilonEquals(newNode.position)) {
if(collisionPosition.epsilonEquals(newNode.position) && !collisionPosition.epsilonEquals(end)) {
shouldIgnore = true;
}
}

View file

@ -93,8 +93,9 @@ public class GameScreen implements Screen {
currentUnit = null;
for(int i = currentUnitIndex; i < units.size(); i++) {
if(units.get(currentUnitIndex).team == currentTurnTeam) {
currentUnit = units.get(currentUnitIndex);
if(units.get(i).team == currentTurnTeam) {
currentUnit = units.get(i);
return;
}
}
}
@ -301,8 +302,31 @@ public class GameScreen implements Screen {
}
}
if(currentTurnTeam == Unit.Team.Enemy) {
advanceTurn();
if(currentTurnTeam == Unit.Team.Enemy && currentUnit != null && !currentlyTakingAction && !currentlyAwaitingAuxilaryAction) {
// first we want to choose the closest enemy unit.
Unit closestUnit = null;
float closestDistance = 999;
for(Unit unit : units) {
if(unit.team == Unit.Team.Player) {
float dist = Vector2.dst(unit.positionX, unit.positionY, currentUnit.positionX, currentUnit.positionY);
if(dist < closestDistance) {
closestUnit = unit;
closestDistance = dist;
}
}
}
// 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);
}
}
if(currentlyTakingAction) {
@ -317,6 +341,10 @@ public class GameScreen implements Screen {
}
if(currentlyAwaitingAuxilaryAction) {
if (currentUnit.team == Unit.Team.Enemy) {
currentlyAwaitingAuxilaryAction = false;
advanceTurn();
} else {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(mousePos);
@ -337,6 +365,7 @@ public class GameScreen implements Screen {
}
}
}
}
game.batch.end();
}