move playersList from App to ViewManager
This commit is contained in:
parent
5beaeacc1e
commit
4098e01630
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
@ -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 + ']';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user