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

View File

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

View File

@ -7,6 +7,6 @@ public class UpdateViewEvent extends AbstractModelListening {
@Override
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.server.games.Game;
import fr.lnl.game.server.games.player.Player;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Window extends AbstractView {
@ -31,7 +34,7 @@ public class Window extends AbstractView {
grid.add(b, 0, 2);
Scene scene = new Scene(grid, 640, 480);
stage.setScene(scene);
stage.sizeToScene();
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.listener.AwakeGame;
import fr.lnl.game.server.listener.ModelListener;
import fr.lnl.game.server.utils.CrashException;
import fr.lnl.game.server.utils.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
public class Game {
@ -44,9 +46,9 @@ public class Game {
@Deprecated
public void placePlayersBRUT(){
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.getPlayers().get(1).setPoint(new Point(7, 8));
grid.getPlayers().get(1).setPosition(new Point(7, 8));
}
public void play() {
@ -61,13 +63,13 @@ public class Game {
ComputerPlayer player = (ComputerPlayer) currentPlayer;
Action action = player.choseAction();
action.doAction();
waitNSeconds(2);
}
selectedAction = null;
nextCurrentPlayer();
viewUpdateEvent.updateModel(null);
gameFinishEvent.updateModel(null);
}
gameFinishEvent.updateModel(null);
}
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() {
synchronized (lock) {
lock.notifyAll();

View File

@ -17,7 +17,7 @@ public abstract class DropObject extends AbstractAction {
public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException {
super(game, player);
List<Point> points = getValidPoint();
Point playerPosition = player.getPoint();
Point playerPosition = player.getPosition();
Point dropDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(dropDirection)) {
throw new NotValidDirectionException(direction + " isn't a valid position");
@ -33,7 +33,7 @@ public abstract class DropObject extends AbstractAction {
public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>();
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 column = -1; column <= 1; 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.HashMap;
import java.util.HashSet;
import java.util.List;
public class Move extends AbstractAction {
@ -20,7 +19,7 @@ public class Move extends AbstractAction {
public Move(Game game, Player player, Direction direction) throws NotValidDirectionException {
super(game, player);
List<Point> points = getValidPoint();
Point playerPosition = player.getPoint();
Point playerPosition = player.getPosition();
Point newPosition = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(newPosition)) {
throw new NotValidDirectionException(direction + " isn't a valid position");
@ -30,9 +29,9 @@ public class Move extends AbstractAction {
@Override
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);
player.setPoint(this.point);
player.setPosition(this.point);
player.decrementEnergy(player.getClassPlayer().getMoveCost());
Box box = game.getGrid().getBoard().get(this.point).getB();
if(box instanceof InteractiveBox interactiveBox) {
@ -49,7 +48,7 @@ public class Move extends AbstractAction {
public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>();
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 deltacolumn = -1; deltacolumn <= 1; deltacolumn++) {
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 java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class Shot extends AbstractAction {
@ -21,7 +20,7 @@ public class Shot extends AbstractAction {
throw new NoMoreBulletInWeaponException();
}
List<Point> points = getValidPoint();
Point playerPosition = player.getPoint();
Point playerPosition = player.getPosition();
Point shotDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY());
if(!points.contains(shotDirection)) {
throw new NotValidDirectionException(direction + " isn't a valid position");
@ -55,7 +54,7 @@ public class Shot extends AbstractAction {
@Override
public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>();
Point position = game.getCurrentPlayer().getPoint();
Point position = game.getCurrentPlayer().getPosition();
Weapon weapon = game.getCurrentPlayer().getWeapon();
for(Direction direction : Direction.values()) {
Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(),
@ -77,7 +76,7 @@ public class Shot extends AbstractAction {
return null;
}
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;
}
}

View File

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

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Point;
public abstract class ComputerPlayer extends AbstractPlayer {
@ -7,4 +8,6 @@ public abstract class ComputerPlayer extends AbstractPlayer {
public ComputerPlayer(Integer id, Point point, ClassPlayer 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);
}
@Override
public Action choseAction() {
return null;
}
}

View File

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

View File

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

View File

@ -26,7 +26,7 @@ public class ActionPlayerTest {
@Test
public void moveActionTest() {
Action move = null;
Point oldPoint = game.getCurrentPlayer().getPoint();
Point oldPoint = game.getCurrentPlayer().getPosition();
Direction savedDirection = null;
for(Direction direction : Direction.values()) {
try {
@ -37,7 +37,7 @@ public class ActionPlayerTest {
}
Assertions.assertNotNull(move);
move.doAction();
Point newPoint = game.getCurrentPlayer().getPoint();
Point newPoint = game.getCurrentPlayer().getPosition();
Assertions.assertEquals(newPoint,
new Point(oldPoint.getA() + savedDirection.getDeltaX(),
oldPoint.getA() + savedDirection.getDeltaY()
@ -86,7 +86,7 @@ public class ActionPlayerTest {
}
Assertions.assertNotNull(action);
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);
}

View File

@ -32,9 +32,9 @@ public class Mock {
public void placePlayersBRUT(){
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.getPlayers().get(1).setPoint(new Point(7, 8));
grid.getPlayers().get(1).setPosition(new Point(7, 8));
}
public void placeEnergyBallBRUT(){