1
Fork 0

Add proper cursor system, and add discoloration when a unit has moved

This commit is contained in:
Joshua Goins 2022-05-02 19:32:36 -04:00
parent 08978c32ec
commit 36ba603933
2 changed files with 268 additions and 209 deletions

View file

@ -12,4 +12,6 @@ public class Unit {
} }
public Team team; public Team team;
public int maxDistance = 5; public int maxDistance = 5;
public boolean hasTakenAction = false;
} }

View file

@ -149,21 +149,32 @@ public class GameScreen implements Screen {
void startNewTeamTurn(Unit.Team team) { void startNewTeamTurn(Unit.Team team) {
currentTurnTeam = team; currentTurnTeam = team;
currentUnitIndex = 0;
for(Unit unit : units) {
if(unit.team == team) {
unit.hasTakenAction = false;
}
}
currentUnitIndex = 0;
getNextUnit(); getNextUnit();
if(currentUnit != null) { if(currentUnit != null) {
smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0));
// reset cursor position
if(team == Unit.Team.Player) {
cursorX = currentUnit.positionX; cursorX = currentUnit.positionX;
cursorY = currentUnit.positionY; cursorY = currentUnit.positionY;
smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0)); currentUnit = null;
}
} }
} }
void updateUnits() { void updateUnits() {
units.removeIf(u -> u.health <= 0); units.removeIf(u -> u.health <= 0);
// then check if theres a win or lose condition // then check if there's a win or lose condition
long numberOfPlayers = units.stream().filter(u -> u.team == Unit.Team.Player).count(); long numberOfPlayers = units.stream().filter(u -> u.team == Unit.Team.Player).count();
long numberOfEnemies = units.stream().filter(u -> u.team == Unit.Team.Enemy).count(); long numberOfEnemies = units.stream().filter(u -> u.team == Unit.Team.Enemy).count();
@ -174,6 +185,13 @@ public class GameScreen implements Screen {
gameWin(); gameWin();
} }
boolean hasAllUnitsTakenAction() {
long numberOfPlayers = units.stream().filter(u -> u.team == currentTurnTeam).count();
long numberOfUnitsTired = units.stream().filter(u -> u.hasTakenAction).count();
return numberOfPlayers == numberOfUnitsTired;
}
void gameOver() { void gameOver() {
game.setScreen(new GameOverMenuScreen(game)); game.setScreen(new GameOverMenuScreen(game));
dispose(); dispose();
@ -188,20 +206,22 @@ public class GameScreen implements Screen {
void advanceTurn() { void advanceTurn() {
updateUnits(); updateUnits();
if(currentTurnTeam == Unit.Team.Player) {
if(hasAllUnitsTakenAction()) {
startNewTeamTurn(Unit.Team.Enemy);
}
} else {
currentUnitIndex++; currentUnitIndex++;
getNextUnit(); getNextUnit();
if(currentUnit == null) { if(currentUnit == null) {
if(currentTurnTeam == Unit.Team.Player) {
startNewTeamTurn(Unit.Team.Enemy);
} else if(currentTurnTeam == Unit.Team.Enemy) {
currentTurn++; currentTurn++;
startNewTeamTurn(Unit.Team.Player); startNewTeamTurn(Unit.Team.Player);
}
} else { } else {
smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0)); smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0));
} }
} }
}
void moveUnitTo(int x, int y) { void moveUnitTo(int x, int y) {
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision(), tiledMap); ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) x, (float) y), getCollision(), tiledMap);
@ -363,6 +383,12 @@ public class GameScreen implements Screen {
for(Unit unit : units) { for(Unit unit : units) {
TextureRegion currentFrame = playerIdleAnimation.getKeyFrame(stateTime, true); TextureRegion currentFrame = playerIdleAnimation.getKeyFrame(stateTime, true);
if(unit.hasTakenAction) {
game.batch.setColor(0.5f, 0.5f, 0.5f, 1.0f);
} else {
game.batch.setColor(1.0f, 1.0f, 1.0f, 1.0f);
}
game.batch.draw(currentFrame, unit.positionX * 16, unit.positionY * 16); game.batch.draw(currentFrame, unit.positionX * 16, unit.positionY * 16);
} }
@ -372,7 +398,18 @@ public class GameScreen implements Screen {
return; return;
} }
if(currentTurnTeam == Unit.Team.Player && currentUnit != null) { if(currentTurnTeam == Unit.Team.Player) {
if(currentUnit == null) {
if (Gdx.input.isKeyPressed(Input.Keys.ENTER) || Gdx.input.isButtonPressed(0)) {
for (Unit unit : units) {
if (unit.team == Unit.Team.Player && !unit.hasTakenAction) {
if (cursorX == unit.positionX && cursorY == unit.positionY) {
currentUnit = unit;
}
}
}
}
} else {
// player is trying to move // player is trying to move
if (Gdx.input.isButtonPressed(0) && !currentlyAwaitingAuxilaryAction) { if (Gdx.input.isButtonPressed(0) && !currentlyAwaitingAuxilaryAction) {
hasStartedMoving = true; hasStartedMoving = true;
@ -576,6 +613,7 @@ public class GameScreen implements Screen {
hasStartedMoving = false; hasStartedMoving = false;
} }
} }
}
if(currentTurnTeam == Unit.Team.Enemy && currentUnit != null && !currentlyTakingAction && !currentlyAwaitingAuxilaryAction) { if(currentTurnTeam == Unit.Team.Enemy && currentUnit != null && !currentlyTakingAction && !currentlyAwaitingAuxilaryAction) {
// first we want to choose the closest enemy unit. // first we want to choose the closest enemy unit.
@ -645,8 +683,10 @@ public class GameScreen implements Screen {
if(isAdjacent(closestUnit, currentUnit)) { if(isAdjacent(closestUnit, currentUnit)) {
AttackAction action = new AttackAction(currentUnit, closestUnit); AttackAction action = new AttackAction(currentUnit, closestUnit);
currentUnit.hasTakenAction = true;
executeAction(action); executeAction(action);
} else { } else {
currentUnit.hasTakenAction = true;
advanceTurn(); advanceTurn();
} }
} else { } else {
@ -666,13 +706,30 @@ public class GameScreen implements Screen {
if (Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) { if (Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) {
currentlyAwaitingAuxilaryAction = false; currentlyAwaitingAuxilaryAction = false;
currentUnit.hasTakenAction = true;
currentUnit = null;
executeAction(actions.get(i)); executeAction(actions.get(i));
} }
} }
} }
} }
if(currentTurnTeam == Unit.Team.Player && currentUnit == null) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(mousePos);
double newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y);
int newFixedX = (int) (newX / 16.0);
int newFixedY = (int) (newY / 16.0);
cursorX = newFixedX;
cursorY = newFixedY;
game.batch.draw(cursorTexture, cursorX * 16, cursorY * 16); game.batch.draw(cursorTexture, cursorX * 16, cursorY * 16);
}
game.batch.end(); game.batch.end();
} }