Move viewUpdate to ViewManager.java, renamed getPoint() to getPosition() in Player, View still not work

This commit is contained in:
Quentin Legot 2021-12-01 23:31:51 +01:00
parent 1b8f9a2b30
commit 2ebd51f011
17 changed files with 127 additions and 47 deletions

View File

@ -1,9 +1,7 @@
package fr.lnl.game.client; package fr.lnl.game.client;
import fr.lnl.game.client.view.*;
import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.client.listener.UpdateViewEvent; import fr.lnl.game.client.listener.UpdateViewEvent;
import fr.lnl.game.client.view.AbstractView;
import fr.lnl.game.client.view.Terminal;
import fr.lnl.game.client.view.Window;
import fr.lnl.game.server.games.Game; 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.*; import fr.lnl.game.server.games.player.*;
@ -20,6 +18,7 @@ public class App extends Application {
private static LinkedList<String> argsList; private static LinkedList<String> argsList;
public static HashMap<Player, ClientPlayer> playerList = new HashMap<>(); public static HashMap<Player, ClientPlayer> playerList = new HashMap<>();
public static Game game; public static Game game;
public static ViewManager viewManager;
public static void main(String[] args) { public static void main(String[] args) {
argsList = new LinkedList<>(Arrays.asList(args)); argsList = new LinkedList<>(Arrays.asList(args));
@ -46,10 +45,6 @@ public class App extends Application {
} }
} }
public static void updateView() {
App.playerList.get(App.game.getCurrentPlayer()).getView().show();
}
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
try { try {
@ -58,8 +53,8 @@ public class App extends Application {
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
viewManager = new ViewManager(playerList, game);
new Thread(() -> game.play()).start(); new Thread(() -> game.play()).start();
updateView();
} }
public static void launchTerminal() { public static void launchTerminal() {
@ -69,6 +64,7 @@ public class App extends Application {
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
viewManager = new ViewManager(playerList, game);
new Thread(() -> game.play()).start(); new Thread(() -> game.play()).start();
} }
@ -132,7 +128,6 @@ public class App extends Application {
} else { } else {
throw new IllegalArgumentException("No argument given"); throw new IllegalArgumentException("No argument given");
} }
System.out.println(clazz.getSimpleName());
return clazz; return clazz;
} }
} }

View File

@ -20,7 +20,6 @@ public class ButtonListener extends AbstractModelListening {
@Override @Override
public void updateModel(Object event) { public void updateModel(Object event) {
App.game.nextCurrentPlayer(); App.viewManager.setNeedUpdate(true);
App.updateView();
} }
} }

View File

@ -7,6 +7,6 @@ public class UpdateViewEvent extends AbstractModelListening {
@Override @Override
public void updateModel(Object obj) { public void updateModel(Object obj) {
App.updateView(); App.viewManager.setNeedUpdate(true);
} }
} }

View File

@ -0,0 +1,19 @@
package fr.lnl.game.client.view;
import fr.lnl.game.client.App;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
public class TimerService extends ScheduledService<Integer> {
@Override
protected Task<Integer> createTask() {
return new Task<>() {
protected Integer call() {
if(App.viewManager.getNeedUpdate()) {
App.viewManager.updateView();
}
return 0;
}
};
}
}

View File

@ -0,0 +1,38 @@
package fr.lnl.game.client.view;
import fr.lnl.game.client.ClientPlayer;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.Player;
import javafx.util.Duration;
import java.util.HashMap;
public class ViewManager {
private final HashMap<Player, ClientPlayer> players;
private final Game game;
private Boolean needUpdate = true;
public ViewManager(HashMap<Player, ClientPlayer> players, Game game) {
this.players = players;
this.game = game;
TimerService timer = new TimerService();
timer.setPeriod(Duration.millis(30));
updateView();
timer.start();
}
public void updateView() {
players.get(game.getCurrentPlayer()).getView().show();
setNeedUpdate(false);
}
public Boolean getNeedUpdate() {
return needUpdate;
}
public void setNeedUpdate(Boolean needUpdate) {
this.needUpdate = needUpdate;
}
}

View File

