Add support to grid elements using a countdown like bomb

This commit is contained in:
Quentin Legot 2021-12-06 13:02:55 +01:00
parent 7dde04303c
commit e7740739d9
6 changed files with 64 additions and 35 deletions

View File

@ -3,10 +3,10 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.build.BuildStrategy;
import fr.lnl.game.server.games.grid.elements.CountdownBox;
import fr.lnl.game.server.games.player.ComputerPlayer; import fr.lnl.game.server.games.player.ComputerPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.server.listener.ModelListener;
import fr.lnl.game.server.utils.CrashException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -46,6 +46,7 @@ public class Game {
selectedAction = player.choseAction(); selectedAction = player.choseAction();
} }
selectedAction.doAction(); selectedAction.doAction();
countdownGridElementsUpdate();
nextCurrentPlayer(); nextCurrentPlayer();
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
if(isOver()) { if(isOver()) {
@ -54,21 +55,9 @@ public class Game {
} }
private void waitForInterfaceEvent() { private void countdownGridElementsUpdate() {
synchronized (this){ List<CountdownBox> countdownBoxes = this.getGrid().getAllCountdownElements();
try { countdownBoxes.forEach(CountdownBox::update);
wait();
} catch (InterruptedException e) {
throw new CrashException(e.getMessage(), e);
}
}
}
public void resumeThread() {
synchronized (this) {
notifyAll();
}
} }
public List<Action> generateAndGetPlayerActions(Player player) { public List<Action> generateAndGetPlayerActions(Player player) {
@ -111,20 +100,17 @@ public class Game {
/** /**
* Change player to the next available in the list * Change player to the next available in the list
*/ */
public boolean nextCurrentPlayer() { public void nextCurrentPlayer() {
if(isOver())
return false;
do { do {
int index = players.indexOf(currentPlayer) + 1; int index = players.indexOf(currentPlayer) + 1;
if(index == players.size()) if(index == players.size())
index = 0; index = 0;
currentPlayer = players.get(index); setCurrentPlayer(players.get(index));
} while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie } while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie
currentPlayer.setShieldDeploy(false); // on reset son état currentPlayer.setShieldDeploy(false); // on reset son état
return true;
} }
public void setCurrent_player(Player current_player) { public void setCurrentPlayer(Player current_player) {
this.currentPlayer = current_player; this.currentPlayer = current_player;
} }

View File

@ -12,14 +12,9 @@ public class DropBomb extends DropObject {
@Override @Override
public void doAction() { public void doAction() {
game.getGrid().getBoard().get(point).setB(new Bomb()); game.getGrid().getBoard().get(point).setB(new Bomb(point, game));
player.decrementEnergy(player.getClassPlayer().getBombCost()); player.decrementEnergy(player.getClassPlayer().getBombCost());
} }
@Override
public boolean isPossible() {
return super.isPossible();
}
} }

View File

@ -11,10 +11,6 @@ public class DropMine extends DropObject {
} }
/**
* @deprecated même principe que {@link Shot#doAction()}
*/
@Deprecated
@Override @Override
public void doAction() { public void doAction() {
game.getGrid().getBoard().get(point).setB(new Mine()); game.getGrid().getBoard().get(point).setB(new Mine());

View File

@ -4,6 +4,7 @@ import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -96,4 +97,13 @@ public class Grid {
return column; return column;
} }
public List<CountdownBox> getAllCountdownElements() {
List<CountdownBox> list = new ArrayList<>();
for(Pair<Player, Box> element : getBoard().values()) {
if(element.getB() instanceof CountdownBox box) {
list.add(box);
}
}
return list;
}
} }

View File

@ -1,15 +1,50 @@
package fr.lnl.game.server.games.grid.elements; package fr.lnl.game.server.games.grid.elements;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
public class Bomb extends Explosive { public class Bomb extends Explosive implements CountdownBox {
private final Point point;
private final Game game;
private int counter = 3;
private static int EXPLOSION_SIZE = 4;
public Bomb(Point point, Game game) {
this.point = point;
this.game = game;
counter = counter * game.getPlayers().size();
}
@Override @Override
public void interact(Grid grid, Player player, Point position) { public void interact(Grid grid, /* Nullable */ Player player, Point position) {
player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); if(player != null)
player.decrementEnergy(player.getClassPlayer().getPenaltyBomb());
super.interact(grid, player, position); super.interact(grid, player, position);
} }
@Override
public void update() {
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é
Grid grid = game.getGrid();
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()) {
interact(grid, null, position);
}
}
}
}
}
}
public double pythagoras(double a, double b) {
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
} }

View File

@ -0,0 +1,7 @@
package fr.lnl.game.server.games.grid.elements;
public interface CountdownBox {
void update();
}