From c8dcd1e08c06cc5ee99fc7e59db324a79eebf886 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 1 May 2022 19:19:15 -0400 Subject: [PATCH] Let the enemy units move to the closest player units automatically --- core/src/com/redstrate/watersymbol/AStar.java | 2 +- .../watersymbol/screens/GameScreen.java | 61 ++++++++++++++----- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/core/src/com/redstrate/watersymbol/AStar.java b/core/src/com/redstrate/watersymbol/AStar.java index 2c4760d..d6412f4 100644 --- a/core/src/com/redstrate/watersymbol/AStar.java +++ b/core/src/com/redstrate/watersymbol/AStar.java @@ -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; } } diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index b1bafeb..6b48f9c 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -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 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 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,23 +341,28 @@ public class GameScreen implements Screen { } 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 newY = Math.floor(mousePos.y); + double newX = Math.floor(mousePos.x); + double newY = Math.floor(mousePos.y); - int newFixedX = (int) (newX / 32.0); - int newFixedY = (int) (newY / 32.0); + int newFixedX = (int) (newX / 32.0); + int newFixedY = (int) (newY / 32.0); - List actions = getAvailableActionsForPosition(newFixedX, newFixedY); - 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)); + List actions = getAvailableActionsForPosition(newFixedX, newFixedY); + 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)); - if(Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) { - currentlyAwaitingAuxilaryAction = false; - executeAction(actions.get(i)); + if (Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) { + currentlyAwaitingAuxilaryAction = false; + executeAction(actions.get(i)); + } } } }