From dcd100cb764ca7b8d27fdc445da6dffb7cee676f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Dec 2021 20:26:26 +0100 Subject: [PATCH] Reworked how work Bomb and Mine, fix Bomb not exploding correctly when player walks on it --- .../game/server/games/action/DropMine.java | 2 +- .../game/server/games/grid/elements/Bomb.java | 48 +++++++------------ .../server/games/grid/elements/Explosive.java | 27 ++++++++--- .../game/server/games/grid/elements/Mine.java | 20 +++----- 4 files changed, 45 insertions(+), 52 deletions(-) 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 866bc50..d70ffec 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 @@ -19,7 +19,7 @@ public class DropMine extends DropObject { */ @Override public void doAction() { - game.getGrid().getBoard().get(point).setB(new Mine(player)); + game.getGrid().getBoard().get(point).setB(new Mine(player, point)); game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost()); } 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 a1681bf..ca72f0e 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 @@ -10,10 +10,6 @@ import fr.lnl.game.server.utils.Point; */ public class Bomb extends Explosive implements CountdownBox { - /** - * Position of the bomb - */ - private final Point point; private final Game game; /** * Timer before explosion @@ -25,25 +21,25 @@ public class Bomb extends Explosive implements CountdownBox { private static final int EXPLOSION_SIZE = 2; public Bomb(Point point, Game game) { - super(game.getCurrentPlayer()); - this.point = point; + super(game.getCurrentPlayer(), point); this.game = game; counter = counter * game.getPlayers().size(); } - /** - * Decrement players energy around this element - * @param grid Game's grid - * @param player the player who walks on this element - * @param position position of this element on the grid - * @see InteractiveBox#interact(Grid, Player, Point) - * @see Explosive#interact(Grid, Player, Point) - */ - @Override - public void interact(Grid grid, /* Nullable */ Player player, Point position) { - if(player != null) - player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); - super.interact(grid, player, position); + protected void explode(Grid grid) { + 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é + Point position = new Point(point.getA() + i, point.getB() + j); + if(grid.boardPositionIsValid(position)) { + Player player = grid.getBoard().get(position).getA(); + if(player != null) + player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); + } + } + } + } + super.explode(grid); } /** @@ -52,21 +48,9 @@ public class Bomb extends Explosive implements CountdownBox { */ @Override public void update() { - Grid grid = game.getGrid(); 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é - 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()) { - Player player = grid.getBoard().get(position).getA(); - interact(grid, player, position); - } - } - } - } + explode(game.getGrid()); } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java index e671f67..fad2c94 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java @@ -9,24 +9,39 @@ import fr.lnl.game.server.utils.Point; */ public abstract class Explosive extends AbstractBox implements InteractiveBox { - Player player; + /** + * Position of the explosive + */ + protected final Point point; + /** + * Owner of the explosive + */ + protected final Player player; - public Explosive(Player player){ + public Explosive(Player player, Point point){ this.player = player; + this.point = point; } /** - * Destroy this element on explosion + * Decrement energy of the player who walks on this element * @param grid Game's grid * @param player the player who walks on this element * @param position position of this element on the grid * @see InteractiveBox#interact(Grid, Player, Point) + * @see Explosive#interact(Grid, Player, Point) */ @Override public void interact(Grid grid, Player player, Point position) { - if(grid.getBoard().get(position).getB() == this){ - grid.getBoard().get(position).setB(null); - } + explode(grid); + } + + /** + * Apply damage to players and delete this object + * @param grid game's grid + */ + protected void explode(Grid grid) { + grid.getBoard().get(point).setB(null); } public Player getPlayer() { diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java index 82a0978..bfe8f8b 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java @@ -9,21 +9,15 @@ import fr.lnl.game.server.utils.Point; */ public class Mine extends Explosive{ - public Mine(Player player) { - super(player); + public Mine(Player player, Point point) { + super(player, point); } - /** - * Decrement energy of the player who walks on this element - * @param grid Game's grid - * @param player the player who walks on this element - * @param position position of this element on the grid - * @see InteractiveBox#interact(Grid, Player, Point) - * @see Explosive#interact(Grid, Player, Point) - */ @Override - public void interact(Grid grid, Player player, Point position) { - player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); - super.interact(grid, player, position); + protected void explode(Grid grid) { + Player player = grid.getBoard().get(point).getA(); + if(player != null) + player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); + super.explode(grid); } }