Move images, optimize imports, move GUI instructions to Window.java

This commit is contained in:
Quentin Legot 2021-12-04 16:13:22 +01:00
parent aea8ac654d
commit 7dde04303c
23 changed files with 101 additions and 176 deletions

View File

@ -1,9 +1,15 @@
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.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.Game;
import fr.lnl.game.server.games.grid.Grid; 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.games.player.*;
import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.server.utils.CrashException; import fr.lnl.game.server.utils.CrashException;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import javafx.application.Application; import javafx.application.Application;
@ -38,7 +44,9 @@ public class App extends Application {
public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> 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()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
} }

View File

@ -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.AbstractModelListening;
import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.server.listener.ModelListener;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.Dialog;
public class ButtonListener extends AbstractModelListening { public class ButtonListener extends AbstractModelListening {
@ -32,11 +31,11 @@ public class ButtonListener extends AbstractModelListening {
public void updateModel(Object event) { public void updateModel(Object event) {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
game.play(); game.play();
App.viewManager.updateView();
Alert alert = new Alert(Alert.AlertType.INFORMATION); Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Un joueur ordinateur a joué"); alert.setTitle("Un joueur ordinateur a joué");
alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué"); alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué");
alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); alert.setContentText("Il a joué l'action: " + game.getSelectedAction());
App.viewManager.updateView();
alert.showAndWait(); alert.showAndWait();
} }
} }

View File

@ -18,11 +18,10 @@ import javafx.scene.shape.Rectangle;
public class Cell extends Rectangle { public class Cell extends Rectangle {
public Cell(int x, int y){ public Cell(int x, int y){
setWidth(GUI.cellSize); setWidth(Window.cellSize);
setHeight(GUI.cellSize); setHeight(Window.cellSize);
relocate(x*GUI.cellSize,y*GUI.cellSize); relocate(x*Window.cellSize,y*Window.cellSize);
setFill(Color.valueOf("#ffffff")); setFill(Color.valueOf("#ffffff"));
setStroke(Color.DARKGRAY); setStroke(Color.DARKGRAY);
} }
@ -30,26 +29,21 @@ public class Cell extends Rectangle {
public static StackPane setImageObject(Object object){ public static StackPane setImageObject(Object object){
Image image;
StackPane sp = new StackPane(); StackPane sp = new StackPane();
String in;
if(object instanceof Player){ if(object instanceof Player){
image = new Image("/images/player.png"); in = "player.png";
} } else if(object instanceof EnergyBall){
else if(object instanceof EnergyBall){ in = "energyBall.png";
image = new Image("/images/energyBall.png"); } else if(object instanceof Bomb){
} in = "bomb.jpg";
else if(object instanceof Bomb){ } else if(object instanceof Mine){
image = new Image("/images/bomb.jpg"); in = "mine.webp";
} } else{
else if(object instanceof Mine){ in = "wall.jpg";
image = new Image("/images/mine.webp");
}
else{
image = new Image("/images/wall.jpg");
} }
ImageView iv = new ImageView(image); ImageView iv = new ImageView(new Image(in));
iv.setFitHeight(40); iv.setFitHeight(40);
iv.setFitWidth(40); iv.setFitWidth(40);
sp.getChildren().add(iv); sp.getChildren().add(iv);

View File

@ -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<Point, Pair<Player, Box>> 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<Player, Box> 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<Player> 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;
}
}

View File

@ -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();
}
}

View File

@ -2,37 +2,94 @@ 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.grid.Grid;
import fr.lnl.game.server.games.grid.elements.*;
import fr.lnl.game.server.games.player.Player; 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.Scene;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.layout.Pane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage; import javafx.stage.Stage;
public class Window extends AbstractView { 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; private final Stage stage;
public Window(Stage stage, Game game, Player player) { public Window(Stage stage, Game game, Player player) {
super(game, player); super(game, player);
this.stage = stage; this.stage = stage;
} }
public void show() { public void show() {
String javaVersion = System.getProperty("java.version"); // stage.initModality(Modality.APPLICATION_MODAL);
String javafxVersion = System.getProperty("javafx.version"); Scene scene = new Scene(createContent());
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.setScene(scene); stage.setScene(scene);
stage.setTitle("Game");
stage.setResizable(false);
stage.sizeToScene(); stage.sizeToScene();
stage.show(); 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<Player, Box> 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;
}
} }

View File

Before

Width:  |  Height:  |  Size: 69 KiB

After

Width:  |  Height:  |  Size: 69 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 6.0 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

Before

Width:  |  Height:  |  Size: 6.0 KiB

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 56 KiB

View File

@ -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.Grid;
import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.build.BuildStrategy;
import fr.lnl.game.server.games.player.ComputerPlayer; 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.games.player.Player;
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.CrashException;
import fr.lnl.game.server.utils.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;

View File

@ -8,4 +8,5 @@ module server {
exports fr.lnl.game.server.utils; exports fr.lnl.game.server.utils;
exports fr.lnl.game.server.listener; exports fr.lnl.game.server.listener;
exports fr.lnl.game.server.games.grid.elements; exports fr.lnl.game.server.games.grid.elements;
exports fr.lnl.game.server.games.grid.build;
} }

View File

@ -2,8 +2,8 @@ package fr.lnl.game.server;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.*; 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.Grid;
import fr.lnl.game.server.games.grid.elements.Bomb;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;

View File

@ -1,9 +1,9 @@
package fr.lnl.game.server; package fr.lnl.game.server;
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.elements.Box; 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.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.grid.elements.Wall;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Pair;

View File

@ -1,10 +1,10 @@
package fr.lnl.game.server; package fr.lnl.game.server;
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.build.BuildStrategy; 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.build.LockStrategy;
import fr.lnl.game.server.games.grid.elements.EnergyBall; 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.grid.elements.Wall;
import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;