From e7740739d942e9959775681f021e2db2fbe1f4de Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 6 Dec 2021 13:02:55 +0100 Subject: [PATCH] Add support to grid elements using a countdown like bomb --- .../java/fr/lnl/game/server/games/Game.java | 30 ++++---------- .../game/server/games/action/DropBomb.java | 7 +--- .../game/server/games/action/DropMine.java | 4 -- .../fr/lnl/game/server/games/grid/Grid.java | 10 +++++ .../game/server/games/grid/elements/Bomb.java | 41 +++++++++++++++++-- .../games/grid/elements/CountdownBox.java | 7 ++++ 6 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index 42b34b6..5d82d3f 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -3,10 +3,10 @@ package fr.lnl.game.server.games; import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.build.BuildStrategy; +import fr.lnl.game.server.games.grid.elements.CountdownBox; import fr.lnl.game.server.games.player.ComputerPlayer; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.listener.ModelListener; -import fr.lnl.game.server.utils.CrashException; import java.util.ArrayList; import java.util.Arrays; @@ -46,6 +46,7 @@ public class Game { selectedAction = player.choseAction(); } selectedAction.doAction(); + countdownGridElementsUpdate(); nextCurrentPlayer(); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); if(isOver()) { @@ -54,21 +55,9 @@ public class Game { } - private void waitForInterfaceEvent() { - synchronized (this){ - try { - wait(); - } catch (InterruptedException e) { - throw new CrashException(e.getMessage(), e); - } - } - - } - - public void resumeThread() { - synchronized (this) { - notifyAll(); - } + private void countdownGridElementsUpdate() { + List countdownBoxes = this.getGrid().getAllCountdownElements(); + countdownBoxes.forEach(CountdownBox::update); } public List generateAndGetPlayerActions(Player player) { @@ -111,20 +100,17 @@ public class Game { /** * Change player to the next available in the list */ - public boolean nextCurrentPlayer() { - if(isOver()) - return false; + public void nextCurrentPlayer() { do { int index = players.indexOf(currentPlayer) + 1; if(index == players.size()) index = 0; - currentPlayer = players.get(index); + setCurrentPlayer(players.get(index)); } while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie currentPlayer.setShieldDeploy(false); // on reset son état - return true; } - public void setCurrent_player(Player current_player) { + public void setCurrentPlayer(Player current_player) { this.currentPlayer = current_player; } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java index c346a77..0b7bea8 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java @@ -12,14 +12,9 @@ public class DropBomb extends DropObject { @Override public void doAction() { - game.getGrid().getBoard().get(point).setB(new Bomb()); + game.getGrid().getBoard().get(point).setB(new Bomb(point, game)); player.decrementEnergy(player.getClassPlayer().getBombCost()); } - @Override - public boolean isPossible() { - return super.isPossible(); - } - } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java index 7d85f02..016035b 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java @@ -11,10 +11,6 @@ public class DropMine extends DropObject { } - /** - * @deprecated même principe que {@link Shot#doAction()} - */ - @Deprecated @Override public void doAction() { game.getGrid().getBoard().get(point).setB(new Mine()); diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java index 0f39221..0d89b51 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java @@ -4,6 +4,7 @@ import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -96,4 +97,13 @@ public class Grid { return column; } + public List getAllCountdownElements() { + List list = new ArrayList<>(); + for(Pair element : getBoard().values()) { + if(element.getB() instanceof CountdownBox box) { + list.add(box); + } + } + return list; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java index f528ebe..4053d40 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java @@ -1,15 +1,50 @@ package fr.lnl.game.server.games.grid.elements; +import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; -public class Bomb extends Explosive { +public class Bomb extends Explosive implements CountdownBox { + private final Point point; + private final Game game; + private int counter = 3; + private static int EXPLOSION_SIZE = 4; + + public Bomb(Point point, Game game) { + this.point = point; + this.game = game; + counter = counter * game.getPlayers().size(); + } @Override - public void interact(Grid grid, Player player, Point position) { - player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); + public void interact(Grid grid, /* Nullable */ Player player, Point position) { + if(player != null) + player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); super.interact(grid, player, position); } + + @Override + public void update() { + counter--; + if(counter == 0) { + for(int i = -EXPLOSION_SIZE; i < EXPLOSION_SIZE; i++) { + for(int j = -EXPLOSION_SIZE; j < EXPLOSION_SIZE; j++) { + if(pythagoras(i, j) <= EXPLOSION_SIZE) { // recherche en cercle, pas en carré + Grid grid = game.getGrid(); + Point position = new Point(point.getA() + i, point.getB() + j); + if(position.getA() >= 0 && position.getA() < grid.getRow() + && position.getB() >= 0 && position.getB() < grid.getColumn()) { + interact(grid, null, position); + } + } + } + } + } + } + + public double pythagoras(double a, double b) { + return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java new file mode 100644 index 0000000..a9321f8 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java @@ -0,0 +1,7 @@ +package fr.lnl.game.server.games.grid.elements; + +public interface CountdownBox { + + void update(); + +}