Add support to 8 axis to shot action

This commit is contained in:
Quentin Legot 2021-12-09 16:50:43 +01:00
parent 1b556e5ac8
commit 2b77a2a566
11 changed files with 102 additions and 57 deletions

View File

@ -94,7 +94,7 @@ public class Game {
*/ */
public List<Action> generateAndGetPlayerActions(Player player) { public List<Action> generateAndGetPlayerActions(Player player) {
List<Action> actions = new ArrayList<>(); List<Action> actions = new ArrayList<>();
for(Direction direction : Direction.values()) { for(Direction4Axis direction : Direction4Axis.values()) {
try { try {
actions.add(new Move(this, player, direction)); actions.add(new Move(this, player, direction));
} catch (NotValidDirectionException ignored){} } catch (NotValidDirectionException ignored){}
@ -104,6 +104,8 @@ public class Game {
try { try {
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 { try {
actions.add(new Shot(this, player, direction)); actions.add(new Shot(this, player, direction));
} catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {} } catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {}

View File

@ -1,39 +1,8 @@
package fr.lnl.game.server.games.action; package fr.lnl.game.server.games.action;
/** public interface Direction {
* Enum used to chose where to move, shot, etc.
*/
public enum Direction {
UP(-1, 0, true), int getDeltaX();
DOWN(1, 0, true),
LEFT(0, -1, false),
RIGHT(0, 1, false);
private final int deltaX; int getDeltaY();
private final int deltaY;
private final boolean isVertical;
Direction(int i, int i1, boolean isVertical) {
this.deltaX = i;
this.deltaY = i1;
this.isVertical = isVertical;
}
public int getDeltaX() {
return deltaX;
}
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 of the direction
* @return true if the direction is vertical
*/
public boolean isVertical() {
return isVertical;
}
} }

View File

@ -0,0 +1,30 @@
package fr.lnl.game.server.games.action;
/**
* Enum used to chose where to move, shot, etc.
*/
public enum Direction4Axis implements Direction {
UP(-1, 0),
DOWN(1, 0),
LEFT(0, -1),
RIGHT(0, 1);
private final int deltaX;
private final int deltaY;
Direction4Axis(int i, int i1) {
this.deltaX = i;
this.deltaY = i1;
}
@Override
public int getDeltaX() {
return deltaX;
}
@Override
public int getDeltaY() {
return deltaY;
}
}

View File

@ -0,0 +1,44 @@
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);
private final int deltaX;
private final int deltaY;
private final boolean isVertical;
Direction8Axis(int i, int i1, boolean isVertical) {
this.deltaX = i;
this.deltaY = i1;
this.isVertical = isVertical;
}
@Override
public int getDeltaX() {
return deltaX;
}
@Override
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 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, Direction direction) throws NotValidDirectionException { public DropBomb(Game game, Player player, Direction4Axis 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, Direction direction) throws NotValidDirectionException { public DropMine(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException {
super(game, player, direction); super(game, player, direction);
} }

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 Direction direction; private final Direction4Axis 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, Direction direction) throws NotValidDirectionException { public DropObject(Game game, Player player, Direction4Axis 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 Direction getDirection() { public Direction4Axis getDirection() {
return direction; return direction;
} }

View File

@ -18,9 +18,9 @@ import java.util.List;
public class Move extends AbstractAction { public class Move extends AbstractAction {
private final Point point; private final Point point;
private final Direction direction; private final Direction4Axis direction;
public Move(Game game, Player player, Direction direction) throws NotValidDirectionException { public Move(Game game, Player player, Direction4Axis 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();
@ -87,7 +87,7 @@ public class Move extends AbstractAction {
return point; return point;
} }
public Direction getDirection() { public Direction4Axis 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 Direction direction; private final Direction8Axis direction;
public Shot(Game game, Player player, Direction direction) throws NoMoreBulletInWeaponException, NotValidDirectionException { public Shot(Game game, Player player, Direction8Axis 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();
@ -29,11 +29,10 @@ public class Shot extends AbstractAction {
this.direction = direction; this.direction = direction;
} }
public Shot(Game game, Player player){ public Shot(Game game, Player player) {
super(game, player); super(game, player);
List<Point> points = getValidPoint(); point = null;
this.point = null; direction = null;
this.direction = null;
} }
/** /**
@ -76,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(Direction direction : Direction.values()) { for(Direction8Axis direction : Direction8Axis.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)
@ -88,8 +87,8 @@ public class Shot extends AbstractAction {
/** /**
* *
* @param point player current position * @param point player current position
* @param deltaX given by {@link Direction} * @param deltaX given by {@link Direction4Axis}
* @param deltaY given by {@link Direction} * @param deltaY given by {@link Direction4Axis}
* @param range given by {@link Weapon#getHorizontalDistance()} or {@link Weapon#getVerticalDistance()} * @param range given by {@link Weapon#getHorizontalDistance()} or {@link Weapon#getVerticalDistance()}
* @return true if there is a player in the chosen direction, false otherwise * @return true if there is a player in the chosen direction, false otherwise
*/ */

View File

@ -10,6 +10,7 @@ import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class StrategyComputerPlayer extends ComputerPlayer { public class StrategyComputerPlayer extends ComputerPlayer {

View File

@ -35,8 +35,8 @@ public class ActionPlayerTest {
public void moveActionTest() { public void moveActionTest() {
Action move = null; Action move = null;
Point oldPoint = game.getCurrentPlayer().getPosition(); Point oldPoint = game.getCurrentPlayer().getPosition();
Direction savedDirection = null; Direction4Axis savedDirection = null;
for(Direction direction : Direction.values()) { for(Direction4Axis direction : Direction4Axis.values()) {
try { try {
move = new Move(game, game.getCurrentPlayer(), direction); move = new Move(game, game.getCurrentPlayer(), direction);
savedDirection = direction; savedDirection = direction;
@ -65,7 +65,7 @@ public class ActionPlayerTest {
@Test @Test
public void shotActionTest(){ public void shotActionTest(){
Action shot = null; Action shot = null;
for(Direction direction : Direction.values()) { for(Direction8Axis direction : Direction8Axis.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;
Direction savedDirection = null; Direction4Axis savedDirection = null;
for(Direction direction : Direction.values()) { for(Direction4Axis direction : Direction4Axis.values()) {
try { try {
action = new DropBomb(game, game.getCurrentPlayer(), direction); action = new DropBomb(game, game.getCurrentPlayer(), direction);
savedDirection = direction; savedDirection = direction;