Reworked how work Bomb and Mine, fix Bomb not exploding correctly when player walks on it

This commit is contained in:
Quentin Legot 2021-12-09 20:26:26 +01:00
parent 80b737ad5f
commit dcd100cb76
4 changed files with 45 additions and 52 deletions

View File

@ -19,7 +19,7 @@ public class DropMine extends DropObject {
*/ */
@Override @Override
public void doAction() { 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()); game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost());
} }

View File

@ -10,10 +10,6 @@ import fr.lnl.game.server.utils.Point;
*/ */
public class Bomb extends Explosive implements CountdownBox { public class Bomb extends Explosive implements CountdownBox {
/**
* Position of the bomb
*/
private final Point point;
private final Game game; private final Game game;
/** /**
* Timer before explosion * Timer before explosion
@ -25,25 +21,25 @@ public class Bomb extends Explosive implements CountdownBox {
private static final int EXPLOSION_SIZE = 2; private static final int EXPLOSION_SIZE = 2;
public Bomb(Point point, Game game) { public Bomb(Point point, Game game) {
super(game.getCurrentPlayer()); super(game.getCurrentPlayer(), point);
this.point = point;
this.game = game; this.game = game;
counter = counter * game.getPlayers().size(); counter = counter * game.getPlayers().size();
} }
/** protected void explode(Grid grid) {
* Decrement players energy around this element for(int i = -EXPLOSION_SIZE; i < EXPLOSION_SIZE; i++) {
* @param grid Game's grid for(int j = -EXPLOSION_SIZE; j < EXPLOSION_SIZE; j++) {
* @param player the player who walks on this element if(pythagoras(i, j) <= EXPLOSION_SIZE) { // recherche en cercle, pas en carré
* @param position position of this element on the grid Point position = new Point(point.getA() + i, point.getB() + j);
* @see InteractiveBox#interact(Grid, Player, Point) if(grid.boardPositionIsValid(position)) {
* @see Explosive#interact(Grid, Player, Point) Player player = grid.getBoard().get(position).getA();
*/ if(player != null)
@Override 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); }
super.explode(grid);
} }
/** /**
@ -52,21 +48,9 @@ public class Bomb extends Explosive implements CountdownBox {
*/ */
@Override @Override
public void update() { public void update() {
Grid grid = game.getGrid();
counter--; counter--;
if(counter == 0) { if(counter == 0) {
for(int i = -EXPLOSION_SIZE; i < EXPLOSION_SIZE; i++) { explode(game.getGrid());
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);
}
}
}
}
} }
} }

View File

@ -9,24 +9,39 @@ import fr.lnl.game.server.utils.Point;
*/ */
public abstract class Explosive extends AbstractBox implements InteractiveBox { 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.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 grid Game's grid
* @param player the player who walks on this element * @param player the player who walks on this element
* @param position position of this element on the grid * @param position position of this element on the grid
* @see InteractiveBox#interact(Grid, Player, Point) * @see InteractiveBox#interact(Grid, Player, Point)
* @see Explosive#interact(Grid, Player, Point)
*/ */
@Override @Override
public void interact(Grid grid, Player player, Point position) { public void interact(Grid grid, Player player, Point position) {
if(grid.getBoard().get(position).getB() == this){ explode(grid);
grid.getBoard().get(position).setB(null); }
}
/**
* 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() { public Player getPlayer() {

View File

@ -9,21 +9,15 @@ import fr.lnl.game.server.utils.Point;
*/ */
public class Mine extends Explosive{ public class Mine extends Explosive{
public Mine(Player player) { public Mine(Player player, Point point) {
super(player); 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 @Override
public void interact(Grid grid, Player player, Point position) { protected void explode(Grid grid) {
player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); Player player = grid.getBoard().get(point).getA();
super.interact(grid, player, position); if(player != null)
player.decrementEnergy(player.getClassPlayer().getPenaltyMine());
super.explode(grid);
} }
} }