From 67f8a88ce7b2b0eb5c3351e3367ca5e9d1e0b424 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 20 Sep 2022 22:15:03 +0200 Subject: [PATCH 1/5] First refactor --- core/src/nl/openweb/confetti/GridManager.java | 41 ++++++++++--- .../openweb/confetti/database/Database.java | 9 +-- .../src/nl/openweb/confetti/model/Player.java | 21 +++++++ .../openweb/confetti/model/PlayerActor.java | 57 +++++++++++++++++++ .../openweb/confetti/screens/GameScreen.java | 55 +++++++++++++----- 5 files changed, 158 insertions(+), 25 deletions(-) create mode 100644 core/src/nl/openweb/confetti/model/PlayerActor.java diff --git a/core/src/nl/openweb/confetti/GridManager.java b/core/src/nl/openweb/confetti/GridManager.java index af72f45..c149045 100644 --- a/core/src/nl/openweb/confetti/GridManager.java +++ b/core/src/nl/openweb/confetti/GridManager.java @@ -2,8 +2,11 @@ import lombok.Data; import nl.openweb.confetti.model.GridCell; +import nl.openweb.confetti.model.Move; import nl.openweb.confetti.model.Player; +import nl.openweb.confetti.model.PlayerActor; +import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; @@ -16,7 +19,7 @@ public class GridManager { private float gridStartX; private float gridStartY; - private List players; + private List players; private String activePlayerId; private static GridManager gridManager; @@ -50,24 +53,48 @@ public void setActivePlayer(Player player) { this.activePlayerId = player.getId(); } - public Player getNextActivePlayer() { + public PlayerActor getNextActivePlayer(boolean resetToStart) { int nextPlayerIndex = players.indexOf(getActivePlayer()) + 1; if(nextPlayerIndex < players.size()) { - Player player = players.get(nextPlayerIndex); + PlayerActor player = players.get(nextPlayerIndex); activePlayerId = player.getId(); return player; } else { - return null; + if (resetToStart) { + return resetActivePlayer(); + } else { + return null; + } } } - public Player resetActivePlayer() { - Player player = players.get(0); + public PlayerActor getNextActivePlayer() { + return getNextActivePlayer(false); + } + + public Point2D getCellCenterCoordinates(int cellRow, int cellColumn) { + if (cellRow > (GRID_DIMENSION - 1)) throw new IllegalArgumentException("Requested cell row does not exist"); + if (cellColumn > (GRID_DIMENSION - 1)) throw new IllegalArgumentException("Requested cell column does not exist"); + + final float cellCenterStartX = GridManager.getInstance().getGridStartX() + (cellColumn * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f); + final float cellCenterStartY = GridManager.getInstance().getGridStartY() + (cellColumn * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f); + + return new Point2D.Float(cellCenterStartX, cellCenterStartY); + } + + public void performPlayerMove() { + PlayerActor activePlayer = getActivePlayer(); + /* Move move = activePlayer.popMove(); + if (move != null) activePlayer.applyMove(move);*/ + } + + public PlayerActor resetActivePlayer() { + PlayerActor player = players.get(0); activePlayerId = player.getId(); return player; } - public Player getActivePlayer() { + public PlayerActor getActivePlayer() { return this.players.stream().filter(player -> player.getId().equals(activePlayerId)).findAny().orElseThrow(); } } diff --git a/core/src/nl/openweb/confetti/database/Database.java b/core/src/nl/openweb/confetti/database/Database.java index e609c15..a5ab2d3 100644 --- a/core/src/nl/openweb/confetti/database/Database.java +++ b/core/src/nl/openweb/confetti/database/Database.java @@ -5,6 +5,7 @@ import nl.openweb.confetti.model.GridCell; import nl.openweb.confetti.model.Move; import nl.openweb.confetti.model.Player; +import nl.openweb.confetti.model.PlayerActor; import java.sql.*; import java.util.ArrayList; @@ -62,7 +63,7 @@ private void addPlayer(String playerName, String position, String textureFilenam player.execute("INSERT INTO PLAYERS (id, name, texture, position) VALUES ('" + playerId + "', '" + playerName + "', '" + textureFilename + "', '" + position + "')"); } - public void addMoves(Player player, List moves) { + public void addMoves(PlayerActor player, List moves) { final AtomicInteger moveId = new AtomicInteger(0); moves.forEach(move -> { Statement addMovesStatement; @@ -96,15 +97,15 @@ public List getMoves(String playerId) { return moves; } - public List getPlayers() { - List players = new ArrayList<>(); + public List getPlayers() { + List players = new ArrayList<>(); try { ResultSet resultSet = conn.createStatement().executeQuery("SELECT * FROM PLAYERS"); while (resultSet.next()) { String[] positions = resultSet.getString("position").split("-"); Texture texture = new Texture(Gdx.files.internal(resultSet.getString("texture"))); - Player player = new Player( + PlayerActor player = new PlayerActor( resultSet.getString("id"), resultSet.getString("name"), texture, diff --git a/core/src/nl/openweb/confetti/model/Player.java b/core/src/nl/openweb/confetti/model/Player.java index 43ecab5..ae6abe6 100644 --- a/core/src/nl/openweb/confetti/model/Player.java +++ b/core/src/nl/openweb/confetti/model/Player.java @@ -3,7 +3,9 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.graphics.Texture; +import nl.openweb.confetti.GridManager; +import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; @@ -19,6 +21,8 @@ public class Player { private boolean dead; private List moves; public static final int AMOUNT_OF_MOVES = 5; + private boolean moving; + private Point2D position; private Music music; @@ -30,6 +34,9 @@ public Player(String id, String name, Texture spriteTexture, int row, int column this.column = column; this.moves = new ArrayList<>(); this.music = Gdx.audio.newMusic((Gdx.files.internal("death.mp3"))); + + final GridCell playerGridCell = GridManager.getInstance().getPlayerGridCell(this); + this.position = new Point2D.Float(getCellDrawXPosition(playerGridCell), getCellDrawYPosition(playerGridCell)); } public String getName() { @@ -86,12 +93,14 @@ public void applyMove(Move move) { if (newColumnPos < GRID_DIMENSION && newColumnPos >= 0) { this.column = newColumnPos; + moving = true; } else { dead = true; } if (newRowPos < GRID_DIMENSION && newRowPos >= 0) { this.row = newRowPos; + moving = true; } else { dead = true; } @@ -101,6 +110,18 @@ public void applyMove(Move move) { } } + public Point2D getPosition() { + return position; + } + + public void setPosition(Point2D position) { + this.position = position; + } + + public boolean isMoving() { + return moving; + } + public void addMove(Move move) { if (moves.size() < AMOUNT_OF_MOVES) { moves.add(move); diff --git a/core/src/nl/openweb/confetti/model/PlayerActor.java b/core/src/nl/openweb/confetti/model/PlayerActor.java new file mode 100644 index 0000000..b6e376c --- /dev/null +++ b/core/src/nl/openweb/confetti/model/PlayerActor.java @@ -0,0 +1,57 @@ +package nl.openweb.confetti.model; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.scenes.scene2d.Actor; +import lombok.Data; +import nl.openweb.confetti.GridManager; + +import java.awt.geom.Point2D; +import java.util.ArrayList; +import java.util.List; + +@Data +public class PlayerActor extends Actor { + public static final int AMOUNT_OF_MOVES = 5; + + private final String id; + private final String name; + private final Texture spriteTexture; + private int row; + private int column; + private boolean alive; + private List moves; + + public PlayerActor(String id, String name, Texture spriteTexture, int row, int column) { + this.id = id; + this.name = name; + this.spriteTexture = spriteTexture; + this.row = row; + this.column = column; + this.moves = new ArrayList<>(); + } + + @Override + public void draw(Batch batch, float parentAlpha) { + Point2D cellCenterCoordinates = GridManager.getInstance().getCellCenterCoordinates(row, column); + batch.draw(spriteTexture, (float)cellCenterCoordinates.getX(), (float)cellCenterCoordinates.getY()); + } + + public void addMove(Move move) { + if (moves.size() < AMOUNT_OF_MOVES) { + moves.add(move); + } + } + + public Move popMove() { + if (!moves.isEmpty()) + return moves.remove(0); + return null; + } + + public void revertMove() { + if(moves.size() > 0) { + moves.remove(moves.size() -1); + } + } +} diff --git a/core/src/nl/openweb/confetti/screens/GameScreen.java b/core/src/nl/openweb/confetti/screens/GameScreen.java index 8f421a5..62f27d3 100644 --- a/core/src/nl/openweb/confetti/screens/GameScreen.java +++ b/core/src/nl/openweb/confetti/screens/GameScreen.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.scenes.scene2d.Stage; import nl.openweb.confetti.ConfettiGame; import nl.openweb.confetti.GridManager; import nl.openweb.confetti.database.Database; @@ -17,6 +18,7 @@ import nl.openweb.confetti.model.GridCell; import nl.openweb.confetti.model.Move; import nl.openweb.confetti.model.Player; +import nl.openweb.confetti.model.PlayerActor; import java.util.List; import java.util.Optional; @@ -27,6 +29,7 @@ import static nl.openweb.confetti.model.Player.AMOUNT_OF_MOVES; public class GameScreen implements Screen { + private final Stage stage; private final SpriteBatch batch; private final ConfettiGame game; private final Camera camera; @@ -45,9 +48,13 @@ public GameScreen(ConfettiGame game) { this.controlsRenderer = new ShapeRenderer(); this.controlsImagesRenderer = new SpriteBatch(); this.gameNotification = new GameNotification(game, "Testing", 2, 400, 280, () -> System.out.println("Closed")); + this.stage = new Stage(game.getViewport(), batch); + + final List players = Database.getInstance().getPlayers(); + players.forEach(stage::addActor); GridManager.getInstance().init(game.getCenterX(), game.getCenterY()); - GridManager.getInstance().setPlayers(Database.getInstance().getPlayers()); + GridManager.getInstance().setPlayers(players); Database.getInstance().addMoves(GridManager.getInstance().getPlayers().get(0), List.of( new Move(GridManager.getInstance().getPlayers().get(0).getId(), 1, 1, 0)) ); @@ -56,9 +63,9 @@ public GameScreen(ConfettiGame game) { Gdx.input.setInputProcessor(new InputAdapter() { @Override public boolean keyDown(int keycode) { - int playersAlive = GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).toList().size() - 1; + int playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).toList().size() - 1; - Player currentPlayer = GridManager.getInstance().getActivePlayer(); + PlayerActor currentPlayer = GridManager.getInstance().getActivePlayer(); if (keycode == Input.Keys.LEFT) { currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), -1, 0)); } @@ -76,7 +83,7 @@ public boolean keyDown(int keycode) { } if (keycode == Input.Keys.SPACE) { if (currentPlayer.getMoves().size() == AMOUNT_OF_MOVES) { - Player nextActivePlayer = GridManager.getInstance().getNextActivePlayer(); + PlayerActor nextActivePlayer = GridManager.getInstance().getNextActivePlayer(); if (nextActivePlayer == null) { GridManager.getInstance().resetActivePlayer(); GameScreen.this.gameNotification.setText("Showtime!"); @@ -123,12 +130,12 @@ private void drawPlayers() { batch.begin(); batch.setProjectionMatrix(camera.combined); - GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).forEach(player -> { + /*GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).forEach(player -> { GridCell playerGridCell = GridManager.getInstance().getPlayerGridCell(player); if (!player.isDead()) { batch.draw(player.getSpriteTexture(), player.getCellDrawXPosition(playerGridCell), player.getCellDrawYPosition(playerGridCell)); } - }); + });*/ batch.end(); } @@ -146,7 +153,7 @@ private void drawGrid() { } private void drawControlImages() { - Player player = GridManager.getInstance().getActivePlayer(); + PlayerActor player = GridManager.getInstance().getActivePlayer(); controlsImagesRenderer.begin(); controlsImagesRenderer.setProjectionMatrix(camera.combined); controlsImagesRenderer.draw(player.getSpriteTexture(), 5, (AMOUNT_OF_MOVES * CONTROL_CELL_SIZE) + 15); @@ -172,9 +179,29 @@ private void drawControlImages() { } private void applyAllPlayerMoves() { - long playersAlive = GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).count(); + long playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).count(); + + PlayerActor activePlayer = GridManager.getInstance().getActivePlayer(); + while (activePlayer != null) { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + GridManager.getInstance().performPlayerMove(); + Optional deadPlayer = hitPlayer(activePlayer); + + if (deadPlayer.isPresent()) { + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + activePlayer = GridManager.getInstance().getNextActivePlayer(true); + } - if (playersAlive > 1) { + /* if (playersAlive > 1) { for (int i = 0; i < AMOUNT_OF_MOVES; i++) { GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).forEach(player -> { try { @@ -197,14 +224,14 @@ private void applyAllPlayerMoves() { this.gameNotification.setDialogEvent(this::startNewRound); } else { this.gameNotification.setText(GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).toList().get(0).getName() + " has WON!"); - } + }*/ } - private Optional hitPlayer(Player activePlayer) { - Player deadPlayer = null; - for (Player player : GridManager.getInstance().getPlayers()) { + private Optional hitPlayer(PlayerActor activePlayer) { + PlayerActor deadPlayer = null; + for (PlayerActor player : GridManager.getInstance().getPlayers()) { if (player.getColumn() == activePlayer.getColumn() && player.getRow() == activePlayer.getRow() && !player.getId().equals(activePlayer.getId())) { - player.setDead(true); + player.setAlive(false); gameNotification.setText(player.getName() + " killed by " + activePlayer.getName()); deadPlayer = player; } From 9f27935c8f4d4bbe052fd34b3ea31ea90746047a Mon Sep 17 00:00:00 2001 From: David Date: Sun, 25 Sep 2022 17:19:50 +0200 Subject: [PATCH 2/5] Players now use a diffferent movement system. Players now actually move. --- core/src/nl/openweb/confetti/GridManager.java | 53 +++++--- .../exception/GridOutOfBoundsException.java | 7 ++ .../nl/openweb/confetti/model/GridCell.java | 5 +- .../confetti/model/GridCoordinates.java | 31 +++++ .../openweb/confetti/model/PlayerActor.java | 117 ++++++++++++++++-- .../openweb/confetti/screens/GameScreen.java | 26 ++-- 6 files changed, 197 insertions(+), 42 deletions(-) create mode 100644 core/src/nl/openweb/confetti/exception/GridOutOfBoundsException.java create mode 100644 core/src/nl/openweb/confetti/model/GridCoordinates.java diff --git a/core/src/nl/openweb/confetti/GridManager.java b/core/src/nl/openweb/confetti/GridManager.java index c149045..c2511f9 100644 --- a/core/src/nl/openweb/confetti/GridManager.java +++ b/core/src/nl/openweb/confetti/GridManager.java @@ -1,10 +1,8 @@ package nl.openweb.confetti; import lombok.Data; -import nl.openweb.confetti.model.GridCell; -import nl.openweb.confetti.model.Move; -import nl.openweb.confetti.model.Player; -import nl.openweb.confetti.model.PlayerActor; +import nl.openweb.confetti.exception.GridOutOfBoundsException; +import nl.openweb.confetti.model.*; import java.awt.geom.Point2D; import java.util.ArrayList; @@ -15,6 +13,7 @@ @Data public class GridManager { + public static final int PLAYER_MOVE_DELAY = 500; private final List gridCells; private float gridStartX; @@ -55,16 +54,30 @@ public void setActivePlayer(Player player) { public PlayerActor getNextActivePlayer(boolean resetToStart) { int nextPlayerIndex = players.indexOf(getActivePlayer()) + 1; - if(nextPlayerIndex < players.size()) { - PlayerActor player = players.get(nextPlayerIndex); - activePlayerId = player.getId(); - return player; - } else { - if (resetToStart) { - return resetActivePlayer(); + long playersAliveCount = players.stream().filter(PlayerActor::isAlive).count(); + + if(playersAliveCount > 1) { + if (nextPlayerIndex < players.size()) { + PlayerActor player = players.get(nextPlayerIndex); + activePlayerId = player.getId(); + + if (!player.isAlive()) { + return getNextActivePlayer(); + } else { + return player; + } } else { - return null; + if (resetToStart) { + return resetActivePlayer(); + } else { + return null; + } } + } else { + PlayerActor winningPlayer = players.stream().filter(PlayerActor::isAlive).findFirst().get(); + activePlayerId = winningPlayer.getId(); + System.out.println("Only one player left, player has WON the game: " + winningPlayer.getName()); + return null; } } @@ -72,24 +85,24 @@ public PlayerActor getNextActivePlayer() { return getNextActivePlayer(false); } - public Point2D getCellCenterCoordinates(int cellRow, int cellColumn) { - if (cellRow > (GRID_DIMENSION - 1)) throw new IllegalArgumentException("Requested cell row does not exist"); - if (cellColumn > (GRID_DIMENSION - 1)) throw new IllegalArgumentException("Requested cell column does not exist"); + public Point2D getCellCenterCoordinates(GridCoordinates gridCoordinates) { + if (gridCoordinates.getRow() >= GRID_DIMENSION || gridCoordinates.getRow() < 0) throw new GridOutOfBoundsException("Requested cell row does not exist: " + gridCoordinates.getRow()); + if (gridCoordinates.getColumn() >= GRID_DIMENSION || gridCoordinates.getColumn() < 0) throw new GridOutOfBoundsException("Requested cell column does not exist: " + gridCoordinates.getColumn()); - final float cellCenterStartX = GridManager.getInstance().getGridStartX() + (cellColumn * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f); - final float cellCenterStartY = GridManager.getInstance().getGridStartY() + (cellColumn * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f); + final float cellCenterStartX = GridManager.getInstance().getGridStartX() + (gridCoordinates.getColumn() * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f); + final float cellCenterStartY = GridManager.getInstance().getGridStartY() + (gridCoordinates.getRow() * GRID_CELL_SIZE) + (GRID_CELL_SIZE / 2f); return new Point2D.Float(cellCenterStartX, cellCenterStartY); } public void performPlayerMove() { PlayerActor activePlayer = getActivePlayer(); - /* Move move = activePlayer.popMove(); - if (move != null) activePlayer.applyMove(move);*/ + Move move = activePlayer.popMove(); + if (move != null) activePlayer.applyMove(move); } public PlayerActor resetActivePlayer() { - PlayerActor player = players.get(0); + PlayerActor player = players.stream().filter(PlayerActor::isAlive).findFirst().get(); activePlayerId = player.getId(); return player; } diff --git a/core/src/nl/openweb/confetti/exception/GridOutOfBoundsException.java b/core/src/nl/openweb/confetti/exception/GridOutOfBoundsException.java new file mode 100644 index 0000000..7082ed8 --- /dev/null +++ b/core/src/nl/openweb/confetti/exception/GridOutOfBoundsException.java @@ -0,0 +1,7 @@ +package nl.openweb.confetti.exception; + +public class GridOutOfBoundsException extends RuntimeException { + public GridOutOfBoundsException(String message) { + super(message); + } +} diff --git a/core/src/nl/openweb/confetti/model/GridCell.java b/core/src/nl/openweb/confetti/model/GridCell.java index 36ae715..c68e51a 100644 --- a/core/src/nl/openweb/confetti/model/GridCell.java +++ b/core/src/nl/openweb/confetti/model/GridCell.java @@ -1,11 +1,8 @@ package nl.openweb.confetti.model; -public class GridCell { +public class GridCell extends GridCoordinates { public static final int GRID_DIMENSION = 5; public static final int GRID_CELL_SIZE = 100; - - int column; - int row; float startX; float startY; diff --git a/core/src/nl/openweb/confetti/model/GridCoordinates.java b/core/src/nl/openweb/confetti/model/GridCoordinates.java new file mode 100644 index 0000000..b4e4d66 --- /dev/null +++ b/core/src/nl/openweb/confetti/model/GridCoordinates.java @@ -0,0 +1,31 @@ +package nl.openweb.confetti.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import nl.openweb.confetti.GridManager; + +import java.awt.geom.Point2D; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class GridCoordinates { + protected int row; + protected int column; + + public GridCoordinates applyMove(Move move) { + column += move.getDeltaX(); + row += move.getDeltaY(); + return this; + } + + public void setCoordinates(GridCoordinates coordinates) { + this.column = coordinates.getColumn(); + this.row = coordinates.getRow(); + } + + public Point2D getXYCenterCoordinates() { + return GridManager.getInstance().getCellCenterCoordinates(this); + } +} diff --git a/core/src/nl/openweb/confetti/model/PlayerActor.java b/core/src/nl/openweb/confetti/model/PlayerActor.java index b6e376c..98a01a3 100644 --- a/core/src/nl/openweb/confetti/model/PlayerActor.java +++ b/core/src/nl/openweb/confetti/model/PlayerActor.java @@ -1,40 +1,104 @@ package nl.openweb.confetti.model; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.scenes.scene2d.Actor; import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.extern.slf4j.Slf4j; import nl.openweb.confetti.GridManager; +import nl.openweb.confetti.exception.GridOutOfBoundsException; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; +import java.util.Optional; + +import static nl.openweb.confetti.GridManager.PLAYER_MOVE_DELAY; @Data +@EqualsAndHashCode(callSuper = true) public class PlayerActor extends Actor { public static final int AMOUNT_OF_MOVES = 5; private final String id; private final String name; private final Texture spriteTexture; - private int row; - private int column; + private GridCoordinates gridCoordinates; + private GridCoordinates targetGridCoordinates; + private float x; + private float y; private boolean alive; private List moves; + private long elapsedMoveTime; public PlayerActor(String id, String name, Texture spriteTexture, int row, int column) { this.id = id; this.name = name; this.spriteTexture = spriteTexture; - this.row = row; - this.column = column; + this.gridCoordinates = new GridCoordinates(row, column); + this.targetGridCoordinates = new GridCoordinates(row, column); + this.x = getCellXOffset(gridCoordinates); + this.y = getCellYOffset(gridCoordinates); this.moves = new ArrayList<>(); + this.alive = true; } @Override public void draw(Batch batch, float parentAlpha) { - Point2D cellCenterCoordinates = GridManager.getInstance().getCellCenterCoordinates(row, column); - batch.draw(spriteTexture, (float)cellCenterCoordinates.getX(), (float)cellCenterCoordinates.getY()); + float deltaTime = Gdx.graphics.getDeltaTime(); + float targetStartX = getCellXOffset(targetGridCoordinates); + float targetStartY = getCellYOffset(targetGridCoordinates); + + if (isAlive()) { + if (elapsedMoveTime < PLAYER_MOVE_DELAY) { + if (gridCoordinates.getRow() != targetGridCoordinates.getRow() || gridCoordinates.getColumn() != targetGridCoordinates.getColumn()) { + if (elapsedMoveTime == 0) { + //System.out.println("Start moving: " + getName()); + } + + float originalStartX = getCellXOffset(this.gridCoordinates); + float originalStartY = getCellYOffset(this.gridCoordinates); + float deltaX = targetStartX - originalStartX; + float deltaY = targetStartY - originalStartY; + + float moveXPerFrame = ((deltaTime * 1000) / PLAYER_MOVE_DELAY) * deltaX; + float moveYPerFrame = ((deltaTime * 1000) / PLAYER_MOVE_DELAY) * deltaY; + + x += moveXPerFrame; + y += moveYPerFrame; + + elapsedMoveTime += (deltaTime * 1000); + } + } else { + elapsedMoveTime = 0; + gridCoordinates.setRow(targetGridCoordinates.getRow()); + gridCoordinates.setColumn(targetGridCoordinates.getColumn()); + + x = getCellXOffset(gridCoordinates); + y = getCellYOffset(gridCoordinates); + + //System.out.println("Stop moving: " + getName()); + PlayerActor nextActivePlayer = GridManager.getInstance().getNextActivePlayer(true); + if (nextActivePlayer != null) { + System.out.println("Next active player: " + nextActivePlayer.getName()); + GridManager.getInstance().performPlayerMove(); + } + } + + batch.draw(spriteTexture, x, y); + } + } + + private float getCellXOffset(GridCoordinates coordinates) { + Point2D cellCenterCoordinates = GridManager.getInstance().getCellCenterCoordinates(coordinates); + return (float) (cellCenterCoordinates.getX() - (spriteTexture.getWidth() / 2f)); + } + + private float getCellYOffset(GridCoordinates coordinates) { + Point2D cellCenterCoordinates = GridManager.getInstance().getCellCenterCoordinates(coordinates); + return (float) (cellCenterCoordinates.getY() - (spriteTexture.getHeight() / 2f)); } public void addMove(Move move) { @@ -50,8 +114,45 @@ public Move popMove() { } public void revertMove() { - if(moves.size() > 0) { - moves.remove(moves.size() -1); + if (moves.size() > 0) { + moves.remove(moves.size() - 1); + } + } + + public void applyMove(Move move) { + GridCoordinates newGridCoordinates = targetGridCoordinates.applyMove(move); + try{ + GridManager.getInstance().getCellCenterCoordinates(newGridCoordinates); + } catch (GridOutOfBoundsException e) { + setAlive(false); + targetGridCoordinates.setCoordinates(gridCoordinates); + GridManager.getInstance().getNextActivePlayer(true); + GridManager.getInstance().performPlayerMove(); } + + Optional hitPlayer = hitPlayer(); + } + + private Optional hitPlayer() { + PlayerActor deadPlayer = null; + for (PlayerActor player : GridManager.getInstance().getPlayers()) { + if (player.getGridCoordinates().getColumn() == getTargetGridCoordinates().getColumn() && + player.getGridCoordinates().getRow() == getTargetGridCoordinates().getRow() && + !player.getId().equals(getId())) { + player.setAlive(false); + //gameNotification.setText(player.getName() + " killed by " + activePlayer.getName()); + System.out.println(player.getName() + " killed by " + getName()); + deadPlayer = player; + } + } + return Optional.ofNullable(deadPlayer); + } + + public void setAlive(boolean alive) { + this.alive = alive; + if (alive) + System.out.println(getName() + " is alive!"); + else + System.out.println(getName() + " is DEAD!"); } } diff --git a/core/src/nl/openweb/confetti/screens/GameScreen.java b/core/src/nl/openweb/confetti/screens/GameScreen.java index 62f27d3..aa91123 100644 --- a/core/src/nl/openweb/confetti/screens/GameScreen.java +++ b/core/src/nl/openweb/confetti/screens/GameScreen.java @@ -17,13 +17,12 @@ import nl.openweb.confetti.dialog.GameNotification; import nl.openweb.confetti.model.GridCell; import nl.openweb.confetti.model.Move; -import nl.openweb.confetti.model.Player; import nl.openweb.confetti.model.PlayerActor; import java.util.List; import java.util.Optional; -import java.util.concurrent.atomic.AtomicInteger; +import static nl.openweb.confetti.GridManager.PLAYER_MOVE_DELAY; import static nl.openweb.confetti.model.GridCell.GRID_CELL_SIZE; import static nl.openweb.confetti.model.GridCell.GRID_DIMENSION; import static nl.openweb.confetti.model.Player.AMOUNT_OF_MOVES; @@ -50,10 +49,11 @@ public GameScreen(ConfettiGame game) { this.gameNotification = new GameNotification(game, "Testing", 2, 400, 280, () -> System.out.println("Closed")); this.stage = new Stage(game.getViewport(), batch); + GridManager.getInstance().init(game.getCenterX(), game.getCenterY()); + final List players = Database.getInstance().getPlayers(); players.forEach(stage::addActor); - GridManager.getInstance().init(game.getCenterX(), game.getCenterY()); GridManager.getInstance().setPlayers(players); Database.getInstance().addMoves(GridManager.getInstance().getPlayers().get(0), List.of( new Move(GridManager.getInstance().getPlayers().get(0).getId(), 1, 1, 0)) @@ -129,7 +129,6 @@ public void render(float delta) { private void drawPlayers() { batch.begin(); batch.setProjectionMatrix(camera.combined); - /*GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).forEach(player -> { GridCell playerGridCell = GridManager.getInstance().getPlayerGridCell(player); if (!player.isDead()) { @@ -137,6 +136,9 @@ private void drawPlayers() { } });*/ batch.end(); + + stage.act(Gdx.graphics.getDeltaTime()); + stage.draw(); } @@ -179,12 +181,13 @@ private void drawControlImages() { } private void applyAllPlayerMoves() { - long playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).count(); + GridManager.getInstance().performPlayerMove(); + /*long playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).count(); PlayerActor activePlayer = GridManager.getInstance().getActivePlayer(); while (activePlayer != null) { - try { - Thread.sleep(500); + *//*try { + Thread.sleep(PLAYER_MOVE_DELAY); } catch (InterruptedException e) { e.printStackTrace(); } @@ -198,8 +201,9 @@ private void applyAllPlayerMoves() { e.printStackTrace(); } } - activePlayer = GridManager.getInstance().getNextActivePlayer(true); - } + activePlayer = GridManager.getInstance().getNextActivePlayer(true);*//* + + }*/ /* if (playersAlive > 1) { for (int i = 0; i < AMOUNT_OF_MOVES; i++) { @@ -230,7 +234,9 @@ private void applyAllPlayerMoves() { private Optional hitPlayer(PlayerActor activePlayer) { PlayerActor deadPlayer = null; for (PlayerActor player : GridManager.getInstance().getPlayers()) { - if (player.getColumn() == activePlayer.getColumn() && player.getRow() == activePlayer.getRow() && !player.getId().equals(activePlayer.getId())) { + if (player.getGridCoordinates().getColumn() == activePlayer.getGridCoordinates().getColumn() && + player.getGridCoordinates().getRow() == activePlayer.getGridCoordinates().getRow() && + !player.getId().equals(activePlayer.getId())) { player.setAlive(false); gameNotification.setText(player.getName() + " killed by " + activePlayer.getName()); deadPlayer = player; From 9a4e546723ee4c5ab6c4f93aa704110f458056dd Mon Sep 17 00:00:00 2001 From: David Date: Sun, 25 Sep 2022 20:38:59 +0200 Subject: [PATCH 3/5] Fixes for dialog and game ending --- core/src/nl/openweb/confetti/GridManager.java | 16 +++++++++++--- .../confetti/dialog/GameNotification.java | 22 ++++++++++++++----- .../openweb/confetti/screens/GameScreen.java | 15 +++++++------ .../openweb/confetti/screens/MainScreen.java | 2 +- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/core/src/nl/openweb/confetti/GridManager.java b/core/src/nl/openweb/confetti/GridManager.java index c2511f9..f90ff38 100644 --- a/core/src/nl/openweb/confetti/GridManager.java +++ b/core/src/nl/openweb/confetti/GridManager.java @@ -1,12 +1,14 @@ package nl.openweb.confetti; import lombok.Data; +import nl.openweb.confetti.dialog.GameNotification; import nl.openweb.confetti.exception.GridOutOfBoundsException; import nl.openweb.confetti.model.*; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import static nl.openweb.confetti.model.GridCell.GRID_CELL_SIZE; import static nl.openweb.confetti.model.GridCell.GRID_DIMENSION; @@ -56,6 +58,11 @@ public PlayerActor getNextActivePlayer(boolean resetToStart) { int nextPlayerIndex = players.indexOf(getActivePlayer()) + 1; long playersAliveCount = players.stream().filter(PlayerActor::isAlive).count(); + long playersNoMovesCount = players.stream().filter(playerActor -> playerActor.getMoves().size() == 0).count(); + if (playersNoMovesCount == players.size()) { + GameNotification.getInstance().setText("Next round!"); + } + if(playersAliveCount > 1) { if (nextPlayerIndex < players.size()) { PlayerActor player = players.get(nextPlayerIndex); @@ -74,9 +81,12 @@ public PlayerActor getNextActivePlayer(boolean resetToStart) { } } } else { - PlayerActor winningPlayer = players.stream().filter(PlayerActor::isAlive).findFirst().get(); - activePlayerId = winningPlayer.getId(); - System.out.println("Only one player left, player has WON the game: " + winningPlayer.getName()); + Optional winningPlayerOpt = players.stream().filter(PlayerActor::isAlive).findFirst(); + if (winningPlayerOpt.isPresent()) { + activePlayerId = winningPlayerOpt.get().getId(); + System.out.println("Only one player left, player has WON the game: " + winningPlayerOpt.get().getName()); + GameNotification.getInstance().setText("Winner " + winningPlayerOpt.get().getName()); + } return null; } } diff --git a/core/src/nl/openweb/confetti/dialog/GameNotification.java b/core/src/nl/openweb/confetti/dialog/GameNotification.java index 1caa246..3770e96 100644 --- a/core/src/nl/openweb/confetti/dialog/GameNotification.java +++ b/core/src/nl/openweb/confetti/dialog/GameNotification.java @@ -13,11 +13,13 @@ public class GameNotification { private static final int MARGIN = 10; - private final ShapeRenderer notificationRenderer; - private final GlyphLayout glyphLayout; - private final ConfettiGame game; - private final SpriteBatch batch; - private final float duration; + private static GameNotification gameNotification; + + private ShapeRenderer notificationRenderer; + private GlyphLayout glyphLayout; + private ConfettiGame game; + private SpriteBatch batch; + private float duration; private float width; private float height; private float startX; @@ -29,7 +31,15 @@ public class GameNotification { private float timePassed = Float.MAX_VALUE; - public GameNotification(ConfettiGame game, String text, int duration, int width, int height, DialogEvent dialogEvent) { + public static GameNotification getInstance() { + if (gameNotification == null) { + gameNotification = new GameNotification(); + } + + return gameNotification; + } + + public void init(ConfettiGame game, String text, int duration, int width, int height, DialogEvent dialogEvent) { this.game = game; this.text = text; this.duration = duration; diff --git a/core/src/nl/openweb/confetti/screens/GameScreen.java b/core/src/nl/openweb/confetti/screens/GameScreen.java index aa91123..e629603 100644 --- a/core/src/nl/openweb/confetti/screens/GameScreen.java +++ b/core/src/nl/openweb/confetti/screens/GameScreen.java @@ -46,10 +46,11 @@ public GameScreen(ConfettiGame game) { this.gridRenderer = new ShapeRenderer(); this.controlsRenderer = new ShapeRenderer(); this.controlsImagesRenderer = new SpriteBatch(); - this.gameNotification = new GameNotification(game, "Testing", 2, 400, 280, () -> System.out.println("Closed")); + this.gameNotification = new GameNotification(); this.stage = new Stage(game.getViewport(), batch); GridManager.getInstance().init(game.getCenterX(), game.getCenterY()); + GameNotification.getInstance().init(game, "Testing", 2, 400, 280, () -> System.out.println("Closed")); final List players = Database.getInstance().getPlayers(); players.forEach(stage::addActor); @@ -86,10 +87,10 @@ public boolean keyDown(int keycode) { PlayerActor nextActivePlayer = GridManager.getInstance().getNextActivePlayer(); if (nextActivePlayer == null) { GridManager.getInstance().resetActivePlayer(); - GameScreen.this.gameNotification.setText("Showtime!"); - GameScreen.this.gameNotification.setDialogEvent(() -> new Thread(() -> applyAllPlayerMoves()).start()); + GameNotification.getInstance().setText("Showtime!"); + GameNotification.getInstance().setDialogEvent(() -> new Thread(() -> applyAllPlayerMoves()).start()); } else { - GameScreen.this.gameNotification.setText(nextActivePlayer.getName()); + GameNotification.getInstance().setText(nextActivePlayer.getName()); } } } @@ -110,7 +111,7 @@ public void show() { } String playerName = GridManager.getInstance().getActivePlayer().getName(); - GameScreen.this.gameNotification.setText(playerName); + GameNotification.getInstance().setText(playerName); } @Override @@ -123,7 +124,7 @@ public void render(float delta) { drawControls(); drawControlImages(); - this.gameNotification.drawNotification(); + GameNotification.getInstance().drawNotification(); } private void drawPlayers() { @@ -285,6 +286,6 @@ public void hide() { public void dispose() { gridRenderer.dispose(); batch.dispose(); - this.gameNotification.dispose(); + GameNotification.getInstance().dispose(); } } diff --git a/core/src/nl/openweb/confetti/screens/MainScreen.java b/core/src/nl/openweb/confetti/screens/MainScreen.java index 7350e04..62e3d23 100644 --- a/core/src/nl/openweb/confetti/screens/MainScreen.java +++ b/core/src/nl/openweb/confetti/screens/MainScreen.java @@ -56,7 +56,7 @@ public void render(float delta) { batch.draw(title, game.getCenterX() - (title.getWidth() / 2f), 420); batch.end(); - drawDebugLines(); + //drawDebugLines(); } @Override From 81210d89c4218dba1d0cc73bc0e8dea0358a8093 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 28 Sep 2022 21:48:59 +0200 Subject: [PATCH 4/5] Not perfect yet, small improvements --- core/src/nl/openweb/confetti/GridManager.java | 14 +- .../confetti/dialog/GameNotification.java | 34 ++++- .../openweb/confetti/model/PlayerActor.java | 18 ++- .../openweb/confetti/screens/GameScreen.java | 122 ++++++------------ 4 files changed, 90 insertions(+), 98 deletions(-) diff --git a/core/src/nl/openweb/confetti/GridManager.java b/core/src/nl/openweb/confetti/GridManager.java index f90ff38..7b07b6a 100644 --- a/core/src/nl/openweb/confetti/GridManager.java +++ b/core/src/nl/openweb/confetti/GridManager.java @@ -23,6 +23,8 @@ public class GridManager { private List players; private String activePlayerId; + private boolean gameFinished; + private static GridManager gridManager; public static GridManager getInstance() { @@ -57,19 +59,18 @@ public void setActivePlayer(Player player) { public PlayerActor getNextActivePlayer(boolean resetToStart) { int nextPlayerIndex = players.indexOf(getActivePlayer()) + 1; long playersAliveCount = players.stream().filter(PlayerActor::isAlive).count(); - long playersNoMovesCount = players.stream().filter(playerActor -> playerActor.getMoves().size() == 0).count(); - if (playersNoMovesCount == players.size()) { - GameNotification.getInstance().setText("Next round!"); - } if(playersAliveCount > 1) { + if (playersNoMovesCount == players.size()) { + GameNotification.getInstance().showNotification("Next round!"); + } if (nextPlayerIndex < players.size()) { PlayerActor player = players.get(nextPlayerIndex); activePlayerId = player.getId(); if (!player.isAlive()) { - return getNextActivePlayer(); + return getNextActivePlayer(true); } else { return player; } @@ -83,9 +84,10 @@ public PlayerActor getNextActivePlayer(boolean resetToStart) { } else { Optional winningPlayerOpt = players.stream().filter(PlayerActor::isAlive).findFirst(); if (winningPlayerOpt.isPresent()) { + gameFinished = true; activePlayerId = winningPlayerOpt.get().getId(); System.out.println("Only one player left, player has WON the game: " + winningPlayerOpt.get().getName()); - GameNotification.getInstance().setText("Winner " + winningPlayerOpt.get().getName()); + GameNotification.getInstance().showNotification("Winner " + winningPlayerOpt.get().getName(), true); } return null; } diff --git a/core/src/nl/openweb/confetti/dialog/GameNotification.java b/core/src/nl/openweb/confetti/dialog/GameNotification.java index 3770e96..d7d9b71 100644 --- a/core/src/nl/openweb/confetti/dialog/GameNotification.java +++ b/core/src/nl/openweb/confetti/dialog/GameNotification.java @@ -7,9 +7,11 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.utils.Align; +import lombok.Getter; import nl.openweb.confetti.ConfettiGame; +import java.util.function.Function; + public class GameNotification { private static final int MARGIN = 10; @@ -27,9 +29,15 @@ public class GameNotification { private DialogEvent dialogEvent; private BitmapFont font; private String text; + + @Getter + private boolean waitForKeyPress; + + @Getter private boolean visible; private float timePassed = Float.MAX_VALUE; + private Runnable executeFunction; public static GameNotification getInstance() { if (gameNotification == null) { @@ -58,7 +66,7 @@ public void drawNotification() { float deltaTime = Gdx.graphics.getDeltaTime(); timePassed += deltaTime; - if (timePassed < duration) { + if (timePassed < duration || waitForKeyPress) { width = glyphLayout.width + 200; startX = game.getCenterX() - (width / 2f); @@ -79,7 +87,12 @@ public void drawNotification() { batch.end(); } else if (visible) { visible = false; + waitForKeyPress = false; dialogEvent.dialogClosed(); + + if (executeFunction != null) { + executeFunction.run(); + } } } @@ -91,10 +104,20 @@ public void setDialogEvent(DialogEvent dialogEvent) { this.dialogEvent = dialogEvent; } - public void setText(final String text) { + public void showNotification(final String text) { + showNotification(text, false); + } + + public void showNotification(final String text, boolean waitForKeyPress) { + showNotification(text, waitForKeyPress, null); + } + + public void showNotification(final String text, boolean waitForKeyPress, Runnable executeFunction) { this.text = text; this.timePassed = 0; this.visible = true; + this.waitForKeyPress = waitForKeyPress; + this.executeFunction = executeFunction; glyphLayout.setText(font, text); } @@ -113,6 +136,11 @@ private void createBitmapFont() { font = generator.generateFont(parameter); } + public void close() { + this.waitForKeyPress = false; + this.visible = false; + } + public void dispose() { notificationRenderer.dispose(); batch.dispose(); diff --git a/core/src/nl/openweb/confetti/model/PlayerActor.java b/core/src/nl/openweb/confetti/model/PlayerActor.java index 98a01a3..78e6d70 100644 --- a/core/src/nl/openweb/confetti/model/PlayerActor.java +++ b/core/src/nl/openweb/confetti/model/PlayerActor.java @@ -8,6 +8,7 @@ import lombok.EqualsAndHashCode; import lombok.extern.slf4j.Slf4j; import nl.openweb.confetti.GridManager; +import nl.openweb.confetti.dialog.GameNotification; import nl.openweb.confetti.exception.GridOutOfBoundsException; import java.awt.geom.Point2D; @@ -84,6 +85,8 @@ public void draw(Batch batch, float parentAlpha) { if (nextActivePlayer != null) { System.out.println("Next active player: " + nextActivePlayer.getName()); GridManager.getInstance().performPlayerMove(); + } else { + System.out.println("Unable to determine next active player!"); } } @@ -126,8 +129,13 @@ public void applyMove(Move move) { } catch (GridOutOfBoundsException e) { setAlive(false); targetGridCoordinates.setCoordinates(gridCoordinates); - GridManager.getInstance().getNextActivePlayer(true); - GridManager.getInstance().performPlayerMove(); + + Runnable executeFunction = () -> { + GridManager.getInstance().getNextActivePlayer(true); + GridManager.getInstance().performPlayerMove(); + }; + + GameNotification.getInstance().showNotification(getName() + " DIED!", false, executeFunction); } Optional hitPlayer = hitPlayer(); @@ -150,9 +158,11 @@ private Optional hitPlayer() { public void setAlive(boolean alive) { this.alive = alive; - if (alive) + if (alive) { System.out.println(getName() + " is alive!"); - else + } else { + this.moves.clear(); System.out.println(getName() + " is DEAD!"); + } } } diff --git a/core/src/nl/openweb/confetti/screens/GameScreen.java b/core/src/nl/openweb/confetti/screens/GameScreen.java index e629603..40a4fdc 100644 --- a/core/src/nl/openweb/confetti/screens/GameScreen.java +++ b/core/src/nl/openweb/confetti/screens/GameScreen.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Optional; -import static nl.openweb.confetti.GridManager.PLAYER_MOVE_DELAY; import static nl.openweb.confetti.model.GridCell.GRID_CELL_SIZE; import static nl.openweb.confetti.model.GridCell.GRID_DIMENSION; import static nl.openweb.confetti.model.Player.AMOUNT_OF_MOVES; @@ -35,9 +34,7 @@ public class GameScreen implements Screen { private final ShapeRenderer gridRenderer; private final ShapeRenderer controlsRenderer; private final SpriteBatch controlsImagesRenderer; - private final GameNotification gameNotification; private final int CONTROL_CELL_SIZE = 55; - //private final AtomicInteger activePlayer = new AtomicInteger(0); public GameScreen(ConfettiGame game) { this.game = game; @@ -46,7 +43,6 @@ public GameScreen(ConfettiGame game) { this.gridRenderer = new ShapeRenderer(); this.controlsRenderer = new ShapeRenderer(); this.controlsImagesRenderer = new SpriteBatch(); - this.gameNotification = new GameNotification(); this.stage = new Stage(game.getViewport(), batch); GridManager.getInstance().init(game.getCenterX(), game.getCenterY()); @@ -64,33 +60,42 @@ public GameScreen(ConfettiGame game) { Gdx.input.setInputProcessor(new InputAdapter() { @Override public boolean keyDown(int keycode) { - int playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).toList().size() - 1; + if (!GameNotification.getInstance().isVisible()) { + int playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).toList().size() - 1; - PlayerActor currentPlayer = GridManager.getInstance().getActivePlayer(); - if (keycode == Input.Keys.LEFT) { - currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), -1, 0)); - } - if (keycode == Input.Keys.RIGHT) { - currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), 1, 0)); - } - if (keycode == Input.Keys.UP) { - currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), 0, 1)); - } - if (keycode == Input.Keys.DOWN) { - currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), 0, -1)); - } - if (keycode == Input.Keys.BACKSPACE) { - currentPlayer.revertMove(); - } - if (keycode == Input.Keys.SPACE) { - if (currentPlayer.getMoves().size() == AMOUNT_OF_MOVES) { - PlayerActor nextActivePlayer = GridManager.getInstance().getNextActivePlayer(); - if (nextActivePlayer == null) { - GridManager.getInstance().resetActivePlayer(); - GameNotification.getInstance().setText("Showtime!"); - GameNotification.getInstance().setDialogEvent(() -> new Thread(() -> applyAllPlayerMoves()).start()); - } else { - GameNotification.getInstance().setText(nextActivePlayer.getName()); + PlayerActor currentPlayer = GridManager.getInstance().getActivePlayer(); + if (keycode == Input.Keys.LEFT) { + currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), -1, 0)); + } + if (keycode == Input.Keys.RIGHT) { + currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), 1, 0)); + } + if (keycode == Input.Keys.UP) { + currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), 0, 1)); + } + if (keycode == Input.Keys.DOWN) { + currentPlayer.addMove(new Move(currentPlayer.getId(), currentPlayer.getMoves().size(), 0, -1)); + } + if (keycode == Input.Keys.BACKSPACE) { + currentPlayer.revertMove(); + } + if (keycode == Input.Keys.SPACE) { + if (currentPlayer.getMoves().size() == AMOUNT_OF_MOVES) { + PlayerActor nextActivePlayer = GridManager.getInstance().getNextActivePlayer(); + if (nextActivePlayer == null) { + GridManager.getInstance().resetActivePlayer(); + GameNotification.getInstance().showNotification("Showtime!"); + GameNotification.getInstance().setDialogEvent(() -> new Thread(() -> applyAllPlayerMoves()).start()); + } else { + GameNotification.getInstance().showNotification(nextActivePlayer.getName()); + } + } + } + } else { + if (keycode == Input.Keys.ENTER && GameNotification.getInstance().isVisible() && GameNotification.getInstance().isWaitForKeyPress()) { + GameNotification.getInstance().close(); + if (GridManager.getInstance().isGameFinished()) { + GameScreen.this.game.setScreen(new MainScreen(GameScreen.this.game)); } } } @@ -111,7 +116,7 @@ public void show() { } String playerName = GridManager.getInstance().getActivePlayer().getName(); - GameNotification.getInstance().setText(playerName); + GameNotification.getInstance().showNotification(playerName); } @Override @@ -130,12 +135,6 @@ public void render(float delta) { private void drawPlayers() { batch.begin(); batch.setProjectionMatrix(camera.combined); - /*GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).forEach(player -> { - GridCell playerGridCell = GridManager.getInstance().getPlayerGridCell(player); - if (!player.isDead()) { - batch.draw(player.getSpriteTexture(), player.getCellDrawXPosition(playerGridCell), player.getCellDrawYPosition(playerGridCell)); - } - });*/ batch.end(); stage.act(Gdx.graphics.getDeltaTime()); @@ -183,53 +182,6 @@ private void drawControlImages() { private void applyAllPlayerMoves() { GridManager.getInstance().performPlayerMove(); - /*long playersAlive = GridManager.getInstance().getPlayers().stream().filter(PlayerActor::isAlive).count(); - - PlayerActor activePlayer = GridManager.getInstance().getActivePlayer(); - while (activePlayer != null) { - *//*try { - Thread.sleep(PLAYER_MOVE_DELAY); - } catch (InterruptedException e) { - e.printStackTrace(); - } - GridManager.getInstance().performPlayerMove(); - Optional deadPlayer = hitPlayer(activePlayer); - - if (deadPlayer.isPresent()) { - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - activePlayer = GridManager.getInstance().getNextActivePlayer(true);*//* - - }*/ - - /* if (playersAlive > 1) { - for (int i = 0; i < AMOUNT_OF_MOVES; i++) { - GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).forEach(player -> { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - //activePlayer.set(GridManager.getInstance().getPlayers().indexOf(player)); - GridManager.getInstance().setActivePlayer(player); - Move move = player.popMove(); - if (move != null) { - player.applyMove(move); - hitPlayer(player); - } - } - ); - } - - this.gameNotification.setText("Next round!"); - this.gameNotification.setDialogEvent(this::startNewRound); - } else { - this.gameNotification.setText(GridManager.getInstance().getPlayers().stream().filter(Player::isAlive).toList().get(0).getName() + " has WON!"); - }*/ } private Optional hitPlayer(PlayerActor activePlayer) { @@ -239,7 +191,7 @@ private Optional hitPlayer(PlayerActor activePlayer) { player.getGridCoordinates().getRow() == activePlayer.getGridCoordinates().getRow() && !player.getId().equals(activePlayer.getId())) { player.setAlive(false); - gameNotification.setText(player.getName() + " killed by " + activePlayer.getName()); + GameNotification.getInstance().showNotification(player.getName() + " killed by " + activePlayer.getName()); deadPlayer = player; } } From 97a76fb180c86b8c55f60f1a36edc90f9fc7b57d Mon Sep 17 00:00:00 2001 From: David Date: Thu, 29 Sep 2022 21:15:11 +0200 Subject: [PATCH 5/5] Bug fixes --- .../confetti/dialog/GameNotification.java | 25 +++++++++++++------ .../openweb/confetti/model/PlayerActor.java | 15 +++++------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/core/src/nl/openweb/confetti/dialog/GameNotification.java b/core/src/nl/openweb/confetti/dialog/GameNotification.java index d7d9b71..0a84dcf 100644 --- a/core/src/nl/openweb/confetti/dialog/GameNotification.java +++ b/core/src/nl/openweb/confetti/dialog/GameNotification.java @@ -10,15 +10,14 @@ import lombok.Getter; import nl.openweb.confetti.ConfettiGame; -import java.util.function.Function; - public class GameNotification { private static final int MARGIN = 10; private static GameNotification gameNotification; private ShapeRenderer notificationRenderer; - private GlyphLayout glyphLayout; + private GlyphLayout normalTextLayout; + private GlyphLayout subTextLayout; private ConfettiGame game; private SpriteBatch batch; private float duration; @@ -28,6 +27,7 @@ public class GameNotification { private float startY; private DialogEvent dialogEvent; private BitmapFont font; + private BitmapFont subFont; private String text; @Getter @@ -56,7 +56,8 @@ public void init(ConfettiGame game, String text, int duration, int width, int he this.startX = game.getCenterX() - (width / 2f); this.startY = game.getCenterY() - (height / 2f); this.notificationRenderer = new ShapeRenderer(); - this.glyphLayout = new GlyphLayout(); + this.normalTextLayout = new GlyphLayout(); + this.subTextLayout = new GlyphLayout(); this.dialogEvent = dialogEvent; this.batch = new SpriteBatch(); this.createBitmapFont(); @@ -67,7 +68,7 @@ public void drawNotification() { timePassed += deltaTime; if (timePassed < duration || waitForKeyPress) { - width = glyphLayout.width + 200; + width = normalTextLayout.width + 200; startX = game.getCenterX() - (width / 2f); notificationRenderer.begin(ShapeRenderer.ShapeType.Filled); @@ -83,7 +84,12 @@ public void drawNotification() { batch.begin(); batch.setProjectionMatrix(game.getCamera().combined); - font.draw(batch, glyphLayout, game.getCenterX() - (glyphLayout.width / 2f) , game.getCenterY() + 16); + font.draw(batch, normalTextLayout, game.getCenterX() - (normalTextLayout.width / 2f) , game.getCenterY() + 16); + + if (waitForKeyPress) { + subFont.draw(batch, subTextLayout, game.getCenterX() - (subTextLayout.width / 2f), game.getCenterY() - 72); + } + batch.end(); } else if (visible) { visible = false; @@ -119,7 +125,8 @@ public void showNotification(final String text, boolean waitForKeyPress, Runnabl this.waitForKeyPress = waitForKeyPress; this.executeFunction = executeFunction; - glyphLayout.setText(font, text); + normalTextLayout.setText(font, text); + subTextLayout.setText(subFont, "< ENTER >"); } private void createBitmapFont() { @@ -134,6 +141,10 @@ private void createBitmapFont() { parameter.shadowColor = new Color(0, 0.5f, 0, 0.75f); font = generator.generateFont(parameter); + + parameter.size = 16; + + subFont = generator.generateFont(parameter); } public void close() { diff --git a/core/src/nl/openweb/confetti/model/PlayerActor.java b/core/src/nl/openweb/confetti/model/PlayerActor.java index 78e6d70..aee98ea 100644 --- a/core/src/nl/openweb/confetti/model/PlayerActor.java +++ b/core/src/nl/openweb/confetti/model/PlayerActor.java @@ -126,19 +126,11 @@ public void applyMove(Move move) { GridCoordinates newGridCoordinates = targetGridCoordinates.applyMove(move); try{ GridManager.getInstance().getCellCenterCoordinates(newGridCoordinates); + Optional hitPlayer = hitPlayer(); } catch (GridOutOfBoundsException e) { setAlive(false); targetGridCoordinates.setCoordinates(gridCoordinates); - - Runnable executeFunction = () -> { - GridManager.getInstance().getNextActivePlayer(true); - GridManager.getInstance().performPlayerMove(); - }; - - GameNotification.getInstance().showNotification(getName() + " DIED!", false, executeFunction); } - - Optional hitPlayer = hitPlayer(); } private Optional hitPlayer() { @@ -163,6 +155,11 @@ public void setAlive(boolean alive) { } else { this.moves.clear(); System.out.println(getName() + " is DEAD!"); + Runnable executeFunction = () -> { + GridManager.getInstance().getNextActivePlayer(true); + }; + + GameNotification.getInstance().showNotification(getName() + " DIED!", false, executeFunction); } } }