Add proper cursor system, and add discoloration when a unit has moved
This commit is contained in:
parent
08978c32ec
commit
36ba603933
2 changed files with 268 additions and 209 deletions
|
@ -12,4 +12,6 @@ public class Unit {
|
|||
}
|
||||
public Team team;
|
||||
public int maxDistance = 5;
|
||||
|
||||
public boolean hasTakenAction = false;
|
||||
}
|
||||
|
|
|
@ -149,21 +149,32 @@ public class GameScreen implements Screen {
|
|||
|
||||
void startNewTeamTurn(Unit.Team team) {
|
||||
currentTurnTeam = team;
|
||||
currentUnitIndex = 0;
|
||||
|
||||
for(Unit unit : units) {
|
||||
if(unit.team == team) {
|
||||
unit.hasTakenAction = false;
|
||||
}
|
||||
}
|
||||
|
||||
currentUnitIndex = 0;
|
||||
getNextUnit();
|
||||
|
||||
if(currentUnit != null) {
|
||||
smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0));
|
||||
|
||||
// reset cursor position
|
||||
if(team == Unit.Team.Player) {
|
||||
cursorX = currentUnit.positionX;
|
||||
cursorY = currentUnit.positionY;
|
||||
smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0));
|
||||
currentUnit = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateUnits() {
|
||||
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 numberOfEnemies = units.stream().filter(u -> u.team == Unit.Team.Enemy).count();
|
||||
|
||||
|
@ -174,6 +185,13 @@ public class GameScreen implements Screen {
|
|||
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() {
|
||||
game.setScreen(new GameOverMenuScreen(game));
|
||||
dispose();
|
||||
|
@ -188,20 +206,22 @@ public class GameScreen implements Screen {
|
|||
void advanceTurn() {
|
||||
updateUnits();
|
||||
|
||||
if(currentTurnTeam == Unit.Team.Player) {
|
||||
if(hasAllUnitsTakenAction()) {
|
||||
startNewTeamTurn(Unit.Team.Enemy);
|
||||
}
|
||||
} else {
|
||||
currentUnitIndex++;
|
||||
getNextUnit();
|
||||
|
||||
if(currentUnit == null) {
|
||||
if(currentTurnTeam == Unit.Team.Player) {
|
||||
startNewTeamTurn(Unit.Team.Enemy);
|
||||
} else if(currentTurnTeam == Unit.Team.Enemy) {
|
||||
currentTurn++;
|
||||
startNewTeamTurn(Unit.Team.Player);
|
||||
}
|
||||
} else {
|
||||
smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -363,6 +383,12 @@ public class GameScreen implements Screen {
|
|||
for(Unit unit : units) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -372,7 +398,18 @@ public class GameScreen implements Screen {
|
|||
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
|
||||
if (Gdx.input.isButtonPressed(0) && !currentlyAwaitingAuxilaryAction) {
|
||||
hasStartedMoving = true;
|
||||
|
@ -576,6 +613,7 @@ public class GameScreen implements Screen {
|
|||
hasStartedMoving = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(currentTurnTeam == Unit.Team.Enemy && currentUnit != null && !currentlyTakingAction && !currentlyAwaitingAuxilaryAction) {
|
||||
// first we want to choose the closest enemy unit.
|
||||
|
@ -645,8 +683,10 @@ public class GameScreen implements Screen {
|
|||
|
||||
if(isAdjacent(closestUnit, currentUnit)) {
|
||||
AttackAction action = new AttackAction(currentUnit, closestUnit);
|
||||
currentUnit.hasTakenAction = true;
|
||||
executeAction(action);
|
||||
} else {
|
||||
currentUnit.hasTakenAction = true;
|
||||
advanceTurn();
|
||||
}
|
||||
} else {
|
||||
|
@ -666,13 +706,30 @@ public class GameScreen implements Screen {
|
|||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.NUM_1 + i)) {
|
||||
currentlyAwaitingAuxilaryAction = false;
|
||||
currentUnit.hasTakenAction = true;
|
||||
currentUnit = null;
|
||||
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.end();
|
||||
}
|
||||
|
|
Reference in a new issue