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
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());
}

View File

@ -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) {
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.interact(grid, player, position);
}
}
}
}
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());
}
}

View File

@ -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() {

View File

@ -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) {
protected void explode(Grid grid) {
Player player = grid.getBoard().get(point).getA();
if(player != null)
player.decrementEnergy(player.getClassPlayer().getPenaltyMine());
super.interact(grid, player, position);
super.explode(grid);
}
}