diff --git a/client/src/main/java/fr/lnl/game/client/AbstractView.java b/client/src/main/java/fr/lnl/game/client/AbstractView.java deleted file mode 100644 index 2eebd5e..0000000 --- a/client/src/main/java/fr/lnl/game/client/AbstractView.java +++ /dev/null @@ -1,12 +0,0 @@ -package fr.lnl.game.client; - -import fr.lnl.game.server.games.Game; - -public abstract class AbstractView implements View { - - Game game; - - public AbstractView(Game game) { - this.game = game; - } -} 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 f8e16d1..a2880d4 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -1,56 +1,118 @@ package fr.lnl.game.client; +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.ClassPlayer; -import fr.lnl.game.server.games.player.ComputerPlayer; -import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.games.player.*; +import fr.lnl.game.server.utils.Point; import javafx.application.Application; import javafx.stage.Stage; -import java.util.Arrays; -import java.util.List; +import java.lang.reflect.InvocationTargetException; +import java.util.*; public class App extends Application { + private static LinkedList argsList; + public static HashMap playerList = new HashMap<>(); + public static Game game; + public static void main(String[] args) { - Class clazz = parseArgs(args); + argsList = new LinkedList<>(Arrays.asList(args)); + Class clazz = parseView(); if(clazz.equals(Terminal.class)) { - launchTerminal(); + try { + launchTerminal(); + } catch (InvocationTargetException | IllegalAccessException | InstantiationException | + NoSuchMethodException e) { + throw new CrashException(e.getCause()); + } } else { launch(); } } - public static Game startGame() { - List players = Arrays.asList(new ComputerPlayer(1, null, ClassPlayer.DEFAULT), - new ComputerPlayer(1, null, ClassPlayer.DEFAULT)); + public static Game startGame() + throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + List players = parsePlayers(); + System.out.println(players); return new Game(new Grid(12, 12, players), players); } @Override public void start(Stage stage) throws Exception { - Game game = startGame(); - Window window = new Window(stage, game); - // TODO: 23/10/2021 faire une view par joueur - window.show(); + game = startGame(); + for (Player player : game.getPlayers()) { + playerList.put(player, new ClientPlayer(player, new Window(stage, game, player))); + } + playerList.get(game.getCurrentPlayer()).getView().show(); } - public static void launchTerminal() { - Game game = startGame(); - Terminal terminal = new Terminal(game); - // TODO: 23/10/2021 faire une view par joueur + public static void launchTerminal() + throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + game = startGame(); + for (Player player : game.getPlayers()) { + playerList.put(player, new ClientPlayer(player, new Terminal(game, player))); + } + playerList.get(game.getCurrentPlayer()).getView().show(); } // TODO: 23/10/2021 nécessite un rework -> faire une view par joueur - public static Class parseArgs(String[] args) { - if(args.length >= 1) { - if(args[0].equalsIgnoreCase("terminal")) { - return Terminal.class; - } else { - return Window.class; + public static List parsePlayers() + throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + List playerList = new ArrayList<>(); + Class playerClass = null; + ClassPlayer classPlayer = null; + for(String str : argsList) { + switch (str) { + case "human" -> { + if(playerClass != null) { + playerList.add(createNewPlayer(playerClass, + classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) + ); + classPlayer = null; + } + playerClass = HumanPlayer.class; + } + case "computer" -> { + if(playerClass != null) { + playerList.add(createNewPlayer(playerClass, + classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) + ); + classPlayer = null; + } + playerClass = ComputerPlayer.class; + } + case "default" -> classPlayer = ClassPlayer.DEFAULT; } - } else { - return Window.class; } + if(playerClass != null) + playerList.add(createNewPlayer(playerClass, + classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) + ); + return playerList; + } + + private static Player createNewPlayer(Class playerClass, ClassPlayer playerType, + int playerListSize) throws NoSuchMethodException, InvocationTargetException, + InstantiationException, IllegalAccessException { + return playerClass.getConstructor(Integer.class, Point.class, ClassPlayer.class) + .newInstance(playerListSize, null, playerType); + } + + public static Class parseView() { + Class clazz; + if(!argsList.isEmpty()) { + if(argsList.get(0).equalsIgnoreCase("terminal")) { + clazz = Terminal.class; + } else { + clazz = Window.class; + } + argsList.removeFirst(); + } else { + clazz = Window.class; + } + return clazz; } } diff --git a/client/src/main/java/fr/lnl/game/client/ClientPlayer.java b/client/src/main/java/fr/lnl/game/client/ClientPlayer.java new file mode 100644 index 0000000..d81626c --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/ClientPlayer.java @@ -0,0 +1,23 @@ +package fr.lnl.game.client; + +import fr.lnl.game.client.view.View; +import fr.lnl.game.server.games.player.Player; + +public class ClientPlayer { + + private final Player serverPlayer; + private final View view; + + public ClientPlayer(Player serverPlayer, View view) { + this.serverPlayer = serverPlayer; + this.view = view; + } + + public Player getServerPlayer() { + return serverPlayer; + } + + public View getView() { + return view; + } +} diff --git a/client/src/main/java/fr/lnl/game/client/CrashException.java b/client/src/main/java/fr/lnl/game/client/CrashException.java new file mode 100644 index 0000000..9c6b3a8 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/CrashException.java @@ -0,0 +1,9 @@ +package fr.lnl.game.client; + +public class CrashException extends RuntimeException { + + public CrashException(Throwable cause) { + super(cause); + } + +} diff --git a/client/src/main/java/fr/lnl/game/client/Terminal.java b/client/src/main/java/fr/lnl/game/client/Terminal.java deleted file mode 100644 index 39af7e7..0000000 --- a/client/src/main/java/fr/lnl/game/client/Terminal.java +++ /dev/null @@ -1,15 +0,0 @@ -package fr.lnl.game.client; - -import fr.lnl.game.server.games.Game; - -public class Terminal extends AbstractView { - - public Terminal(Game game) { - super(game); - } - - public void displayGrid() { - - } - -} diff --git a/client/src/main/java/fr/lnl/game/client/View.java b/client/src/main/java/fr/lnl/game/client/View.java deleted file mode 100644 index c36feb7..0000000 --- a/client/src/main/java/fr/lnl/game/client/View.java +++ /dev/null @@ -1,4 +0,0 @@ -package fr.lnl.game.client; - -public interface View { -} diff --git a/client/src/main/java/fr/lnl/game/client/Window.java b/client/src/main/java/fr/lnl/game/client/Window.java deleted file mode 100644 index c3e36e4..0000000 --- a/client/src/main/java/fr/lnl/game/client/Window.java +++ /dev/null @@ -1,26 +0,0 @@ -package fr.lnl.game.client; - -import fr.lnl.game.server.games.Game; -import javafx.scene.Scene; -import javafx.scene.control.Label; -import javafx.scene.layout.StackPane; -import javafx.stage.Stage; - -public class Window extends AbstractView { - - private final Stage stage; - - public Window(Stage stage, Game game) { - super(game); - this.stage = stage; - } - - public void show() { - String javaVersion = System.getProperty("java.version"); - String javafxVersion = System.getProperty("javafx.version"); - Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); - Scene scene = new Scene(new StackPane(l), 640, 480); - stage.setScene(scene); - stage.show(); - } -} diff --git a/client/src/main/java/fr/lnl/game/client/view/AbstractView.java b/client/src/main/java/fr/lnl/game/client/view/AbstractView.java new file mode 100644 index 0000000..4ba892c --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/AbstractView.java @@ -0,0 +1,15 @@ +package fr.lnl.game.client.view; + +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.player.Player; + +public abstract class AbstractView implements View { + + protected final Player player; + protected Game game; + + public AbstractView(Game game, Player player) { + this.game = game; + this.player = player; + } +} diff --git a/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java b/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java new file mode 100644 index 0000000..cca5c80 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java @@ -0,0 +1,14 @@ +package fr.lnl.game.client.view; + +import fr.lnl.game.client.App; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; + +public class ClientEventHandler implements EventHandler { + + @Override + public void handle(ActionEvent event) { + App.game.nextCurrentPlayer(); + App.playerList.get(App.game.getCurrentPlayer()).getView().show(); + } +} diff --git a/client/src/main/java/fr/lnl/game/client/view/Terminal.java b/client/src/main/java/fr/lnl/game/client/view/Terminal.java new file mode 100644 index 0000000..53ca27a --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/Terminal.java @@ -0,0 +1,17 @@ +package fr.lnl.game.client.view; + +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.player.Player; + +public class Terminal extends AbstractView { + + public Terminal(Game game, Player player) { + super(game, player); + } + + public void show() { + // TODO: 26/10/2021 + game.getGrid().printGrid(); + } + +} diff --git a/client/src/main/java/fr/lnl/game/client/view/View.java b/client/src/main/java/fr/lnl/game/client/view/View.java new file mode 100644 index 0000000..1f4512f --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/View.java @@ -0,0 +1,6 @@ +package fr.lnl.game.client.view; + +public interface View { + + void show(); +} 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 new file mode 100644 index 0000000..f9204d0 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -0,0 +1,35 @@ +package fr.lnl.game.client.view; + +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.player.Player; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.GridPane; +import javafx.stage.Stage; + +public class Window extends AbstractView { + + private final Stage stage; + + public Window(Stage stage, Game game, Player player) { + super(game, player); + this.stage = stage; + } + + public void show() { + String javaVersion = System.getProperty("java.version"); + String javafxVersion = System.getProperty("javafx.version"); + Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); + Label l2 = new Label("I'm " + player.getClass().getSimpleName() + " num " + player.getId() + " my class is " + player.getClassPlayer().toString()); + Button b = new Button("Click to see others players informations"); + b.setOnAction(new ClientEventHandler()); + GridPane grid = new GridPane(); + grid.add(l, 0, 0); + grid.add(l2, 0, 1); + grid.add(b, 0, 2); + Scene scene = new Scene(grid, 640, 480); + stage.setScene(scene); + stage.show(); + } +} diff --git a/client/src/main/java/module-info.java b/client/src/main/java/module-info.java index a4ffc7a..6ee9003 100644 --- a/client/src/main/java/module-info.java +++ b/client/src/main/java/module-info.java @@ -3,4 +3,5 @@ module client { requires transitive javafx.graphics; requires server; exports fr.lnl.game.client; + exports fr.lnl.game.client.view; } diff --git a/server/src/main/java/fr/lnl/game/server/ServerMain.java b/server/src/main/java/fr/lnl/game/server/ServerMain.java index 60d97a9..cc8844c 100644 --- a/server/src/main/java/fr/lnl/game/server/ServerMain.java +++ b/server/src/main/java/fr/lnl/game/server/ServerMain.java @@ -12,14 +12,7 @@ import java.util.List; public class ServerMain { public static void main(String[] args) { - List players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT), - new ComputerPlayer(2,null, ClassPlayer.DEFAULT)); - Grid grid = new Grid(16,16, players); - grid.initGrid(); - grid.placePlayersBRUT(); - grid.placeEnergyBallBRUT(); - grid.placeInternWallBRUT(); - Game game = new Game(grid, players); - game.getGrid().printGrid(); + throw new UnsupportedOperationException("Server standalone launch not supported, please use tests or launch the" + + " game from the client"); } } 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 9cdf0ab..15be9c7 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 @@ -8,14 +8,14 @@ import java.util.List; public class Game { Grid grid; - Player current_player; + Player currentPlayer; List players; public Game(Grid grid, List players) { if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); this.players = players; - this.current_player = players.get(0); + this.currentPlayer = players.get(0); this.grid = grid; } @@ -32,7 +32,14 @@ public class Game { } public Player getCurrentPlayer() { - return current_player; + return currentPlayer; + } + + public void nextCurrentPlayer() { + int index = players.indexOf(currentPlayer) + 1; + if(index == players.size()) + index = 0; + currentPlayer = players.get(index); } public Grid getGrid() { 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 83e2763..1354ba2 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 @@ -14,7 +14,7 @@ public abstract class AbstractPlayer implements Player { private Action[] actions; private ClassPlayer classPlayer; - public AbstractPlayer(int id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { + public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { this.id = id; this.classPlayer = classPlayer; this.energy = classPlayer.getEnergy(); 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 33de86e..4e6b1c1 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 @@ -4,7 +4,7 @@ import fr.lnl.game.server.utils.Point; public class ComputerPlayer extends AbstractPlayer{ - public ComputerPlayer(int id, Point point, ClassPlayer classPlayer) { + public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { super(id,point,false, classPlayer); } } 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 45fa0b3..8672b1d 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 @@ -5,7 +5,7 @@ import fr.lnl.game.server.utils.Point; public class HumanPlayer extends AbstractPlayer { - public HumanPlayer(int id, Point point, ClassPlayer classPlayer) { + public HumanPlayer(Integer id, Point point, ClassPlayer classPlayer) { super(id, point,false, classPlayer); } } diff --git a/server/src/main/java/module-info.java b/server/src/main/java/module-info.java index fdbc89b..4e804c4 100644 --- a/server/src/main/java/module-info.java +++ b/server/src/main/java/module-info.java @@ -3,5 +3,7 @@ module server { exports fr.lnl.game.server.games; exports fr.lnl.game.server.games.player; exports fr.lnl.game.server.games.grid; + exports fr.lnl.game.server.games.weapon; + exports fr.lnl.game.server.games.action; exports fr.lnl.game.server.utils; } \ No newline at end of file