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.util.*;
/**
* Application starting point
*/
public class App extends Application {
private static LinkedList<String> argsList;
public static HashMap<Player, ClientPlayer> playerList = new HashMap<>();
private static Game game;
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 {
List<Player> players = parsePlayers();
GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).gridDimensions(12, 12);
game = new Game(builder, players);
for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
}
}
@Override
public void start(Stage stage) {
try {
startGame(player -> new Window(stage, game, player));
startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException 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();
}
public static void launchTerminal() {
try {
startGame(player -> new Terminal(game, player));
startGame();
} catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException
| IllegalAccessException 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();
}

View File

@ -1,17 +1,29 @@
package fr.lnl.game.client.view;
import fr.lnl.game.client.ClientPlayer;
import fr.lnl.game.client.ViewLambda;
import fr.lnl.game.client.listener.DisplayWinnerEvent;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.player.HumanPlayer;
import fr.lnl.game.server.games.player.Player;
import java.util.HashMap;
import java.util.Objects;
public record ViewManager(
HashMap<Player, ClientPlayer> players,
Game game, Class<? extends View> viewType) {
public final class ViewManager {
private final Game game;
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() {
players.get(game.getCurrentPlayer()).getView().show();
@ -48,4 +60,34 @@ public record ViewManager(
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 {
requires javafx.controls;
requires transitive javafx.graphics;