Improved actions consistency
This commit is contained in:
parent
4b5f9b719d
commit
cb4c020fe6
@ -8,22 +8,15 @@ import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class AbstractAction implements Action {
|
||||
private final Game game;
|
||||
private final Player player;
|
||||
|
||||
protected final Game game;
|
||||
protected final Player player;
|
||||
|
||||
public AbstractAction(Game game, Player player){
|
||||
this.game = game;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
protected Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
protected Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
protected Point choseRandomPoint(List<Point> getValidPoint) {
|
||||
Point point = null;
|
||||
switch (getValidPoint.size()) {
|
||||
|
@ -1,19 +1,18 @@
|
||||
package fr.lnl.game.server.games.action;
|
||||
|
||||
import fr.lnl.game.server.games.Game;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DeployShield extends AbstractAction {
|
||||
public DeployShield(Game game, Player player){
|
||||
super(game, player);
|
||||
|
||||
public DeployShield(Player player){
|
||||
super(null, player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAction(){
|
||||
Player player = getGame().getCurrentPlayer();
|
||||
player.setShieldDeploy(true);
|
||||
player.decrementEnergy(player.getClassPlayer().getShieldCost());
|
||||
}
|
||||
|
@ -13,13 +13,11 @@ public class DropBomb extends DropObject {
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
// voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine
|
||||
@Override
|
||||
public void doAction() {
|
||||
List<Point> points = getValidPoint();
|
||||
Point point = choseRandomPoint(points);
|
||||
getGame().getGrid().getBoard().get(point).setB(new Bomb());
|
||||
Player player = getGame().getCurrentPlayer();
|
||||
game.getGrid().getBoard().get(point).setB(new Bomb());
|
||||
player.decrementEnergy(player.getClassPlayer().getBombCost());
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ import fr.lnl.game.server.utils.Point;
|
||||
import java.util.List;
|
||||
|
||||
public class DropMine extends DropObject {
|
||||
|
||||
public DropMine(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
@ -15,10 +16,8 @@ public class DropMine extends DropObject {
|
||||
public void doAction() {
|
||||
List<Point> points = getValidPoint();
|
||||
Point point = choseRandomPoint(points);
|
||||
Mine mine = new Mine();
|
||||
getGame().getGrid().getBoard().get(point).setB(mine);
|
||||
Player player = getGame().getCurrentPlayer();
|
||||
getGame().getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost());
|
||||
game.getGrid().getBoard().get(point).setB(new Mine());
|
||||
game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,10 +16,6 @@ public abstract class DropObject extends AbstractAction {
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAction() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPossible() {
|
||||
return !getValidPoint().isEmpty();
|
||||
@ -27,11 +23,11 @@ public abstract class DropObject extends AbstractAction {
|
||||
|
||||
public List<Point> getValidPoint() {
|
||||
List<Point> listMoves = new ArrayList<>();
|
||||
HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard();
|
||||
Point position = getGame().getCurrentPlayer().getPoint();
|
||||
HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard();
|
||||
Point position = player.getPoint();
|
||||
for (int row = -1; row <= 1; row++) {
|
||||
for (int column = -1; column <= 1; column++) {
|
||||
if(getGame().getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){
|
||||
if(game.getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){
|
||||
Point neighbour = new Point(position.getA() + row, position.getB() + column);
|
||||
Pair<Player, Box> state = board.get(neighbour);
|
||||
if(state.getA() == null && state.getB() == null){
|
||||
|
@ -28,14 +28,13 @@ public class Move extends AbstractAction {
|
||||
|
||||
@Override
|
||||
public void doAction() {
|
||||
Player player = getGame().getCurrentPlayer();
|
||||
getGame().getGrid().getBoard().get(player.getPoint()).setA(null);
|
||||
getGame().getGrid().getBoard().get(this.point).setA(player);
|
||||
game.getGrid().getBoard().get(player.getPoint()).setA(null);
|
||||
game.getGrid().getBoard().get(this.point).setA(player);
|
||||
player.setPoint(this.point);
|
||||
player.decrementEnergy(player.getClassPlayer().getMoveCost());
|
||||
Box box = getGame().getGrid().getBoard().get(this.point).getB();
|
||||
Box box = game.getGrid().getBoard().get(this.point).getB();
|
||||
if(box instanceof InteractiveBox interactiveBox) {
|
||||
interactiveBox.interact(getGame().getGrid(), player, this.point);
|
||||
interactiveBox.interact(game.getGrid(), player, this.point);
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,12 +46,12 @@ public class Move extends AbstractAction {
|
||||
@Override
|
||||
public List<Point> getValidPoint() {
|
||||
List<Point> listMoves = new ArrayList<>();
|
||||
HashMap<Point, Pair<Player, Box>> board = getGame().getGrid().getBoard();
|
||||
Point position = getPlayer().getPoint();
|
||||
HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard();
|
||||
Point position = player.getPoint();
|
||||
for (int deltarow = -1; deltarow <= 1; deltarow++) {
|
||||
for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) {
|
||||
if(deltarow == 0 || deltacolumn == 0){
|
||||
if(getGame().getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){
|
||||
if(game.getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){
|
||||
Point neighbour = new Point(position.getA() + deltarow, position.getB() + deltacolumn);
|
||||
Pair<Player, Box> state = board.get(neighbour);
|
||||
if(state.getA() == null && !(state.getB() instanceof Wall)){
|
||||
|
@ -1,17 +1,11 @@
|
||||
package fr.lnl.game.server.games.action;
|
||||
|
||||
import fr.lnl.game.server.games.Game;
|
||||
import fr.lnl.game.server.games.player.Player;
|
||||
import fr.lnl.game.server.utils.Point;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Nothing extends AbstractAction {
|
||||
|
||||
public Nothing(Game game, Player player){
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
public Nothing() {
|
||||
super(null, null);
|
||||
}
|
||||
|
@ -10,18 +10,20 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Shot extends AbstractAction {
|
||||
|
||||
public Shot(Game game, Player player) {
|
||||
super(game, player);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated a rewrite -> L'aléatoire ne devrait pas être ici, mais au moment de l'instanciation comme dans {@link Move}
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public void doAction() {
|
||||
List<Point> points = getValidPoint();
|
||||
Point point = choseRandomPoint(points);
|
||||
Player currentPlayer = getGame().getCurrentPlayer();
|
||||
currentPlayer.decrementEnergy(currentPlayer.getClassPlayer().getShootCost());
|
||||
Player targetPlayer = getGame().getGrid().getBoard().get(point).getA();
|
||||
targetPlayer.decrementEnergy(currentPlayer.getClassPlayer().getPenaltyShoot());
|
||||
player.decrementEnergy(player.getClassPlayer().getShootCost());
|
||||
game.getGrid().getBoard().get(choseRandomPoint(getValidPoint())).getA()
|
||||
.decrementEnergy(player.getClassPlayer().getPenaltyShoot());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -29,11 +31,15 @@ public class Shot extends AbstractAction {
|
||||
return !getValidPoint().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated voir {@link Shot#doAction()}, surement renommé en isValidPoint(Point): bool après rework
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "07/11/2021")
|
||||
@Override
|
||||
public List<Point> getValidPoint() {
|
||||
List<Point> listMoves = new ArrayList<>();
|
||||
Point position = getGame().getCurrentPlayer().getPoint();
|
||||
Weapon weapon = getGame().getCurrentPlayer().getWeapon();
|
||||
Point position = game.getCurrentPlayer().getPoint();
|
||||
Weapon weapon = game.getCurrentPlayer().getWeapon();
|
||||
for (int delta = -1; delta <= 1; delta++) {
|
||||
if(delta != 0){
|
||||
Point verticalNeibourg = seeNeibourg(position,delta,weapon.getVerticalDistance(),true);
|
||||
@ -49,21 +55,22 @@ public class Shot extends AbstractAction {
|
||||
return listMoves;
|
||||
}
|
||||
|
||||
@Deprecated(since = "07/11/2021", forRemoval = true)
|
||||
public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) {
|
||||
Point neibourg = null;
|
||||
if (isVertical) {
|
||||
if (getGame().getGrid().boardVerticalIsValid(point.getA(), delta)) {
|
||||
if (game.getGrid().boardVerticalIsValid(point.getA(), delta)) {
|
||||
neibourg = new Point(point.getA() + delta, point.getB());
|
||||
}
|
||||
} else {
|
||||
if (getGame().getGrid().boardHorizontalIsValid(point.getB(), delta)) {
|
||||
if (game.getGrid().boardHorizontalIsValid(point.getB(), delta)) {
|
||||
neibourg = new Point(point.getA(), point.getB() + delta);
|
||||
}
|
||||
}
|
||||
if (getGame().getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) {
|
||||
if (game.getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) {
|
||||
return null;
|
||||
}
|
||||
if(getGame().getGrid().getBoard().get(neibourg).getA() instanceof Player){
|
||||
if(game.getGrid().getBoard().get(neibourg).getA() instanceof Player){
|
||||
return neibourg;
|
||||
}
|
||||
return seeNeibourg(neibourg,delta,range - 1,isVertical);
|
||||
|
@ -56,13 +56,13 @@ public class GridTest {
|
||||
actions.add(new Move(game, player, direction));
|
||||
} catch (NotValidDirectionException ignored){}
|
||||
}
|
||||
actions.addAll(Arrays.asList(new Nothing(game, player), new Shot(game, player),
|
||||
new DeployShield(game, player), new DropBomb(game, player), new DropMine(game, player)));
|
||||
actions.addAll(Arrays.asList(new Nothing(), new Shot(game, player),
|
||||
new DeployShield(player), new DropBomb(game, player), new DropMine(game, player)));
|
||||
player.setActions(actions);
|
||||
System.out.println(game.getGrid().toString());
|
||||
Action action = null;
|
||||
switch (player.getActions().size()){
|
||||
case 0 -> action = new Nothing(game, player);
|
||||
case 0 -> action = new Nothing();
|
||||
case 1 -> action = game.getCurrentPlayer().getActions().get(0);
|
||||
default -> {
|
||||
Random random = new Random();
|
||||
|
Loading…
Reference in New Issue
Block a user