move playersList from App to ViewManager

This commit is contained in:
Quentin Legot 2021-12-09 14:18:44 +01:00
parent 5beaeacc1e
commit 4098e01630
3 changed files with 59 additions and 15 deletions

View File

@ -16,10 +16,12 @@ import javafx.stage.Stage;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.*; import java.util.*;
/**
* Application starting point
*/
public class App extends Application { public class App extends Application {
private static LinkedList<String> argsList; private static LinkedList<String> argsList;
public static HashMap<Player, ClientPlayer> playerList = new HashMap<>();
private static Game game; private static Game game;
private static ViewManager viewManager; private static ViewManager viewManager;
@ -48,36 +50,33 @@ public class App extends Application {
} }
} }
public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, public static void startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> players = parsePlayers();
GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).gridDimensions(12, 12); GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).gridDimensions(12, 12);
game = new Game(builder, players); game = new Game(builder, players);
for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
}
} }
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
try { try {
startGame(player -> new Window(stage, game, player)); startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
viewManager = new ViewManager(playerList, game, Window.class); viewManager = new ViewManager(game, Window.class, player -> new Window(stage, game, player));
viewManager.run(); viewManager.run();
} }
public static void launchTerminal() { public static void launchTerminal() {
try { try {
startGame(player -> new Terminal(game, player)); startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException e) { | IllegalAccessException e) {
throw new CrashException(e.getMessage(), e); throw new CrashException(e.getMessage(), e);
} }
viewManager = new ViewManager(playerList, game, Terminal.class); viewManager = new ViewManager(game, Terminal.class, player -> new Terminal(game, player));
viewManager.run(); viewManager.run();
} }

View File

@ -1,17 +1,29 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.client.ClientPlayer; import fr.lnl.game.client.ClientPlayer;
import fr.lnl.game.client.ViewLambda;
import fr.lnl.game.client.listener.DisplayWinnerEvent; import fr.lnl.game.client.listener.DisplayWinnerEvent;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.HumanPlayer; 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 java.util.HashMap; import java.util.HashMap;
import java.util.Objects;
public record ViewManager( public final class ViewManager {
HashMap<Player, ClientPlayer> players, private final Game game;
Game game, Class<? extends View> viewType) { private final Class<? extends View> viewType;
public ViewManager(Game game, Class<? extends View> viewType, ViewLambda lambda) {
this.game = game;
this.viewType = viewType;
for (Player player : game.getPlayers()) {
players.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
}
}
public HashMap<Player, ClientPlayer> players = new HashMap<>();
public void updateView() { public void updateView() {
players.get(game.getCurrentPlayer()).getView().show(); players.get(game.getCurrentPlayer()).getView().show();
@ -22,13 +34,13 @@ public record ViewManager(
while (true) { while (true) {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
System.out.println("\n\033[0;34m====== Tour n°" + game.getNbrTurn() + " =======\033[0m"); System.out.println("\n\033[0;34m====== Tour n°" + game.getNbrTurn() + " =======\033[0m");
System.out.println("\nA \033[0;31m" + player + " " + player.getId() + "\033[0m de jouer"); System.out.println("\nA \033[0;31m" + player + " " + player.getId() + "\033[0m de jouer");
players.get(game.getCurrentPlayer()).getView().show(); players.get(game.getCurrentPlayer()).getView().show();
if(game.getCurrentPlayer() instanceof HumanPlayer human) { if (game.getCurrentPlayer() instanceof HumanPlayer human) {
game.setSelectedAction(((Terminal) players.get(human).getView()).choseAction()); game.setSelectedAction(((Terminal) players.get(human).getView()).choseAction());
} }
boolean isOver = game.play(); boolean isOver = game.play();
System.out.println("\n\033[0;31m" + player + " " + player.getId() + "\033[0m utilise l'action \033[0;36m"+ System.out.println("\n\033[0;31m" + player + " " + player.getId() + "\033[0m utilise l'action \033[0;36m" +
game.getSelectedAction().getClass().getSimpleName() + "\033[0m"); game.getSelectedAction().getClass().getSimpleName() + "\033[0m");
if (isOver) { if (isOver) {
displayWinnerEvent.updateModel(game.getWinner()); displayWinnerEvent.updateModel(game.getWinner());
@ -48,4 +60,34 @@ public record ViewManager(
updateView(); updateView();
} }
} }
public Game game() {
return game;
}
public Class<? extends View> viewType() {
return viewType;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (ViewManager) obj;
return Objects.equals(this.game, that.game) &&
Objects.equals(this.viewType, that.viewType);
}
@Override
public int hashCode() {
return Objects.hash(game, viewType);
}
@Override
public String toString() {
return "ViewManager[" +
"game=" + game + ", " +
"viewType=" + viewType + ']';
}
} }

View File

@ -1,3 +1,6 @@
/**
* Client module, include every view and controller classes from MVC model
*/
module client { module client {
requires javafx.controls; requires javafx.controls;
requires transitive javafx.graphics; requires transitive javafx.graphics;