@ -3,11 +3,14 @@ package fr.lnl.game.client.view;
import fr.lnl.game.client.listener.ButtonListener; import fr.lnl.game.client.listener.ButtonListener;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Duration;
public class Window extends AbstractView { public class Window extends AbstractView {
@ -31,7 +34,7 @@ public class Window extends AbstractView {
grid.add(b, 0, 2); grid.add(b, 0, 2);
Scene scene = new Scene(grid, 640, 480); Scene scene = new Scene(grid, 640, 480);
stage.setScene(scene); stage.setScene(scene);
stage.sizeToScene();
stage.show(); stage.show();
} }
} }

View File

@ -7,11 +7,13 @@ import fr.lnl.game.server.games.player.HumanPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.listener.AwakeGame; import fr.lnl.game.server.listener.AwakeGame;
import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.server.listener.ModelListener;
import fr.lnl.game.server.utils.CrashException;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream; import java.util.stream.Stream;
public class Game { public class Game {
@ -44,9 +46,9 @@ public class Game {
@Deprecated @Deprecated
public void placePlayersBRUT(){ public void placePlayersBRUT(){
grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0)); grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0));
grid.getPlayers().get(0).setPoint(new Point(7, 7)); grid.getPlayers().get(0).setPosition(new Point(7, 7));
grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1)); grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1));
grid.getPlayers().get(1).setPoint(new Point(7, 8)); grid.getPlayers().get(1).setPosition(new Point(7, 8));
} }
public void play() { public void play() {
@ -61,13 +63,13 @@ public class Game {
ComputerPlayer player = (ComputerPlayer) currentPlayer; ComputerPlayer player = (ComputerPlayer) currentPlayer;
Action action = player.choseAction(); Action action = player.choseAction();
action.doAction(); action.doAction();
waitNSeconds(2);
} }
selectedAction = null; selectedAction = null;
nextCurrentPlayer(); nextCurrentPlayer();
viewUpdateEvent.updateModel(null); viewUpdateEvent.updateModel(null);
gameFinishEvent.updateModel(null);
} }
gameFinishEvent.updateModel(null);
} }
private void waitForInterfaceEvent() { private void waitForInterfaceEvent() {
@ -81,6 +83,16 @@ public class Game {
} }
private void waitNSeconds(int n) {
synchronized (this){
try {
wait(TimeUnit.SECONDS.toMillis(n));
} catch (InterruptedException e) {
throw new CrashException(e.getMessage(), e);
}
}
}
public void resumeThread() { public void resumeThread() {
synchronized (lock) { synchronized (lock) {
lock.notifyAll(); lock.notifyAll();

View File

@ -17,7 +17,7 @@ public abstract class DropObject extends AbstractAction {
public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException { public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException {
super(game, player); super(game, player);
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Point playerPosition = player.getPoint(); Point playerPosition = player.getPosition();
Point dropDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY()); Point dropDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(dropDirection)) { if(!points.contains(dropDirection)) {
throw new NotValidDirectionException(direction + " isn't a valid position"); throw new NotValidDirectionException(direction + " isn't a valid position");
@ -33,7 +33,7 @@ public abstract class DropObject extends AbstractAction {
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard(); HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard();
Point position = player.getPoint(); Point position = player.getPosition();
for (int row = -1; row <= 1; row++) { for (int row = -1; row <= 1; row++) {
for (int column = -1; column <= 1; column++) { for (int column = -1; column <= 1; column++) {
if(game.getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){ if(game.getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){

View File

@ -10,7 +10,6 @@ import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class Move extends AbstractAction { public class Move extends AbstractAction {
@ -20,7 +19,7 @@ public class Move extends AbstractAction {
public Move(Game game, Player player, Direction direction) throws NotValidDirectionException { public Move(Game game, Player player, Direction direction) throws NotValidDirectionException {
super(game, player); super(game, player);
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Point playerPosition = player.getPoint(); Point playerPosition = player.getPosition();
Point newPosition = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY()); Point newPosition = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(newPosition)) { if(!points.contains(newPosition)) {
throw new NotValidDirectionException(direction + " isn't a valid position"); throw new NotValidDirectionException(direction + " isn't a valid position");
@ -30,9 +29,9 @@ public class Move extends AbstractAction {
@Override @Override
public void doAction() { public void doAction() {
game.getGrid().getBoard().get(player.getPoint()).setA(null); game.getGrid().getBoard().get(player.getPosition()).setA(null);
game.getGrid().getBoard().get(this.point).setA(player); game.getGrid().getBoard().get(this.point).setA(player);
player.setPoint(this.point); player.setPosition(this.point);
player.decrementEnergy(player.getClassPlayer().getMoveCost()); player.decrementEnergy(player.getClassPlayer().getMoveCost());
Box box = game.getGrid().getBoard().get(this.point).getB(); Box box = game.getGrid().getBoard().get(this.point).getB();
if(box instanceof InteractiveBox interactiveBox) { if(box instanceof InteractiveBox interactiveBox) {
@ -49,7 +48,7 @@ public class Move extends AbstractAction {
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard(); HashMap<Point, Pair<Player, Box>> board = game.getGrid().getBoard();
Point position = player.getPoint(); Point position = player.getPosition();
for (int deltarow = -1; deltarow <= 1; deltarow++) { for (int deltarow = -1; deltarow <= 1; deltarow++) {
for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) { for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) {
if(deltarow == 0 || deltacolumn == 0){ if(deltarow == 0 || deltacolumn == 0){

View File

@ -7,7 +7,6 @@ import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class Shot extends AbstractAction { public class Shot extends AbstractAction {
@ -21,7 +20,7 @@ public class Shot extends AbstractAction {
throw new NoMoreBulletInWeaponException(); throw new NoMoreBulletInWeaponException();
} }
List<Point> points = getValidPoint(); List<Point> points = getValidPoint();
Point playerPosition = player.getPoint(); Point playerPosition = player.getPosition();
Point shotDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY()); Point shotDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(shotDirection)) { if(!points.contains(shotDirection)) {
throw new NotValidDirectionException(direction + " isn't a valid position"); throw new NotValidDirectionException(direction + " isn't a valid position");
@ -55,7 +54,7 @@ public class Shot extends AbstractAction {
@Override @Override
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
Point position = game.getCurrentPlayer().getPoint(); Point position = game.getCurrentPlayer().getPosition();
Weapon weapon = game.getCurrentPlayer().getWeapon(); Weapon weapon = game.getCurrentPlayer().getWeapon();
for(Direction direction : Direction.values()) { for(Direction direction : Direction.values()) {
Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(), Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(),
@ -77,7 +76,7 @@ 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().getPoint()); System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPosition());
return neighbour; return neighbour;
} }
} }

View File

@ -9,20 +9,20 @@ import java.util.List;
public abstract class AbstractPlayer implements Player { public abstract class AbstractPlayer implements Player {
private final int id; private final int id;
private Point point; private Point position;
private int energy; private int energy;
private Weapon weapon; private Weapon weapon;
private boolean shieldDeploy; private boolean shieldDeploy;
private List<Action> actions; private List<Action> actions;
private final ClassPlayer classPlayer; private final ClassPlayer classPlayer;
public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { public AbstractPlayer(Integer id, Point position, boolean shieldDeploy, ClassPlayer classPlayer) {
this.id = id; this.id = id;
this.classPlayer = classPlayer; this.classPlayer = classPlayer;
this.energy = classPlayer.getEnergy(); this.energy = classPlayer.getEnergy();
this.weapon = classPlayer.getWeapon(); this.weapon = classPlayer.getWeapon();
this.shieldDeploy = shieldDeploy; this.shieldDeploy = shieldDeploy;
this.point = point; this.position = position;
} }
@Override @Override
@ -81,13 +81,13 @@ public abstract class AbstractPlayer implements Player {
} }
@Override @Override
public Point getPoint() { public Point getPosition() {
return point; return position;
} }
@Override @Override
public void setPoint(Point point){ public void setPosition(Point position){
this.point = point; this.position = position;
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
public abstract class ComputerPlayer extends AbstractPlayer { public abstract class ComputerPlayer extends AbstractPlayer {
@ -7,4 +8,6 @@ public abstract class ComputerPlayer extends AbstractPlayer {
public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id, point, false, classPlayer); super(id, point, false, classPlayer);
} }
public abstract Action choseAction();
} }

View File

@ -9,8 +9,4 @@ public class HumanPlayer extends AbstractPlayer {
super(id, point,false, classPlayer); super(id, point,false, classPlayer);
} }
@Override
public Action choseAction() {
return null;
}
} }

View File

@ -7,21 +7,35 @@ import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
public interface Player { public interface Player {
Point getPoint();
Point getPosition();
boolean isAlive(); boolean isAlive();
int getId(); int getId();
int getEnergy(); int getEnergy();
Weapon getWeapon(); Weapon getWeapon();
boolean isShieldDeploy(); boolean isShieldDeploy();
void setEnergy(int energy); void setEnergy(int energy);
void setShieldDeploy(boolean shieldDeploy); void setShieldDeploy(boolean shieldDeploy);
void setWeapon(Weapon weapon); void setWeapon(Weapon weapon);
List<Action> getActions(); List<Action> getActions();
void setActions(List<Action> actions); void setActions(List<Action> actions);
Action choseAction();
ClassPlayer getClassPlayer(); ClassPlayer getClassPlayer();
void setPoint(Point point);
void setPosition(Point position);
void decrementEnergy(int energy); void decrementEnergy(int energy);
void incrementEnergy(int energy); void incrementEnergy(int energy);
} }

View File

@ -4,6 +4,9 @@ public class CrashException extends RuntimeException {
public CrashException(String message, Throwable cause) { public CrashException(String message, Throwable cause) {
super(message, cause); super(message, cause);
System.exit(1);
} }
} }

View File

@ -26,7 +26,7 @@ public class ActionPlayerTest {
@Test @Test
public void moveActionTest() { public void moveActionTest() {
Action move = null; Action move = null;
Point oldPoint = game.getCurrentPlayer().getPoint(); Point oldPoint = game.getCurrentPlayer().getPosition();
Direction savedDirection = null; Direction savedDirection = null;
for(Direction direction : Direction.values()) { for(Direction direction : Direction.values()) {
try { try {
@ -37,7 +37,7 @@ public class ActionPlayerTest {
} }
Assertions.assertNotNull(move); Assertions.assertNotNull(move);
move.doAction(); move.doAction();
Point newPoint = game.getCurrentPlayer().getPoint(); Point newPoint = game.getCurrentPlayer().getPosition();
Assertions.assertEquals(newPoint, Assertions.assertEquals(newPoint,
new Point(oldPoint.getA() + savedDirection.getDeltaX(), new Point(oldPoint.getA() + savedDirection.getDeltaX(),
oldPoint.getA() + savedDirection.getDeltaY() oldPoint.getA() + savedDirection.getDeltaY()
@ -86,7 +86,7 @@ public class ActionPlayerTest {
} }
Assertions.assertNotNull(action); Assertions.assertNotNull(action);
action.doAction(); action.doAction();
Point bombPosition = new Point(player.getPoint().getA() + savedDirection.getDeltaX(), player.getPoint().getB() + savedDirection.getDeltaY()); Point bombPosition = new Point(player.getPosition().getA() + savedDirection.getDeltaX(), player.getPosition().getB() + savedDirection.getDeltaY());
Assertions.assertTrue(game.getGrid().getBoard().get(bombPosition).getB() instanceof Bomb); Assertions.assertTrue(game.getGrid().getBoard().get(bombPosition).getB() instanceof Bomb);
} }

View File

@ -32,9 +32,9 @@ public class Mock {
public void placePlayersBRUT(){ public void placePlayersBRUT(){
grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0)); grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0));
grid.getPlayers().get(0).setPoint(new Point(7, 7)); grid.getPlayers().get(0).setPosition(new Point(7, 7));
grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1)); grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1));
grid.getPlayers().get(1).setPoint(new Point(7, 8)); grid.getPlayers().get(1).setPosition(new Point(7, 8));
} }
public void placeEnergyBallBRUT(){ public void placeEnergyBallBRUT(){