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; boolean shouldIgnore = false;
for(Vector2 collisionPosition : collisionPoints) { for(Vector2 collisionPosition : collisionPoints) {
if(collisionPosition.epsilonEquals(newNode.position)) { if(collisionPosition.epsilonEquals(newNode.position) && !collisionPosition.epsilonEquals(end)) {
shouldIgnore = true; shouldIgnore = true;
} }
} }

View file

@ -93,8 +93,9 @@ public class GameScreen implements Screen {
currentUnit = null; currentUnit = null;
for(int i = currentUnitIndex; i < units.size(); i++) { for(int i = currentUnitIndex; i < units.size(); i++) {
if(units.get(currentUnitIndex).team == currentTurnTeam) { if(units.get(i).team == currentTurnTeam) {
currentUnit = units.get(currentUnitIndex); currentUnit = units.get(i);
return;
} }
} }
} }
@ -301,8 +302,31 @@ public class GameScreen implements Screen {
} }
} }
if(currentTurnTeam == Unit.Team.Enemy) { if(currentTurnTeam == Unit.Team.Enemy && currentUnit != null && !currentlyTakingAction && !currentlyAwaitingAuxilaryAction) {
advanceTurn(); // 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) { if(currentlyTakingAction) {
@ -317,23 +341,28 @@ public class GameScreen implements Screen {
} }
if(currentlyAwaitingAuxilaryAction) { if(currentlyAwaitingAuxilaryAction) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); if (currentUnit.team == Unit.Team.Enemy) {
currentlyAwaitingAuxilaryAction = false;
advanceTurn();
} else {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(mousePos); camera.unproject(mousePos);
double newX = Math.floor(mousePos.x); double newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y); double newY = Math.floor(mousePos.y);
int newFixedX = (int) (newX / 32.0); int newFixedX = (int) (newX / 32.0);
int newFixedY = (int) (newY / 32.0); int newFixedY = (int) (newY / 32.0);
List<UnitAction> actions = getAvailableActionsForPosition(newFixedX, newFixedY); List<UnitAction> actions = getAvailableActionsForPosition(newFixedX, newFixedY);
for(int i = 0; i < actions.size(); i++) { for (int i = 0; i < actions.size(); i++) {
game.font.draw(game.batch, (i + 1) + ": " + actions.get(i).getName(), (float) newX, (float)newY + (i * 32)); game.font.draw(game.batch, (i + 1) + ": " + actions.get(i).getName(), (float) newX, (float) newY + (i * 32));
if(Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) { if (Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) {
currentlyAwaitingAuxilaryAction = false; currentlyAwaitingAuxilaryAction = false;
executeAction(actions.get(i)); executeAction(actions.get(i));
}
} }
} }
} }