Merge remote-tracking branch 'origin/master'

This commit is contained in:
Katchan 2021-12-09 21:02:06 +01:00
commit cdb11a2de6
12 changed files with 87 additions and 95 deletions

View File

@ -146,7 +146,6 @@ public class App extends Application {
default -> throw new IllegalArgumentException("Unknown argument: " + str); default -> throw new IllegalArgumentException("Unknown argument: " + str);
} }
} }
System.out.println("oui");
if(playerClass != null) if(playerClass != null)
playerList.add(createNewPlayer(playerClass, playerList.add(createNewPlayer(playerClass,
classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size())

View File

@ -98,6 +98,12 @@ public class Game {
try { try {
actions.add(new Move(this, player, direction)); actions.add(new Move(this, player, direction));
} catch (NotValidDirectionException ignored){} } catch (NotValidDirectionException ignored){}
try {
actions.add(new Shot(this, player, direction));
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}
}
for(Direction8Axis direction : Direction8Axis.values()) {
try { try {
actions.add(new DropBomb(this, player, direction)); actions.add(new DropBomb(this, player, direction));
} catch (NotValidDirectionException ignored) {} } catch (NotValidDirectionException ignored) {}
@ -105,11 +111,6 @@ public class Game {
actions.add(new DropMine(this, player, direction)); actions.add(new DropMine(this, player, direction));
} catch (NotValidDirectionException ignored) {} } 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))); actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player)));
return actions; return actions;
} }

View File

@ -5,17 +5,19 @@ package fr.lnl.game.server.games.action;
*/ */
public enum Direction4Axis implements Direction { public enum Direction4Axis implements Direction {
UP(-1, 0), UP(-1, 0, true),
DOWN(1, 0), DOWN(1, 0, true),
LEFT(0, -1), LEFT(0, -1, false),
RIGHT(0, 1); RIGHT(0, 1, false);
private final int deltaX; private final int deltaX;
private final int deltaY; private final int deltaY;
private final boolean isVertical;
Direction4Axis(int i, int i1) { Direction4Axis(int i, int i1, boolean isVertical) {
this.deltaX = i; this.deltaX = i;
this.deltaY = i1; this.deltaY = i1;
this.isVertical = isVertical;
} }
@Override @Override
@ -27,4 +29,13 @@ public enum Direction4Axis implements Direction {
public int getDeltaY() { public int getDeltaY() {
return deltaY; 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;
}
} }

View File

@ -2,23 +2,21 @@ package fr.lnl.game.server.games.action;
public enum Direction8Axis implements Direction { public enum Direction8Axis implements Direction {
UP(-1, 0, true), UP(-1, 0),
DOWN(1, 0, true), DOWN(1, 0),
LEFT(0, -1, false), LEFT(0, -1),
RIGHT(0, 1, false), RIGHT(0, 1),
UP_LEFT(-1, -1, true), UP_LEFT(-1, -1),
UP_RIGHT(-1, 1, true), UP_RIGHT(-1, 1),
DOWN_RIGHT(1, 1, true), DOWN_RIGHT(1, 1),
DOWN_LEFT(1, -1, true); DOWN_LEFT(1, -1);
private final int deltaX; private final int deltaX;
private final int deltaY; private final int deltaY;
private final boolean isVertical;
Direction8Axis(int i, int i1, boolean isVertical) { Direction8Axis(int i, int i1) {
this.deltaX = i; this.deltaX = i;
this.deltaY = i1; this.deltaY = i1;
this.isVertical = isVertical;
} }
@Override @Override
@ -31,14 +29,5 @@ public enum Direction8Axis implements Direction {
return deltaY; 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;
}
} }

View File

@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player;
*/ */
public class DropBomb extends DropObject { 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); super(game, player, direction);
} }

View File

@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player;
*/ */
public class DropMine extends DropObject { 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); super(game, player, direction);
} }
@ -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

@ -16,14 +16,14 @@ import java.util.List;
public abstract class DropObject extends AbstractAction { public abstract class DropObject extends AbstractAction {
protected final Point point; protected final Point point;
private final Direction4Axis direction; private final Direction8Axis direction;
/** /**
* @param player basically current player * @param player basically current player
* @param direction chosen direction * @param direction chosen direction
* @throws NotValidDirectionException throw when the chosen direction is invalid * @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); super(game, player);
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Point playerPosition = player.getPosition(); Point playerPosition = player.getPosition();
@ -67,7 +67,7 @@ public abstract class DropObject extends AbstractAction {
return listMoves; return listMoves;
} }
public Direction4Axis getDirection() { public Direction8Axis getDirection() {
return direction; return direction;
} }

View File

@ -12,9 +12,9 @@ import java.util.List;
public class Shot extends AbstractAction { public class Shot extends AbstractAction {
private final Point point; 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); super(game, player);
if(player.getWeapon().getBullet() == 0) { if(player.getWeapon().getBullet() == 0) {
throw new NoMoreBulletInWeaponException(); throw new NoMoreBulletInWeaponException();
@ -75,7 +75,7 @@ public class Shot extends AbstractAction {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
Point position = game.getCurrentPlayer().getPosition(); Point position = game.getCurrentPlayer().getPosition();
Weapon weapon = game.getCurrentPlayer().getWeapon(); Weapon weapon = game.getCurrentPlayer().getWeapon();
for(Direction8Axis direction : Direction8Axis.values()) { for(Direction4Axis direction : Direction4Axis.values()) {
Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(), Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(),
direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance()); direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance());
if(neighbour != null) if(neighbour != null)
@ -102,7 +102,6 @@ public class Shot extends AbstractAction {
return null; return null;
} }
if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) { if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) {
System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPosition());
return neighbour; return neighbour;
} }
} }

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

View File

@ -65,7 +65,7 @@ public class ActionPlayerTest {
@Test @Test
public void shotActionTest(){ public void shotActionTest(){
Action shot = null; Action shot = null;
for(Direction8Axis direction : Direction8Axis.values()) { for(Direction4Axis direction : Direction4Axis.values()) {
try { try {
shot = new Shot(game, game.getCurrentPlayer(), direction); shot = new Shot(game, game.getCurrentPlayer(), direction);
break; break;
@ -84,8 +84,8 @@ public class ActionPlayerTest {
public void dropBombActionTest() { public void dropBombActionTest() {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
Action action = null; Action action = null;
Direction4Axis savedDirection = null; Direction8Axis savedDirection = null;
for(Direction4Axis direction : Direction4Axis.values()) { for(Direction8Axis direction : Direction8Axis.values()) {
try { try {
action = new DropBomb(game, game.getCurrentPlayer(), direction); action = new DropBomb(game, game.getCurrentPlayer(), direction);
savedDirection = direction; savedDirection = direction;