Skip to content

Commit 4671242

Browse files
Refac
1 parent ef923d5 commit 4671242

File tree

7 files changed

+78
-63
lines changed

7 files changed

+78
-63
lines changed

pacman-app-arcade-pacmanxxl/src/main/java/de/amr/pacmanfx/arcade/pacman_xxl/PacManXXL_Common_StartPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public PacManXXL_Common_StartPage(GameUI ui) {
3131
flyer.setPageLayout(0, Flyer.LayoutMode.FILL);
3232
flyer.selectPage(0);
3333

34-
menu = new PacManXXL_Common_StartPageMenu(ui.theGameContext());
34+
menu = new PacManXXL_Common_StartPageMenu(ui);
3535
// scale menu to take 90% of start page height
3636
menu.scalingProperty().bind(root.heightProperty().multiply(0.9).divide(menu.numTilesY() * TS));
3737

pacman-app-arcade-pacmanxxl/src/main/java/de/amr/pacmanfx/arcade/pacman_xxl/PacManXXL_Common_StartPageMenu.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import static de.amr.pacmanfx.arcade.pacman.ArcadePacMan_GameModel.createGhost;
3131
import static de.amr.pacmanfx.arcade.pacman.ArcadePacMan_GameModel.createPac;
3232
import static de.amr.pacmanfx.model.actors.CommonAnimationID.*;
33-
import static de.amr.pacmanfx.ui.GameUI.theUI;
3433
import static de.amr.pacmanfx.ui.input.Keyboard.nude;
3534
import static de.amr.pacmanfx.uilib.widgets.OptionMenuStyle.DEFAULT_OPTION_MENU_STYLE;
3635
import static java.util.Objects.requireNonNull;
@@ -145,8 +144,7 @@ void draw(float scaling) {
145144
ctx.restore();
146145
}
147146

148-
void setGameVariant(String gameVariant) {
149-
final GameUI_Config config = theUI().config(gameVariant);
147+
void setGameConfig(GameUI_Config config) {
150148
renderer = config.createGameRenderer(ctx.getCanvas());
151149
pac.setAnimations(config.createPacAnimations(pac));
152150
pac.playAnimation(ANIM_PAC_MUNCHING);
@@ -170,13 +168,13 @@ protected void onValueChanged(int index) {
170168
String gameVariant = selectedValue();
171169
if (GameVariant.PACMAN_XXL.name().equals(gameVariant)) {
172170
Logger.info("Loading assets for game variant {}", gameVariant);
173-
theUI().config(gameVariant).storeAssets(theUI().theAssets());
171+
ui.config(gameVariant).storeAssets(ui.theAssets());
174172
}
175173
else if (GameVariant.MS_PACMAN_XXL.name().equals(gameVariant)) {
176174
Logger.info("Loading assets for game variant {}", gameVariant);
177-
theUI().config(gameVariant).storeAssets(theUI().theAssets());
175+
ui.config(gameVariant).storeAssets(ui.theAssets());
178176
}
179-
chaseAnimation.setGameVariant(gameVariant);
177+
chaseAnimation.setGameConfig(ui.config(gameVariant));
180178
state.gameVariant = gameVariant;
181179
logState();
182180
}
@@ -244,22 +242,25 @@ public String selectedValueText() {
244242
}
245243
};
246244

245+
private final GameUI ui;
247246
private final GameContext gameContext;
248247
private final MenuState state = new MenuState();
249248
private final ChaseAnimation chaseAnimation;
250249

251-
public PacManXXL_Common_StartPageMenu(GameContext gameContext) {
250+
public PacManXXL_Common_StartPageMenu(GameUI ui) {
252251
super(42, 36, 6, 20);
253-
this.gameContext = requireNonNull(gameContext);
252+
253+
this.ui = requireNonNull(ui);
254+
this.gameContext = requireNonNull(ui.theGameContext());
254255

255256
state.gameVariant = "PACMAN_XXL";
256257
state.play3D = false;
257258
state.cutScenesEnabled = true;
258259
state.mapOrder = MapSelectionMode.CUSTOM_MAPS_FIRST;
259260

260261
var style = new OptionMenuStyle(
261-
theUI().theAssets().font("font.pacfontgood", 32),
262-
theUI().theAssets().arcadeFont(8),
262+
ui.theAssets().font("font.pacfontgood", 32),
263+
ui.theAssets().arcadeFont(8),
263264
DEFAULT_OPTION_MENU_STYLE.backgroundFill(),
264265
DEFAULT_OPTION_MENU_STYLE.borderStroke(),
265266
ArcadePalette.ARCADE_RED,
@@ -304,14 +305,14 @@ public void syncMenuState() {
304305
entryMapOrder.selectValue(state.mapOrder);
305306
entryMapOrder.setEnabled(customMapsExist);
306307

307-
chaseAnimation.setGameVariant(state.gameVariant);
308+
chaseAnimation.setGameConfig(ui.config(state.gameVariant));
308309
chaseAnimation.reset();
309310
}
310311

311312
@Override
312313
protected void handleKeyPress(KeyEvent e) {
313314
if (nude(KeyCode.E).match(e)) {
314-
theUI().showEditorView();
315+
ui.showEditorView();
315316
} else if (nude(KeyCode.ENTER).match(e)) {
316317
startGame();
317318
} else {
@@ -343,6 +344,6 @@ private void startGame() {
343344
var mapSelector = (PacManXXL_Common_MapSelector) game.mapSelector();
344345
mapSelector.setMapSelectionMode(state.mapOrder);
345346
mapSelector.loadAllMaps();
346-
theUI().selectGameVariant(state.gameVariant);
347+
ui.selectGameVariant(state.gameVariant);
347348
}
348349
}

pacman-core/src/main/java/de/amr/pacmanfx/controller/GameController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void registerGame(String variant, GameModel gameModel) {
127127
Logger.warn("Game model ({}) is already registered for game variant {}", gameModel.getClass().getName(), variant);
128128
}
129129
knownGames.put(variant, gameModel);
130+
gameModel.init();
130131
}
131132

132133
public StringProperty gameVariantProperty() {

pacman-ui/src/main/java/de/amr/pacmanfx/ui/GameUI_Builder.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.io.File;
1818
import java.util.*;
1919

20+
import static java.util.Objects.requireNonNull;
21+
2022
public class GameUI_Builder {
2123

2224
private static class GameConfiguration {
@@ -32,24 +34,27 @@ private static class StartPageConfiguration {
3234

3335
public static GameUI_Builder createUI(Stage stage, double width, double height) {
3436
Logger.info("JavaFX runtime: {}", System.getProperty("javafx.runtime.version"));
35-
PacManGames_UI_Impl.THE_ONE = new PacManGames_UI_Impl(Globals.theGameContext(), stage, width, height);
36-
return new GameUI_Builder(PacManGames_UI_Impl.THE_ONE);
37+
return new GameUI_Builder(stage, width, height);
3738
}
3839

39-
private final PacManGames_UI_Impl ui;
40-
private final Map<String, GameConfiguration> configurationByGameVariant = new LinkedHashMap<>();
40+
private final Stage stage;
41+
private final double width;
42+
private final double height;
43+
private final Map<String, GameConfiguration> configByGameVariant = new LinkedHashMap<>();
4144
private final List<StartPageConfiguration> startPageConfigs = new ArrayList<>();
4245
private List<DashboardID> dashboardIDs = List.of();
4346

44-
private GameUI_Builder(PacManGames_UI_Impl ui) {
45-
this.ui = ui;
47+
private GameUI_Builder(Stage stage, double width, double height) {
48+
this.stage = requireNonNull(stage);
49+
this.width = width;
50+
this.height = height;
4651
}
4752

4853
private GameConfiguration configuration(String gameVariant) {
49-
if (!configurationByGameVariant.containsKey(gameVariant)) {
50-
configurationByGameVariant.put(gameVariant, new GameConfiguration());
54+
if (!configByGameVariant.containsKey(gameVariant)) {
55+
configByGameVariant.put(gameVariant, new GameConfiguration());
5156
}
52-
return configurationByGameVariant.get(gameVariant);
57+
return configByGameVariant.get(gameVariant);
5358
}
5459

5560
public GameUI_Builder game(
@@ -115,30 +120,31 @@ public GameUI_Builder dashboard(DashboardID... dashboardIDs) {
115120

116121
public GameUI build() {
117122
validateConfiguration();
118-
configurationByGameVariant.keySet().forEach(gameVariant -> {
119-
GameConfiguration gameConfiguration = configuration(gameVariant);
120-
GameModel gameModel = createGameModel(
121-
gameConfiguration.gameModelClass,
122-
gameConfiguration.mapSelector,
123-
ui.theGameContext(),
124-
highScoreFile(ui.theGameContext().theHomeDir(), gameVariant)
125-
);
126-
gameModel.init();
127-
ui.theGameContext().theGameController().registerGame(gameVariant, gameModel);
128-
ui.applyConfiguration(gameVariant, gameConfiguration.uiConfigClass);
123+
124+
final GameContext gameContext = Globals.theGameContext();
125+
126+
//TODO this is crap
127+
Map<String, Class<?>> uiConfigMap = new HashMap<>();
128+
configByGameVariant.forEach((gameVariant, config) -> uiConfigMap.put(gameVariant, config.uiConfigClass));
129+
var ui = new PacManGames_UI_Impl(uiConfigMap, gameContext, stage, width, height);
130+
131+
configByGameVariant.forEach((gameVariant, config) -> {
132+
File highScoreFile = highScoreFile(gameContext.theHomeDir(), gameVariant);
133+
GameModel gameModel = createGameModel(config.gameModelClass, config.mapSelector, gameContext, highScoreFile);
134+
gameContext.theGameController().registerGame(gameVariant, gameModel);
129135
});
136+
130137
for (StartPageConfiguration config : startPageConfigs) {
131-
StartPage startPage = createStartPage(config.gameVariants.getFirst(), config.startPageClass);
138+
StartPage startPage = createStartPage(ui, config.gameVariants.getFirst(), config.startPageClass);
132139
ui.theStartPagesView().addStartPage(startPage);
133-
134140
}
141+
135142
ui.thePlayView().configureDashboard(dashboardIDs);
136-
ui.theStartPagesView().selectStartPage(0); //TODO check this
137-
ui.theGameContext().theGameController().setEventsEnabled(true);
138-
return ui;
143+
144+
return PacManGames_UI_Impl.THE_ONE = ui;
139145
}
140146

141-
private StartPage createStartPage(String gameVariant, Class<?> startPageClass) {
147+
private StartPage createStartPage(GameUI ui, String gameVariant, Class<?> startPageClass) {
142148
// first try constructor(GameUI, String)
143149
try {
144150
var constructor = startPageClass.getDeclaredConstructor(GameUI.class, String.class);
@@ -150,11 +156,11 @@ private StartPage createStartPage(String gameVariant, Class<?> startPageClass) {
150156
return (StartPage) constructor.newInstance(ui);
151157
} catch (Exception xx) {
152158
error("Could not create start page from class '%s'".formatted(startPageClass.getSimpleName()));
153-
throw new IllegalStateException();
159+
throw new IllegalStateException(xx);
154160
}
155161
} catch (Exception x) {
156162
error("Could not create start page from class '%s'".formatted(startPageClass.getSimpleName()));
157-
throw new IllegalStateException();
163+
throw new IllegalStateException(x);
158164
}
159165
}
160166

@@ -174,7 +180,7 @@ private GameModel createGameModel(Class<?> modelClass, MapSelector mapSelector,
174180
}
175181

176182
private void validateConfiguration() {
177-
if (configurationByGameVariant.isEmpty()) {
183+
if (configByGameVariant.isEmpty()) {
178184
error("No game configuration specified");
179185
}
180186
}

pacman-ui/src/main/java/de/amr/pacmanfx/ui/PacManGames_UI_Impl.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public class PacManGames_UI_Impl implements GameUI {
6060
private final PlayView playView;
6161
private EditorView editorView; // created on demand
6262

63-
public PacManGames_UI_Impl(GameContext gameContext, Stage stage, double width, double height) {
63+
public PacManGames_UI_Impl(Map<String, Class<?>> configurationMap, GameContext gameContext, Stage stage, double width, double height) {
64+
requireNonNull(configurationMap, "UI configuration map is null");
6465
requireNonNull(gameContext, "Game context is null");
6566
requireNonNull(stage, "Stage is null");
6667

@@ -78,6 +79,7 @@ public PacManGames_UI_Impl(GameContext gameContext, Stage stage, double width, d
7879
theUIPrefs = new PacManGames_Preferences();
7980
theStage = stage;
8081

82+
configurationMap.forEach(this::applyConfiguration);
8183
initGlobalActionBindings();
8284

8385
mainScene = new MainScene(this, width, height);
@@ -94,6 +96,23 @@ public PacManGames_UI_Impl(GameContext gameContext, Stage stage, double width, d
9496
PROPERTY_3D_WALL_OPACITY.set(theUIPrefs.getFloat("3d.obstacle.opacity"));
9597
}
9698

99+
private void applyConfiguration(String gameVariant, Class<?> configClass) {
100+
try {
101+
GameUI_Config config = (GameUI_Config) configClass.getDeclaredConstructor(GameUI.class).newInstance(this);
102+
config.createGameScenes();
103+
Logger.info("Game scenes for game variant '{}' created", gameVariant);
104+
config.gameScenes().forEach(scene -> {
105+
if (scene instanceof GameScene2D gameScene2D) {
106+
gameScene2D.debugInfoVisibleProperty().bind(PROPERTY_DEBUG_INFO_VISIBLE);
107+
}
108+
});
109+
setConfig(gameVariant, config);
110+
} catch (Exception x) {
111+
Logger.error("Could not apply UI configuration of class {}", configClass);
112+
throw new IllegalStateException(x);
113+
}
114+
}
115+
97116
private void configureMainScene() {
98117
mainScene.currentGameSceneProperty().bindBidirectional(PROPERTY_CURRENT_GAME_SCENE);
99118
mainScene.currentViewProperty().bindBidirectional(PROPERTY_CURRENT_VIEW);
@@ -158,23 +177,6 @@ private void selectView(PacManGames_View view) {
158177
GameUI.PROPERTY_CURRENT_VIEW.set(view);
159178
}
160179

161-
public void applyConfiguration(String gameVariant, Class<?> configClass) {
162-
try {
163-
GameUI_Config config = (GameUI_Config) configClass.getDeclaredConstructor(GameUI.class).newInstance(this);
164-
config.createGameScenes();
165-
Logger.info("Game scenes for game variant '{}' created", gameVariant);
166-
config.gameScenes().forEach(scene -> {
167-
if (scene instanceof GameScene2D gameScene2D) {
168-
gameScene2D.debugInfoVisibleProperty().bind(PROPERTY_DEBUG_INFO_VISIBLE);
169-
}
170-
});
171-
setConfig(gameVariant, config);
172-
} catch (Exception x) {
173-
Logger.error("Could not apply UI configuration of class {}", configClass);
174-
throw new IllegalStateException(x);
175-
}
176-
}
177-
178180
/**
179181
* @param reason what caused this catastrophe
180182
*
@@ -314,10 +316,12 @@ public void selectGameVariant(String gameVariant) {
314316
@Override
315317
public void show() {
316318
playView.initDashboard();
319+
startPagesView.selectStartPage(0);
317320
showStartView();
318321
theStage.centerOnScreen();
319322
theStage.show();
320323
Platform.runLater(theCustomDirWatchdog::startWatching);
324+
theGameContext.theGameController().setEventsEnabled(true);
321325
}
322326

323327
@Override

pacman-ui/src/main/java/de/amr/pacmanfx/ui/_3d/GameLevel3D.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ private void handleDrawModeChange(ObservableValue<? extends DrawMode> obs, DrawM
842842
}
843843

844844
private static void setDrawModeUnder(Node node, Predicate<Node> exclusionFilter, DrawMode drawMode) {
845+
if (node == null) return; //TODO why does this happen?
845846
node.lookupAll("*").stream()
846847
.filter(exclusionFilter.negate())
847848
.filter(Shape3D.class::isInstance)

pacman-ui/src/main/java/de/amr/pacmanfx/ui/layout/MiniGameView.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ public void setGameUI(GameUI ui) {
113113
}
114114

115115
public void setGameLevel(GameLevel gameLevel) {
116+
/* TODO: The game renderer cannot yet be created in setGameUI because at the time, setGameUI is called, the
117+
game controller has not yet selected a game variant and therefore the current UI config is null! */
118+
gr = ui.theConfiguration().createGameRenderer(canvas);
119+
gr.setScaling(scalingProperty.floatValue());
116120
this.gameLevel = requireNonNull(gameLevel);
117121
worldSizeProperty.set(gameLevel.worldSizePx());
118-
gr = ui.theConfiguration().createGameRenderer(canvas);
119122
gr.applyRenderingHints(gameLevel);
120-
gr.setScaling(scalingProperty.floatValue());
121123
}
122124

123125
public void slideIn() {

0 commit comments

Comments
 (0)