From 7dde04303ca195188123d60ed4e6b1b1b3b774ca Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sat, 4 Dec 2021 16:13:22 +0100 Subject: [PATCH] Move images, optimize imports, move GUI instructions to Window.java --- .../src/main/java/fr/lnl/game/client/App.java | 14 ++- .../game/client/listener/ButtonListener.java | 3 +- .../java/fr/lnl/game/client/view/Cell.java | 34 +++-- .../java/fr/lnl/game/client/view/GUI.java | 116 ------------------ .../java/fr/lnl/game/client/view/Main.java | 16 --- .../java/fr/lnl/game/client/view/Window.java | 85 ++++++++++--- .../src/main/resources/{images => }/bomb.jpg | Bin .../resources/{images => }/bottomLeftWall.PNG | Bin .../{images => }/bottomRightWall.PNG | Bin .../resources/{images => }/bottomWall.PNG | Bin .../resources/{images => }/energyBall.png | Bin .../src/main/resources/{images => }/mine.webp | Bin .../main/resources/{images => }/player.png | Bin .../main/resources/{images => }/rightWall.PNG | Bin .../resources/{images => }/topLeftWall.PNG | Bin .../resources/{images => }/topRightWall.PNG | Bin .../main/resources/{images => }/topWall.PNG | Bin .../src/main/resources/{images => }/wall.jpg | Bin .../java/fr/lnl/game/server/games/Game.java | 2 - server/src/main/java/module-info.java | 1 + .../fr/lnl/game/server/ActionPlayerTest.java | 2 +- .../java/fr/lnl/game/server/GridTest.java | 2 +- .../test/java/fr/lnl/game/server/Mock.java | 2 +- 23 files changed, 101 insertions(+), 176 deletions(-) delete mode 100644 client/src/main/java/fr/lnl/game/client/view/GUI.java delete mode 100644 client/src/main/java/fr/lnl/game/client/view/Main.java rename client/src/main/resources/{images => }/bomb.jpg (100%) rename client/src/main/resources/{images => }/bottomLeftWall.PNG (100%) rename client/src/main/resources/{images => }/bottomRightWall.PNG (100%) rename client/src/main/resources/{images => }/bottomWall.PNG (100%) rename client/src/main/resources/{images => }/energyBall.png (100%) rename client/src/main/resources/{images => }/mine.webp (100%) rename client/src/main/resources/{images => }/player.png (100%) rename client/src/main/resources/{images => }/rightWall.PNG (100%) rename client/src/main/resources/{images => }/topLeftWall.PNG (100%) rename client/src/main/resources/{images => }/topRightWall.PNG (100%) rename client/src/main/resources/{images => }/topWall.PNG (100%) rename client/src/main/resources/{images => }/wall.jpg (100%) 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 f5a07c2..8565f25 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,15 @@ package fr.lnl.game.client; -import fr.lnl.game.client.view.*; -import fr.lnl.game.server.listener.GameFinishEvent; + +import fr.lnl.game.client.view.AbstractView; +import fr.lnl.game.client.view.Terminal; +import fr.lnl.game.client.view.ViewManager; +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.grid.build.BuildStrategy; +import fr.lnl.game.server.games.grid.build.LockStrategy; import fr.lnl.game.server.games.player.*; +import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.utils.CrashException; import fr.lnl.game.server.utils.Point; import javafx.application.Application; @@ -38,7 +44,9 @@ public class App extends Application { public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); - game = new Game(new Grid(12, 12, players, 0.80F,0.95F), players, new GameFinishEvent()); + Grid grid = new Grid(12, 12, players); + BuildStrategy buildStrategy = new LockStrategy(grid, 0.80F, 0.95F); + game = new Game(buildStrategy, players, new GameFinishEvent()); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } 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 e557008..860c1f3 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 @@ -6,7 +6,6 @@ import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.ModelListener; import javafx.scene.control.Alert; -import javafx.scene.control.Dialog; public class ButtonListener extends AbstractModelListening { @@ -32,11 +31,11 @@ public class ButtonListener extends AbstractModelListening { public void updateModel(Object event) { Player player = game.getCurrentPlayer(); game.play(); - App.viewManager.updateView(); Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Un joueur ordinateur a joué"); alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué"); alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); + App.viewManager.updateView(); alert.showAndWait(); } } diff --git a/client/src/main/java/fr/lnl/game/client/view/Cell.java b/client/src/main/java/fr/lnl/game/client/view/Cell.java index fe1f011..623bfe0 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Cell.java +++ b/client/src/main/java/fr/lnl/game/client/view/Cell.java @@ -18,11 +18,10 @@ import javafx.scene.shape.Rectangle; public class Cell extends Rectangle { - public Cell(int x, int y){ - setWidth(GUI.cellSize); - setHeight(GUI.cellSize); - relocate(x*GUI.cellSize,y*GUI.cellSize); + setWidth(Window.cellSize); + setHeight(Window.cellSize); + relocate(x*Window.cellSize,y*Window.cellSize); setFill(Color.valueOf("#ffffff")); setStroke(Color.DARKGRAY); } @@ -30,26 +29,21 @@ public class Cell extends Rectangle { public static StackPane setImageObject(Object object){ - Image image; StackPane sp = new StackPane(); - + String in; if(object instanceof Player){ - image = new Image("/images/player.png"); - } - else if(object instanceof EnergyBall){ - image = new Image("/images/energyBall.png"); - } - else if(object instanceof Bomb){ - image = new Image("/images/bomb.jpg"); - } - else if(object instanceof Mine){ - image = new Image("/images/mine.webp"); - } - else{ - image = new Image("/images/wall.jpg"); + in = "player.png"; + } else if(object instanceof EnergyBall){ + in = "energyBall.png"; + } else if(object instanceof Bomb){ + in = "bomb.jpg"; + } else if(object instanceof Mine){ + in = "mine.webp"; + } else{ + in = "wall.jpg"; } - ImageView iv = new ImageView(image); + ImageView iv = new ImageView(new Image(in)); iv.setFitHeight(40); iv.setFitWidth(40); sp.getChildren().add(iv); diff --git a/client/src/main/java/fr/lnl/game/client/view/GUI.java b/client/src/main/java/fr/lnl/game/client/view/GUI.java deleted file mode 100644 index 24b70cd..0000000 --- a/client/src/main/java/fr/lnl/game/client/view/GUI.java +++ /dev/null @@ -1,116 +0,0 @@ -package fr.lnl.game.client.view; - - -import fr.lnl.game.server.games.grid.*; -import fr.lnl.game.server.games.grid.elements.*; -import fr.lnl.game.server.games.player.ClassPlayer; -import fr.lnl.game.server.games.player.Player; -import fr.lnl.game.server.games.player.RandomComputerPlayer; -import fr.lnl.game.server.utils.Pair; -import fr.lnl.game.server.utils.Point; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.layout.Pane; -import javafx.scene.layout.StackPane; -import javafx.scene.shape.Rectangle; -import javafx.stage.Modality; -import javafx.stage.Stage; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; - -public class GUI { - - //à revoir pour respecter MVC - HashMap> board; - Stage stage; - Scene scene; - Grid grid; - - - public static final int cellSize = 40; - public static final int width = 24; - public static final int height = 16; - - public GUI() { - stage = new Stage(); - stage.initModality(Modality.APPLICATION_MODAL); - //à enlever avec sa méthode car ne respecte pas mvc - grid = getGrid(); - - scene = new Scene(createContent()); - - stage.setScene(scene); - stage.setTitle("Game"); - stage.setResizable(false); - stage.show(); - } - - private Parent createContent() { - Pane principalPane = new Pane(); - principalPane.setPrefSize(width * cellSize, height * cellSize); - - //à définir avec n pour moduler la taille du plateau - for (int i = 0; i < 16; i++) { - for (int j = 0; j < 16; j++) { - Cell cell = new Cell(i, j); - principalPane.getChildren().add(cell); - } - } - - board = grid.getBoard(); - for (int i = 0; i < 16; i++) { - for (int j = 0; j < 16; j++) { - Pair value = board.get(new Point(i, j)); - if (value.getA() != null) { - addToPrincipalPanel(value.getA(), principalPane, i, j); - } - if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Mine || value.getB() instanceof Bomb) { - System.out.println(value.getB()); - addToPrincipalPanel(value.getB(), principalPane, i, j); - } - } - } - - Rectangle shape = new Rectangle(); - shape.setX(700); - shape.setY(20); - shape.setWidth(200); - shape.setHeight(600); - shape.setFill(javafx.scene.paint.Color.WHITE); - - - Button followingButton = new Button("SUIVANT"); - followingButton.setLayoutX(775); - followingButton.setLayoutY(600); - followingButton.setStyle("-fx-background-color: #a96806;"); - followingButton.setTextFill(javafx.scene.paint.Color.WHITE); - //add un eventListener au button - - principalPane.getChildren().add(followingButton); - //pas compris le principe - return principalPane; - } - - public Pane addToPrincipalPanel(Object object, Pane principalPane, int i, int j) { - StackPane sp = Cell.setImageObject(object); - sp.setLayoutY(i * cellSize); - sp.setLayoutX(j * cellSize); - principalPane.getChildren().add(sp); - return principalPane; - } - - - //méthode à supprimer existe juste pour test - private Grid getGrid() { - List players = Arrays.asList(new RandomComputerPlayer(1, null, ClassPlayer.DEFAULT), - new RandomComputerPlayer(2, null, ClassPlayer.DEFAULT)); - Grid grid = new Grid(16, 16, players, 0.80F, 0.95F); - //grid.placePlayersBRUT(); - return grid; - } - - -} diff --git a/client/src/main/java/fr/lnl/game/client/view/Main.java b/client/src/main/java/fr/lnl/game/client/view/Main.java deleted file mode 100644 index daa8ef5..0000000 --- a/client/src/main/java/fr/lnl/game/client/view/Main.java +++ /dev/null @@ -1,16 +0,0 @@ -package fr.lnl.game.client.view; - -import javafx.application.Application; -import javafx.stage.Stage; - -public class Main extends Application{ - - public static void main(String[] args) { - launch(args); - } - - @Override - public void start(Stage primaryStage) throws Exception { - new GUI(); - } -} 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 65b7a05..011d30b 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 @@ -2,37 +2,94 @@ 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.grid.Grid; +import fr.lnl.game.server.games.grid.elements.*; import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Pair; +import fr.lnl.game.server.utils.Point; +import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.layout.GridPane; +import javafx.scene.layout.Pane; +import javafx.scene.layout.StackPane; +import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Window extends AbstractView { + public static final int cellSize = 40; + public static final int width = 24; + public static final int height = 16; + 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() + ", my remaining energy is " + player.getEnergy()); - Button b = new Button("Click to continue the game"); - b.setOnAction(new ClientEventHandler(new ButtonListener(game))); - 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.initModality(Modality.APPLICATION_MODAL); + Scene scene = new Scene(createContent()); stage.setScene(scene); + stage.setTitle("Game"); + stage.setResizable(false); stage.sizeToScene(); stage.show(); } + + private Parent createContent() { + Pane principalPane = new Pane(); + principalPane.setPrefSize(width * cellSize, height * cellSize); // TODO: 04/12/2021 A corriger -> doit plutôt s'adapter à la taille de la grid (grid.getRow() et grid.getColumn()) + + //à définir avec n pour moduler la taille du plateau + for (int i = 0; i < game.getGrid().getRow(); i++) { + for (int j = 0; j < game.getGrid().getColumn(); j++) { + Cell cell = new Cell(i, j); + principalPane.getChildren().add(cell); + } + } + + Grid grid = game.getGrid(); + for (int i = 0; i < grid.getRow(); i++) { + for (int j = 0; j < grid.getColumn(); j++) { + Pair value = grid.getBoard().get(new Point(i, j)); + if (value.getA() != null) { + addToPrincipalPanel(value.getA(), principalPane, i, j); + } + if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Mine || value.getB() instanceof Bomb) { + addToPrincipalPanel(value.getB(), principalPane, i, j); + } + } + } + + Rectangle shape = new Rectangle(); + shape.setX(700); + shape.setY(20); + shape.setWidth(200); + shape.setHeight(600); + shape.setFill(javafx.scene.paint.Color.WHITE); + + + Button followingButton = new Button("SUIVANT"); + followingButton.setOnAction(new ClientEventHandler(new ButtonListener(game))); + followingButton.setLayoutX(775); + followingButton.setLayoutY(600); + followingButton.setStyle("-fx-background-color: #a96806;"); + followingButton.setTextFill(javafx.scene.paint.Color.WHITE); + //add un eventListener au button + + principalPane.getChildren().add(followingButton); + //pas compris le principe + return principalPane; + } + + public Pane addToPrincipalPanel(Object object, Pane principalPane, int i, int j) { + StackPane sp = Cell.setImageObject(object); + sp.setLayoutY(i * cellSize); + sp.setLayoutX(j * cellSize); + principalPane.getChildren().add(sp); + return principalPane; + } } diff --git a/client/src/main/resources/images/bomb.jpg b/client/src/main/resources/bomb.jpg similarity index 100% rename from client/src/main/resources/images/bomb.jpg rename to client/src/main/resources/bomb.jpg diff --git a/client/src/main/resources/images/bottomLeftWall.PNG b/client/src/main/resources/bottomLeftWall.PNG similarity index 100% rename from client/src/main/resources/images/bottomLeftWall.PNG rename to client/src/main/resources/bottomLeftWall.PNG diff --git a/client/src/main/resources/images/bottomRightWall.PNG b/client/src/main/resources/bottomRightWall.PNG similarity index 100% rename from client/src/main/resources/images/bottomRightWall.PNG rename to client/src/main/resources/bottomRightWall.PNG diff --git a/client/src/main/resources/images/bottomWall.PNG b/client/src/main/resources/bottomWall.PNG similarity index 100% rename from client/src/main/resources/images/bottomWall.PNG rename to client/src/main/resources/bottomWall.PNG diff --git a/client/src/main/resources/images/energyBall.png b/client/src/main/resources/energyBall.png similarity index 100% rename from client/src/main/resources/images/energyBall.png rename to client/src/main/resources/energyBall.png diff --git a/client/src/main/resources/images/mine.webp b/client/src/main/resources/mine.webp similarity index 100% rename from client/src/main/resources/images/mine.webp rename to client/src/main/resources/mine.webp diff --git a/client/src/main/resources/images/player.png b/client/src/main/resources/player.png similarity index 100% rename from client/src/main/resources/images/player.png rename to client/src/main/resources/player.png diff --git a/client/src/main/resources/images/rightWall.PNG b/client/src/main/resources/rightWall.PNG similarity index 100% rename from client/src/main/resources/images/rightWall.PNG rename to client/src/main/resources/rightWall.PNG diff --git a/client/src/main/resources/images/topLeftWall.PNG b/client/src/main/resources/topLeftWall.PNG similarity index 100% rename from client/src/main/resources/images/topLeftWall.PNG rename to client/src/main/resources/topLeftWall.PNG diff --git a/client/src/main/resources/images/topRightWall.PNG b/client/src/main/resources/topRightWall.PNG similarity index 100% rename from client/src/main/resources/images/topRightWall.PNG rename to client/src/main/resources/topRightWall.PNG diff --git a/client/src/main/resources/images/topWall.PNG b/client/src/main/resources/topWall.PNG similarity index 100% rename from client/src/main/resources/images/topWall.PNG rename to client/src/main/resources/topWall.PNG diff --git a/client/src/main/resources/images/wall.jpg b/client/src/main/resources/wall.jpg similarity index 100% rename from client/src/main/resources/images/wall.jpg rename to client/src/main/resources/wall.jpg 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 f96d84f..42b34b6 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 @@ -4,11 +4,9 @@ import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.player.ComputerPlayer; -import fr.lnl.game.server.games.player.HumanPlayer; import fr.lnl.game.server.games.player.Player; 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; diff --git a/server/src/main/java/module-info.java b/server/src/main/java/module-info.java index c856548..1747951 100644 --- a/server/src/main/java/module-info.java +++ b/server/src/main/java/module-info.java @@ -8,4 +8,5 @@ module server { exports fr.lnl.game.server.utils; exports fr.lnl.game.server.listener; exports fr.lnl.game.server.games.grid.elements; + exports fr.lnl.game.server.games.grid.build; } \ No newline at end of file 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 0a2cb38..c0638bc 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -2,8 +2,8 @@ package fr.lnl.game.server; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.action.*; -import fr.lnl.game.server.games.grid.elements.Bomb; import fr.lnl.game.server.games.grid.Grid; +import fr.lnl.game.server.games.grid.elements.Bomb; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; import org.junit.jupiter.api.Assertions; diff --git a/server/src/test/java/fr/lnl/game/server/GridTest.java b/server/src/test/java/fr/lnl/game/server/GridTest.java index 65ddd63..f8739fe 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -1,9 +1,9 @@ package fr.lnl.game.server; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.elements.Box; import fr.lnl.game.server.games.grid.elements.EnergyBall; -import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.elements.Wall; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Pair; 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 96b445c..5bf0c0c 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -1,10 +1,10 @@ package fr.lnl.game.server; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.build.LockStrategy; import fr.lnl.game.server.games.grid.elements.EnergyBall; -import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.elements.Wall; import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.Player;