From 80b737ad5fca128c380a4d5d7adb85f42b2bf4d6 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Dec 2021 19:55:16 +0100 Subject: [PATCH 1/2] fix DropBomb, DropMine and Shot not using the correct Direction implementation, delete debug messages --- .../src/main/java/fr/lnl/game/client/App.java | 1 - .../java/fr/lnl/game/server/games/Game.java | 11 +++---- .../server/games/action/Direction4Axis.java | 21 ++++++++++---- .../server/games/action/Direction8Axis.java | 29 ++++++------------- .../game/server/games/action/DropBomb.java | 2 +- .../game/server/games/action/DropMine.java | 2 +- .../game/server/games/action/DropObject.java | 6 ++-- .../fr/lnl/game/server/games/action/Shot.java | 7 ++--- .../fr/lnl/game/server/ActionPlayerTest.java | 6 ++-- 9 files changed, 42 insertions(+), 43 deletions(-) diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index 1d0fc7b..e591d1f 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -143,7 +143,6 @@ public class App extends Application { default -> throw new IllegalArgumentException("Unknown argument: " + str); } } - System.out.println("oui"); if(playerClass != null) playerList.add(createNewPlayer(playerClass, classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) 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 b6c0ea6..8e9c242 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 @@ -98,6 +98,12 @@ public class Game { try { actions.add(new Move(this, player, direction)); } catch (NotValidDirectionException ignored){} + try { + actions.add(new Shot(this, player, direction)); + } catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {} + + } + for(Direction8Axis direction : Direction8Axis.values()) { try { actions.add(new DropBomb(this, player, direction)); } catch (NotValidDirectionException ignored) {} @@ -105,11 +111,6 @@ public class Game { actions.add(new DropMine(this, player, direction)); } catch (NotValidDirectionException ignored) {} } - for(Direction8Axis direction : Direction8Axis.values()) { - try { - actions.add(new Shot(this, player, direction)); - } catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {} - } actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player))); return actions; } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java b/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java index 24dcf3d..4c3edea 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java @@ -5,17 +5,19 @@ package fr.lnl.game.server.games.action; */ public enum Direction4Axis implements Direction { - UP(-1, 0), - DOWN(1, 0), - LEFT(0, -1), - RIGHT(0, 1); + UP(-1, 0, true), + DOWN(1, 0, true), + LEFT(0, -1, false), + RIGHT(0, 1, false); private final int deltaX; private final int deltaY; + private final boolean isVertical; - Direction4Axis(int i, int i1) { + Direction4Axis(int i, int i1, boolean isVertical) { this.deltaX = i; this.deltaY = i1; + this.isVertical = isVertical; } @Override @@ -27,4 +29,13 @@ public enum Direction4Axis implements Direction { public int getDeltaY() { return deltaY; } + + /** + * Used by {@link Shot} to know if the direction is in vertical direction cause shot action can have a different + * distance depending on the direction + * @return true if the direction is vertical + */ + public boolean isVertical() { + return isVertical; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java b/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java index 3ed49b2..fe3cf48 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java @@ -2,23 +2,21 @@ package fr.lnl.game.server.games.action; public enum Direction8Axis implements Direction { - UP(-1, 0, true), - DOWN(1, 0, true), - LEFT(0, -1, false), - RIGHT(0, 1, false), - UP_LEFT(-1, -1, true), - UP_RIGHT(-1, 1, true), - DOWN_RIGHT(1, 1, true), - DOWN_LEFT(1, -1, true); + UP(-1, 0), + DOWN(1, 0), + LEFT(0, -1), + RIGHT(0, 1), + UP_LEFT(-1, -1), + UP_RIGHT(-1, 1), + DOWN_RIGHT(1, 1), + DOWN_LEFT(1, -1); private final int deltaX; private final int deltaY; - private final boolean isVertical; - Direction8Axis(int i, int i1, boolean isVertical) { + Direction8Axis(int i, int i1) { this.deltaX = i; this.deltaY = i1; - this.isVertical = isVertical; } @Override @@ -31,14 +29,5 @@ public enum Direction8Axis implements Direction { return deltaY; } - /** - * Used by {@link Shot} to know if the direction is in vertical direction cause shot action can have a different - * distance depending of the direction - * @return true if the direction is vertical - */ - public boolean isVertical() { - return isVertical; - } - } 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 44e25bc..b078633 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 @@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player; */ public class DropBomb extends DropObject { - public DropBomb(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { + public DropBomb(Game game, Player player, Direction8Axis direction) throws NotValidDirectionException { super(game, player, direction); } 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 2728609..866bc50 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 @@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player; */ public class DropMine extends DropObject { - public DropMine(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { + public DropMine(Game game, Player player, Direction8Axis direction) throws NotValidDirectionException { super(game, player, direction); } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java index dedaabd..d146b25 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java @@ -16,14 +16,14 @@ import java.util.List; public abstract class DropObject extends AbstractAction { protected final Point point; - private final Direction4Axis direction; + private final Direction8Axis direction; /** * @param player basically current player * @param direction chosen direction * @throws NotValidDirectionException throw when the chosen direction is invalid */ - public DropObject(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { + public DropObject(Game game, Player player, Direction8Axis direction) throws NotValidDirectionException { super(game, player); List points = getValidPoint(); Point playerPosition = player.getPosition(); @@ -67,7 +67,7 @@ public abstract class DropObject extends AbstractAction { return listMoves; } - public Direction4Axis getDirection() { + public Direction8Axis getDirection() { return direction; } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java index 9b4dff8..4640941 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java @@ -12,9 +12,9 @@ import java.util.List; public class Shot extends AbstractAction { private final Point point; - private final Direction8Axis direction; + private final Direction4Axis direction; - public Shot(Game game, Player player, Direction8Axis direction) throws NoMoreBulletInWeaponException, NotValidDirectionException { + public Shot(Game game, Player player, Direction4Axis direction) throws NoMoreBulletInWeaponException, NotValidDirectionException { super(game, player); if(player.getWeapon().getBullet() == 0) { throw new NoMoreBulletInWeaponException(); @@ -75,7 +75,7 @@ public class Shot extends AbstractAction { List listMoves = new ArrayList<>(); Point position = game.getCurrentPlayer().getPosition(); Weapon weapon = game.getCurrentPlayer().getWeapon(); - for(Direction8Axis direction : Direction8Axis.values()) { + for(Direction4Axis direction : Direction4Axis.values()) { Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(), direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance()); if(neighbour != null) @@ -102,7 +102,6 @@ public class Shot extends AbstractAction { return null; } if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) { - System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPosition()); return neighbour; } } diff --git a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java index fab0c87..1e2b7b9 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -65,7 +65,7 @@ public class ActionPlayerTest { @Test public void shotActionTest(){ Action shot = null; - for(Direction8Axis direction : Direction8Axis.values()) { + for(Direction4Axis direction : Direction4Axis.values()) { try { shot = new Shot(game, game.getCurrentPlayer(), direction); break; @@ -84,8 +84,8 @@ public class ActionPlayerTest { public void dropBombActionTest() { Player player = game.getCurrentPlayer(); Action action = null; - Direction4Axis savedDirection = null; - for(Direction4Axis direction : Direction4Axis.values()) { + Direction8Axis savedDirection = null; + for(Direction8Axis direction : Direction8Axis.values()) { try { action = new DropBomb(game, game.getCurrentPlayer(), direction); savedDirection = direction; From dcd100cb764ca7b8d27fdc445da6dffb7cee676f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Dec 2021 20:26:26 +0100 Subject: [PATCH 2/2] 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); } }