Create a view per player + improved arguments parser
This commit is contained in:
parent
4e2da3330a
commit
ad9ac682bb
@ -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;
|
||||
}
|
||||
}
|
@ -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<String> argsList;
|
||||
public static HashMap<Player, ClientPlayer> playerList = new HashMap<>();
|
||||
public static Game game;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Class<? extends AbstractView> clazz = parseArgs(args);
|
||||
argsList = new LinkedList<>(Arrays.asList(args));
|
||||
Class<? extends AbstractView> clazz = parseView();
|
||||
if(clazz.equals(Terminal.class)) {
|
||||
try {
|
||||
launchTerminal();
|
||||
} catch (InvocationTargetException | IllegalAccessException | InstantiationException |
|
||||
NoSuchMethodException e) {
|
||||
throw new CrashException(e.getCause());
|
||||
}
|
||||
} else {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
|
||||
public static Game startGame() {
|
||||
List<Player> 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<Player> 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<? extends AbstractView> parseArgs(String[] args) {
|
||||
if(args.length >= 1) {
|
||||
if(args[0].equalsIgnoreCase("terminal")) {
|
||||
return Terminal.class;
|
||||
public static List<Player> parsePlayers()
|
||||
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
List<Player> playerList = new ArrayList<>();
|
||||
Class<? extends AbstractPlayer> 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;
|
||||
}
|
||||
}
|
||||
if(playerClass != null)
|
||||
playerList.add(createNewPlayer(playerClass,
|
||||
classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size())
|
||||
);
|
||||
return playerList;
|
||||
}
|
||||
|
||||
private static Player createNewPlayer(Class<? extends AbstractPlayer> 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<? extends AbstractView> parseView() {
|
||||
Class<? extends AbstractView> clazz;
|
||||
if(!argsList.isEmpty()) {
|
||||
if(argsList.get(0).equalsIgnoreCase("terminal")) {
|
||||
clazz = Terminal.class;
|
||||
} else {
|
||||
return Window.class;
|
||||
clazz = Window.class;
|
||||
}
|
||||
argsList.removeFirst();
|
||||
} else {
|
||||
return Window.class;
|
||||
}
|
||||
clazz = Window.class;
|
||||
}
|
||||
return clazz;
|
||||
}
|
||||
}
|
||||
|
23
client/src/main/java/fr/lnl/game/client/ClientPlayer.java
Normal file
23
client/src/main/java/fr/lnl/game/client/ClientPlayer.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package fr.lnl.game.client;
|
||||
|
||||
public class CrashException extends RuntimeException {
|
||||
|
||||
public CrashException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
@ -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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package fr.lnl.game.client;
|
||||
|
||||
public interface View {
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<ActionEvent> {
|
||||
|
||||
@Override
|
||||
public void handle(ActionEvent event) {
|
||||
App.game.nextCurrentPlayer();
|
||||
App.playerList.get(App.game.getCurrentPlayer()).getView().show();
|
||||
}
|
||||
}
|
17
client/src/main/java/fr/lnl/game/client/view/Terminal.java
Normal file
17
client/src/main/java/fr/lnl/game/client/view/Terminal.java
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
6
client/src/main/java/fr/lnl/game/client/view/View.java
Normal file
6
client/src/main/java/fr/lnl/game/client/view/View.java
Normal file
@ -0,0 +1,6 @@
|
||||
package fr.lnl.game.client.view;
|
||||
|
||||
public interface View {
|
||||
|
||||
void show();
|
||||
}
|
35
client/src/main/java/fr/lnl/game/client/view/Window.java
Normal file
35
client/src/main/java/fr/lnl/game/client/view/Window.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -3,4 +3,5 @@ module client {
|
||||
requires transitive javafx.graphics;
|
||||
requires server;
|
||||
exports fr.lnl.game.client;
|
||||
exports fr.lnl.game.client.view;
|
||||
}
|
||||
|
@ -12,14 +12,7 @@ import java.util.List;
|
||||
public class ServerMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Player> 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");
|
||||
}
|
||||
}
|
||||
|
@ -8,14 +8,14 @@ import java.util.List;
|
||||
public class Game {
|
||||
|
||||
Grid grid;
|
||||
Player current_player;
|
||||
Player currentPlayer;
|
||||
List<Player> players;
|
||||
|
||||
public Game(Grid grid, List<Player> 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() {
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user