From 4b5f9b719df8bf4e59d2b0743dfe62f959159d0a Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 7 Nov 2021 15:15:27 +0100 Subject: [PATCH] Improved interaction with box --- .../java/fr/lnl/game/server/games/action/Move.java | 13 ++----------- .../java/fr/lnl/game/server/games/grid/Bomb.java | 12 +++++++++++- .../fr/lnl/game/server/games/grid/EnergyBall.java | 11 ++++++++++- .../fr/lnl/game/server/games/grid/Explosive.java | 10 +++++++++- .../lnl/game/server/games/grid/InteractiveBox.java | 11 +++++++++++ .../java/fr/lnl/game/server/games/grid/Mine.java | 9 +++++++++ 6 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 server/src/main/java/fr/lnl/game/server/games/grid/InteractiveBox.java diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Move.java b/server/src/main/java/fr/lnl/game/server/games/action/Move.java index 28f077a..8d27b4a 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Move.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Move.java @@ -34,17 +34,8 @@ public class Move extends AbstractAction { player.setPoint(this.point); player.decrementEnergy(player.getClassPlayer().getMoveCost()); Box box = getGame().getGrid().getBoard().get(this.point).getB(); - if (box instanceof Mine){ - player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); - getGame().getGrid().getBoard().get(this.point).setB(null); - } - if(box instanceof Bomb){ - player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); - getGame().getGrid().getBoard().get(this.point).setB(null); - } - if(box instanceof EnergyBall){ - player.incrementEnergy(player.getClassPlayer().getGainEnergy()); - getGame().getGrid().getBoard().get(this.point).setB(null); + if(box instanceof InteractiveBox interactiveBox) { + interactiveBox.interact(getGame().getGrid(), player, this.point); } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Bomb.java b/server/src/main/java/fr/lnl/game/server/games/grid/Bomb.java index 090d41a..ff7e1a7 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Bomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Bomb.java @@ -1,4 +1,14 @@ package fr.lnl.game.server.games.grid; -public class Bomb extends Explosive{ +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Point; + +public class Bomb extends Explosive { + + + @Override + public void interact(Grid grid, Player player, Point position) { + player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); + super.interact(grid, player, position); + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java b/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java index 303c715..1fd9193 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java @@ -1,10 +1,19 @@ package fr.lnl.game.server.games.grid; -public class EnergyBall implements Box{ +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Point; + +public class EnergyBall implements Box, InteractiveBox { @Override public boolean equals(Object o) { if (this == o) return true; return o != null && getClass() == o.getClass();// no var to test } + + @Override + public void interact(Grid grid, Player player, Point position) { + player.incrementEnergy(player.getClassPlayer().getGainEnergy()); + grid.getBoard().get(position).setB(null); + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java b/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java index f321505..8861a4b 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java @@ -1,4 +1,12 @@ package fr.lnl.game.server.games.grid; -public abstract class Explosive implements Box{ +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Point; + +public abstract class Explosive implements Box, InteractiveBox { + + @Override + public void interact(Grid grid, Player player, Point position) { + grid.getBoard().get(position).setB(null); + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/InteractiveBox.java b/server/src/main/java/fr/lnl/game/server/games/grid/InteractiveBox.java new file mode 100644 index 0000000..e604b8b --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/grid/InteractiveBox.java @@ -0,0 +1,11 @@ +package fr.lnl.game.server.games.grid; + +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Point; + +public interface InteractiveBox { + + + void interact(Grid grid, Player player, Point position); + +} diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Mine.java b/server/src/main/java/fr/lnl/game/server/games/grid/Mine.java index bd3b703..7f3030f 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Mine.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Mine.java @@ -1,4 +1,13 @@ package fr.lnl.game.server.games.grid; +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Point; + public class Mine extends Explosive{ + + @Override + public void interact(Grid grid, Player player, Point position) { + player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); + super.interact(grid, player, position); + } }