From 36ba603933aa0c92c6be8290971a1d249db30863 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 2 May 2022 19:32:36 -0400 Subject: [PATCH] Add proper cursor system, and add discoloration when a unit has moved --- core/src/com/redstrate/watersymbol/Unit.java | 2 + .../watersymbol/screens/GameScreen.java | 475 ++++++++++-------- 2 files changed, 268 insertions(+), 209 deletions(-) diff --git a/core/src/com/redstrate/watersymbol/Unit.java b/core/src/com/redstrate/watersymbol/Unit.java index 4bca9ad..92d82cb 100644 --- a/core/src/com/redstrate/watersymbol/Unit.java +++ b/core/src/com/redstrate/watersymbol/Unit.java @@ -12,4 +12,6 @@ public class Unit { } public Team team; public int maxDistance = 5; + + public boolean hasTakenAction = false; } diff --git a/core/src/com/redstrate/watersymbol/screens/GameScreen.java b/core/src/com/redstrate/watersymbol/screens/GameScreen.java index d1baf22..bf498f2 100644 --- a/core/src/com/redstrate/watersymbol/screens/GameScreen.java +++ b/core/src/com/redstrate/watersymbol/screens/GameScreen.java @@ -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) { - cursorX = currentUnit.positionX; - cursorY = currentUnit.positionY; smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0)); + + // reset cursor position + if(team == Unit.Team.Player) { + cursorX = currentUnit.positionX; + cursorY = currentUnit.positionY; + 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,18 +206,20 @@ public class GameScreen implements Screen { void advanceTurn() { updateUnits(); - currentUnitIndex++; - getNextUnit(); - - if(currentUnit == null) { - if(currentTurnTeam == Unit.Team.Player) { + if(currentTurnTeam == Unit.Team.Player) { + if(hasAllUnitsTakenAction()) { 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)); + currentUnitIndex++; + getNextUnit(); + + if(currentUnit == null) { + currentTurn++; + startNewTeamTurn(Unit.Team.Player); + } else { + smoothlyTransitionCamera(new Vector3(currentUnit.positionX * 16, currentUnit.positionY * 16, 0)); + } } } @@ -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,208 +398,220 @@ public class GameScreen implements Screen { return; } - if(currentTurnTeam == Unit.Team.Player && currentUnit != null) { - // player is trying to move - if (Gdx.input.isButtonPressed(0) && !currentlyAwaitingAuxilaryAction) { - hasStartedMoving = true; - - 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); - - ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision(), tiledMap); - if (path != null && path.size() > 1) { - Collections.reverse(path); - List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); - - // we start at index 1 since there's no point in drawing an arrow over the player - for (int i = 1; i < realPath.size() - 1; i++) { - Vector2 previousPosition = realPath.get(i - 1); - Vector2 newPosition = realPath.get(i); - Vector2 nextPosition = realPath.get(i + 1); - - // we want to grab two directions, first is our "entrance" and then our "exit". of course arrows that are just a position don't exist - Direction entrance = getFromDelta(new Vector2(newPosition.x - previousPosition.x, newPosition.y - previousPosition.y)); - Direction exit = getFromDelta(new Vector2(newPosition.x - nextPosition.x, newPosition.y - nextPosition.y)); - - TextureRegion spriteRegion = null; - TextureRegion leftSprite = null; - TextureRegion rightSprite = null; - - // TODO: condense duplicate branches - - // handle straights - if(entrance == Direction.Bottom && exit == Direction.Top) { - spriteRegion = arrowSpritesheet.getUpDown(); + 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; + } } - - if(entrance == Direction.Top && exit == Direction.Bottom) { - spriteRegion = arrowSpritesheet.getUpDown(); - } - - if(entrance == Direction.Left && exit == Direction.Right) { - spriteRegion = arrowSpritesheet.getLeftRight(); - } - - if(entrance == Direction.Right && exit == Direction.Left) { - spriteRegion = arrowSpritesheet.getLeftRight(); - } - - // handle crosses - if(entrance == Direction.TopLeft && exit == Direction.BottomRight) { - spriteRegion = arrowSpritesheet.getDiagDown(); - } - - if(entrance == Direction.TopLeft && exit == Direction.TopLeft) { - spriteRegion = arrowSpritesheet.getDiagDown(); - } - - if(entrance == Direction.BottomLeft && exit == Direction.BottomLeft) { - spriteRegion = arrowSpritesheet.getDiagUp(); - } - - if(entrance == Direction.BottomLeft && exit == Direction.TopRight) { - spriteRegion = arrowSpritesheet.getDiagUp(); - } - - if(entrance == Direction.BottomRight && exit == Direction.TopLeft) { - spriteRegion = arrowSpritesheet.getDiagDown(); - } - - if(entrance == Direction.TopRight && exit == Direction.BottomLeft) { - spriteRegion = arrowSpritesheet.getDiagDown(); - } - - if(spriteRegion == arrowSpritesheet.getDiagDown()) { - leftSprite = arrowSpritesheet.getCornerTopRight(); - rightSprite = arrowSpritesheet.getCornerBottomLeft(); - } - - if(spriteRegion == arrowSpritesheet.getDiagUp()) { - leftSprite = arrowSpritesheet.getCornerBottomRight(); - rightSprite = arrowSpritesheet.getCornerTopLeft(); - } - - // handle bends - if(entrance == Direction.BottomRight && exit == Direction.Top) { - spriteRegion = arrowSpritesheet.getBendBottomRightToTop(); - } - - if(entrance == Direction.Top && exit == Direction.BottomRight) { - spriteRegion = arrowSpritesheet.getBendBottomRightToTop(); - } - - if(entrance == Direction.BottomLeft && exit == Direction.Top) { - spriteRegion = arrowSpritesheet.getBendBottomLeftToTop(); - } - - if(entrance == Direction.Top && exit == Direction.BottomLeft) { - spriteRegion = arrowSpritesheet.getBendBottomLeftToTop(); - } - - if(entrance == Direction.TopLeft && exit == Direction.Bottom) { - spriteRegion = arrowSpritesheet.getBendTopLeftToBottom(); - } - - if(entrance == Direction.Bottom && exit == Direction.TopLeft) { - spriteRegion = arrowSpritesheet.getBendTopLeftToBottom(); - } - - if(entrance == Direction.TopRight && exit == Direction.Bottom) { - spriteRegion = arrowSpritesheet.getBendTopRightToBottom(); - } - - if(entrance == Direction.Bottom && exit == Direction.TopRight) { - spriteRegion = arrowSpritesheet.getBendTopRightToBottom(); - } - - if(entrance == Direction.TopLeft && exit == Direction.Right) { - spriteRegion = arrowSpritesheet.getBendTopLeftToRight(); - } - - if(entrance == Direction.Right && exit == Direction.TopLeft) { - spriteRegion = arrowSpritesheet.getBendTopLeftToRight(); - } - - if(entrance == Direction.BottomLeft && exit == Direction.Right) { - spriteRegion = arrowSpritesheet.getBendBottomLeftToRight(); - } - - if(entrance == Direction.Right && exit == Direction.BottomLeft) { - spriteRegion = arrowSpritesheet.getBendBottomLeftToRight(); - } - - //game.font.draw(game.batch, entrance.toString() + " to " + exit.toString(), newPosition.x * 16, newPosition.y * 16); - - if (spriteRegion != null) { - game.batch.draw(spriteRegion, newPosition.x * 16, newPosition.y * 16); - } - - if(leftSprite != null) { - game.batch.draw(leftSprite, (newPosition.x - 1) * 16, newPosition.y * 16); - } - - if(rightSprite != null) { - game.batch.draw(rightSprite, (newPosition.x + 1) * 16, newPosition.y * 16); - } - } - - // now we want to draw the arrow or the "end" point. - Vector2 secondToLastPosition = realPath.get(realPath.size() - 2); - Vector2 lastPosition = realPath.get(realPath.size() - 1); - - Direction exit = getFromDelta(new Vector2(lastPosition.x - secondToLastPosition.x, lastPosition.y - secondToLastPosition.y)); - - TextureRegion spriteRegion = null; - - switch(exit) { - case Top: - spriteRegion = arrowSpritesheet.getArrowFromTop(); - break; - case Bottom: - spriteRegion = arrowSpritesheet.getArrowFromBottom(); - break; - case Left: - spriteRegion = arrowSpritesheet.getArrowFromLeft(); - break; - case Right: - spriteRegion = arrowSpritesheet.getArrowFromRight(); - break; - case TopLeft: - spriteRegion = arrowSpritesheet.getArrowFromTopLeft(); - break; - case TopRight: - spriteRegion = arrowSpritesheet.getArrowFromTopRight(); - break; - case BottomLeft: - spriteRegion = arrowSpritesheet.getArrowFromBottomLeft(); - break; - case BottomRight: - spriteRegion = arrowSpritesheet.getArrowFromBottomRight(); - break; - } - - if(spriteRegion != null) { - game.batch.draw(spriteRegion, lastPosition.x * 16, lastPosition.y * 16); } } - } else if (hasStartedMoving) { - Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); + } else { + // player is trying to move + if (Gdx.input.isButtonPressed(0) && !currentlyAwaitingAuxilaryAction) { + hasStartedMoving = true; - camera.unproject(mousePos); + Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); - double newX = Math.floor(mousePos.x); - double newY = Math.floor(mousePos.y); + camera.unproject(mousePos); - moveUnitTo((int) (newX / 16.0), (int) (newY / 16.0)); + double newX = Math.floor(mousePos.x); + double newY = Math.floor(mousePos.y); - hasStartedMoving = false; + int newFixedX = (int) (newX / 16.0); + int newFixedY = (int) (newY / 16.0); + + ArrayList path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY), getCollision(), tiledMap); + if (path != null && path.size() > 1) { + Collections.reverse(path); + List realPath = path.subList(0, Math.min(currentUnit.maxDistance, path.size())); + + // we start at index 1 since there's no point in drawing an arrow over the player + for (int i = 1; i < realPath.size() - 1; i++) { + Vector2 previousPosition = realPath.get(i - 1); + Vector2 newPosition = realPath.get(i); + Vector2 nextPosition = realPath.get(i + 1); + + // we want to grab two directions, first is our "entrance" and then our "exit". of course arrows that are just a position don't exist + Direction entrance = getFromDelta(new Vector2(newPosition.x - previousPosition.x, newPosition.y - previousPosition.y)); + Direction exit = getFromDelta(new Vector2(newPosition.x - nextPosition.x, newPosition.y - nextPosition.y)); + + TextureRegion spriteRegion = null; + TextureRegion leftSprite = null; + TextureRegion rightSprite = null; + + // TODO: condense duplicate branches + + // handle straights + if(entrance == Direction.Bottom && exit == Direction.Top) { + spriteRegion = arrowSpritesheet.getUpDown(); + } + + if(entrance == Direction.Top && exit == Direction.Bottom) { + spriteRegion = arrowSpritesheet.getUpDown(); + } + + if(entrance == Direction.Left && exit == Direction.Right) { + spriteRegion = arrowSpritesheet.getLeftRight(); + } + + if(entrance == Direction.Right && exit == Direction.Left) { + spriteRegion = arrowSpritesheet.getLeftRight(); + } + + // handle crosses + if(entrance == Direction.TopLeft && exit == Direction.BottomRight) { + spriteRegion = arrowSpritesheet.getDiagDown(); + } + + if(entrance == Direction.TopLeft && exit == Direction.TopLeft) { + spriteRegion = arrowSpritesheet.getDiagDown(); + } + + if(entrance == Direction.BottomLeft && exit == Direction.BottomLeft) { + spriteRegion = arrowSpritesheet.getDiagUp(); + } + + if(entrance == Direction.BottomLeft && exit == Direction.TopRight) { + spriteRegion = arrowSpritesheet.getDiagUp(); + } + + if(entrance == Direction.BottomRight && exit == Direction.TopLeft) { + spriteRegion = arrowSpritesheet.getDiagDown(); + } + + if(entrance == Direction.TopRight && exit == Direction.BottomLeft) { + spriteRegion = arrowSpritesheet.getDiagDown(); + } + + if(spriteRegion == arrowSpritesheet.getDiagDown()) { + leftSprite = arrowSpritesheet.getCornerTopRight(); + rightSprite = arrowSpritesheet.getCornerBottomLeft(); + } + + if(spriteRegion == arrowSpritesheet.getDiagUp()) { + leftSprite = arrowSpritesheet.getCornerBottomRight(); + rightSprite = arrowSpritesheet.getCornerTopLeft(); + } + + // handle bends + if(entrance == Direction.BottomRight && exit == Direction.Top) { + spriteRegion = arrowSpritesheet.getBendBottomRightToTop(); + } + + if(entrance == Direction.Top && exit == Direction.BottomRight) { + spriteRegion = arrowSpritesheet.getBendBottomRightToTop(); + } + + if(entrance == Direction.BottomLeft && exit == Direction.Top) { + spriteRegion = arrowSpritesheet.getBendBottomLeftToTop(); + } + + if(entrance == Direction.Top && exit == Direction.BottomLeft) { + spriteRegion = arrowSpritesheet.getBendBottomLeftToTop(); + } + + if(entrance == Direction.TopLeft && exit == Direction.Bottom) { + spriteRegion = arrowSpritesheet.getBendTopLeftToBottom(); + } + + if(entrance == Direction.Bottom && exit == Direction.TopLeft) { + spriteRegion = arrowSpritesheet.getBendTopLeftToBottom(); + } + + if(entrance == Direction.TopRight && exit == Direction.Bottom) { + spriteRegion = arrowSpritesheet.getBendTopRightToBottom(); + } + + if(entrance == Direction.Bottom && exit == Direction.TopRight) { + spriteRegion = arrowSpritesheet.getBendTopRightToBottom(); + } + + if(entrance == Direction.TopLeft && exit == Direction.Right) { + spriteRegion = arrowSpritesheet.getBendTopLeftToRight(); + } + + if(entrance == Direction.Right && exit == Direction.TopLeft) { + spriteRegion = arrowSpritesheet.getBendTopLeftToRight(); + } + + if(entrance == Direction.BottomLeft && exit == Direction.Right) { + spriteRegion = arrowSpritesheet.getBendBottomLeftToRight(); + } + + if(entrance == Direction.Right && exit == Direction.BottomLeft) { + spriteRegion = arrowSpritesheet.getBendBottomLeftToRight(); + } + + //game.font.draw(game.batch, entrance.toString() + " to " + exit.toString(), newPosition.x * 16, newPosition.y * 16); + + if (spriteRegion != null) { + game.batch.draw(spriteRegion, newPosition.x * 16, newPosition.y * 16); + } + + if(leftSprite != null) { + game.batch.draw(leftSprite, (newPosition.x - 1) * 16, newPosition.y * 16); + } + + if(rightSprite != null) { + game.batch.draw(rightSprite, (newPosition.x + 1) * 16, newPosition.y * 16); + } + } + + // now we want to draw the arrow or the "end" point. + Vector2 secondToLastPosition = realPath.get(realPath.size() - 2); + Vector2 lastPosition = realPath.get(realPath.size() - 1); + + Direction exit = getFromDelta(new Vector2(lastPosition.x - secondToLastPosition.x, lastPosition.y - secondToLastPosition.y)); + + TextureRegion spriteRegion = null; + + switch(exit) { + case Top: + spriteRegion = arrowSpritesheet.getArrowFromTop(); + break; + case Bottom: + spriteRegion = arrowSpritesheet.getArrowFromBottom(); + break; + case Left: + spriteRegion = arrowSpritesheet.getArrowFromLeft(); + break; + case Right: + spriteRegion = arrowSpritesheet.getArrowFromRight(); + break; + case TopLeft: + spriteRegion = arrowSpritesheet.getArrowFromTopLeft(); + break; + case TopRight: + spriteRegion = arrowSpritesheet.getArrowFromTopRight(); + break; + case BottomLeft: + spriteRegion = arrowSpritesheet.getArrowFromBottomLeft(); + break; + case BottomRight: + spriteRegion = arrowSpritesheet.getArrowFromBottomRight(); + break; + } + + if(spriteRegion != null) { + game.batch.draw(spriteRegion, lastPosition.x * 16, lastPosition.y * 16); + } + } + } else if (hasStartedMoving) { + 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); + + moveUnitTo((int) (newX / 16.0), (int) (newY / 16.0)); + + hasStartedMoving = false; + } } } @@ -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)); } } } } - game.batch.draw(cursorTexture, cursorX * 16, cursorY * 16); + 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(); }