diff --git a/core/src/com/redstrate/watersymbol/Unit.java b/core/src/com/redstrate/watersymbol/Unit.java index 2a15baf..70b8e8f 100644 --- a/core/src/com/redstrate/watersymbol/Unit.java +++ b/core/src/com/redstrate/watersymbol/Unit.java @@ -11,4 +11,6 @@ public class Unit { } public Team team; + + public int maxDistance = 5; } diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index 6029fa5..90399f5 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -20,6 +20,7 @@ import com.redstrate.watersymbol.WaterSymbol; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class GameScreen implements Screen { private final WaterSymbol game; @@ -39,6 +40,10 @@ public class GameScreen implements Screen { TextureAtlas arrowAtlas; + // data for in-turn actions + boolean currentlyTakingAction = false; + List currentMovementList; + GameScreen(WaterSymbol game) { this.game = game; @@ -106,6 +111,14 @@ public class GameScreen implements Screen { } } + void moveUnitTo(int x, int y) { + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y)); + Collections.reverse(path); + + currentMovementList = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); + currentlyTakingAction = true; + } + @Override public void render(float delta) { ScreenUtils.clear(0, 0, 0.2f, 1); @@ -142,12 +155,13 @@ public class GameScreen implements Screen { ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY)); Collections.reverse(path); + List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); - if (path != null) { + 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 < path.size(); i++) { - Vector2 previousPosition = path.get(Math.max(i - 1, 0)); - Vector2 newPosition = path.get(i); + for (int i = 1; i < realPath.size(); i++) { + Vector2 previousPosition = realPath.get(Math.max(i - 1, 0)); + Vector2 newPosition = realPath.get(i); Vector2 d = new Vector2(newPosition.x - previousPosition.x, newPosition.y - previousPosition.y); @@ -186,7 +200,6 @@ public class GameScreen implements Screen { spriteRegion = arrowAtlas.findRegion("cross-down"); } - if (spriteRegion != null) { game.batch.draw(spriteRegion, newPosition.x * 32, newPosition.y * 32); } @@ -200,11 +213,9 @@ public class GameScreen implements Screen { double newX = Math.floor(mousePos.x); double newY = Math.floor(mousePos.y); - currentUnit.positionX = (int) (newX / 32.0); - currentUnit.positionY = (int) (newY / 32.0); + moveUnitTo((int) (newX / 32.0), (int) (newY / 32.0)); hasStartedMoving = false; - advanceTurn(); } } @@ -212,6 +223,17 @@ public class GameScreen implements Screen { advanceTurn(); } + if(currentlyTakingAction) { + if(!currentMovementList.isEmpty()) { + Vector2 nextMove = currentMovementList.remove(0); + currentUnit.positionX = (int) nextMove.x; + currentUnit.positionY = (int) nextMove.y; + } else { + currentlyTakingAction = false; + advanceTurn(); + } + } + game.batch.end(); }