1
Fork 0

Add basic walking animation and max distance calculation

There's actual no sprite animation (yet)
This commit is contained in:
Joshua Goins 2022-05-01 17:25:55 -04:00
parent 41d1fa55ef
commit 5eb5e45f3f
2 changed files with 32 additions and 8 deletions

View file

@ -11,4 +11,6 @@ public class Unit {
} }
public Team team; public Team team;
public int maxDistance = 5;
} }

View file

@ -20,6 +20,7 @@ import com.redstrate.watersymbol.WaterSymbol;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
public class GameScreen implements Screen { public class GameScreen implements Screen {
private final WaterSymbol game; private final WaterSymbol game;
@ -39,6 +40,10 @@ public class GameScreen implements Screen {
TextureAtlas arrowAtlas; TextureAtlas arrowAtlas;
// data for in-turn actions
boolean currentlyTakingAction = false;
List<Vector2> currentMovementList;
GameScreen(WaterSymbol game) { GameScreen(WaterSymbol game) {
this.game = game; this.game = game;
@ -106,6 +111,14 @@ public class GameScreen implements Screen {
} }
} }
void moveUnitTo(int x, int y) {
ArrayList<Vector2> 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 @Override
public void render(float delta) { public void render(float delta) {
ScreenUtils.clear(0, 0, 0.2f, 1); ScreenUtils.clear(0, 0, 0.2f, 1);
@ -142,12 +155,13 @@ public class GameScreen implements Screen {
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));
Collections.reverse(path); Collections.reverse(path);
List<Vector2> 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 // 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++) { for (int i = 1; i < realPath.size(); i++) {
Vector2 previousPosition = path.get(Math.max(i - 1, 0)); Vector2 previousPosition = realPath.get(Math.max(i - 1, 0));
Vector2 newPosition = path.get(i); Vector2 newPosition = realPath.get(i);
Vector2 d = new Vector2(newPosition.x - previousPosition.x, newPosition.y - previousPosition.y); 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"); spriteRegion = arrowAtlas.findRegion("cross-down");
} }
if (spriteRegion != null) { if (spriteRegion != null) {
game.batch.draw(spriteRegion, newPosition.x * 32, newPosition.y * 32); 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 newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y); double newY = Math.floor(mousePos.y);
currentUnit.positionX = (int) (newX / 32.0); moveUnitTo((int) (newX / 32.0), (int) (newY / 32.0));
currentUnit.positionY = (int) (newY / 32.0);
hasStartedMoving = false; hasStartedMoving = false;
advanceTurn();
} }
} }
@ -212,6 +223,17 @@ public class GameScreen implements Screen {
advanceTurn(); 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(); game.batch.end();
} }