From 2ebd51f01127a0acfbd6df34d1f23c8321e87dc1 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 1 Dec 2021 23:31:51 +0100 Subject: [PATCH] Move viewUpdate to ViewManager.java, renamed getPoint() to getPosition() in Player, View still not work --- .../src/main/java/fr/lnl/game/client/App.java | 13 ++----- .../game/client/listener/ButtonListener.java | 3 +- .../game/client/listener/UpdateViewEvent.java | 2 +- .../fr/lnl/game/client/view/TimerService.java | 19 ++++++++++ .../fr/lnl/game/client/view/ViewManager.java | 38 +++++++++++++++++++ .../java/fr/lnl/game/client/view/Window.java | 5 ++- .../java/fr/lnl/game/server/games/Game.java | 20 ++++++++-- .../game/server/games/action/DropObject.java | 4 +- .../fr/lnl/game/server/games/action/Move.java | 9 ++--- .../fr/lnl/game/server/games/action/Shot.java | 7 ++-- .../server/games/player/AbstractPlayer.java | 14 +++---- .../server/games/player/ComputerPlayer.java | 3 ++ .../game/server/games/player/HumanPlayer.java | 4 -- .../lnl/game/server/games/player/Player.java | 20 ++++++++-- .../lnl/game/server/utils/CrashException.java | 3 ++ .../fr/lnl/game/server/ActionPlayerTest.java | 6 +-- .../test/java/fr/lnl/game/server/Mock.java | 4 +- 17 files changed, 127 insertions(+), 47 deletions(-) create mode 100644 client/src/main/java/fr/lnl/game/client/view/TimerService.java create mode 100644 client/src/main/java/fr/lnl/game/client/view/ViewManager.java diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index f01c2fb..7b41864 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -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 argsList; public static HashMap 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; } } diff --git a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java index cbd7d47..bfcf322 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java +++ b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java @@ -20,7 +20,6 @@ public class ButtonListener extends AbstractModelListening { @Override public void updateModel(Object event) { - App.game.nextCurrentPlayer(); - App.updateView(); + App.viewManager.setNeedUpdate(true); } } diff --git a/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java b/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java index 4ed69c0..ac6b91e 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java +++ b/client/src/main/java/fr/lnl/game/client/listener/UpdateViewEvent.java @@ -7,6 +7,6 @@ public class UpdateViewEvent extends AbstractModelListening { @Override public void updateModel(Object obj) { - App.updateView(); + App.viewManager.setNeedUpdate(true); } } diff --git a/client/src/main/java/fr/lnl/game/client/view/TimerService.java b/client/src/main/java/fr/lnl/game/client/view/TimerService.java new file mode 100644 index 0000000..936bb35 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/TimerService.java @@ -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 { + @Override + protected Task createTask() { + return new Task<>() { + protected Integer call() { + if(App.viewManager.getNeedUpdate()) { + App.viewManager.updateView(); + } + return 0; + } + }; + } +} diff --git a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java new file mode 100644 index 0000000..50d11df --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java @@ -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 players; + private final Game game; + private Boolean needUpdate = true; + + public ViewManager(HashMap 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; + } +} diff --git a/client/src/main/java/fr/lnl/game/client/view/Window.java b/client/src/main/java/fr/lnl/game/client/view/Window.java index 023d289..65bb4e0 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Window.java +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -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(); - } } diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index 0dba186..993219d 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -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(); diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java index 5c96754..6960be8 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java @@ -17,7 +17,7 @@ public abstract class DropObject extends AbstractAction { public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException { super(game, player); List 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 getValidPoint() { List listMoves = new ArrayList<>(); HashMap> 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)){ diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Move.java b/server/src/main/java/fr/lnl/game/server/games/action/Move.java index 8b01a15..596f6d1 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Move.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Move.java @@ -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 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 getValidPoint() { List listMoves = new ArrayList<>(); HashMap> 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){ diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java index ca69e49..8f12403 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java @@ -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 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 getValidPoint() { List 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; } } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java index d2d31cb..7f85b36 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java @@ -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 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 diff --git a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java index 9468333..a6fd6c5 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java @@ -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(); } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java index ab43868..2be409c 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java @@ -9,8 +9,4 @@ public class HumanPlayer extends AbstractPlayer { super(id, point,false, classPlayer); } - @Override - public Action choseAction() { - return null; - } } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/Player.java b/server/src/main/java/fr/lnl/game/server/games/player/Player.java index 37d7cf1..4eef35e 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/Player.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/Player.java @@ -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 getActions(); + void setActions(List actions); - Action choseAction(); + ClassPlayer getClassPlayer(); - void setPoint(Point point); + + void setPosition(Point position); + void decrementEnergy(int energy); + void incrementEnergy(int energy); } diff --git a/server/src/main/java/fr/lnl/game/server/utils/CrashException.java b/server/src/main/java/fr/lnl/game/server/utils/CrashException.java index f49ea1c..8db0926 100644 --- a/server/src/main/java/fr/lnl/game/server/utils/CrashException.java +++ b/server/src/main/java/fr/lnl/game/server/utils/CrashException.java @@ -4,6 +4,9 @@ public class CrashException extends RuntimeException { public CrashException(String message, Throwable cause) { super(message, cause); + System.exit(1); } + + } diff --git a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java index f9f5f98..ff50b3f 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -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); } diff --git a/server/src/test/java/fr/lnl/game/server/Mock.java b/server/src/test/java/fr/lnl/game/server/Mock.java index 1bb7581..19e13bb 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -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(){