Skip to content

Commit f418525

Browse files
authored
feat: allow selecting difficulty in the gameplay menu
1 parent 737e282 commit f418525

4 files changed

Lines changed: 78 additions & 2 deletions

File tree

fxgl-samples/src/main/java/intermediate/MenuSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected void initSettings(GameSettings settings) {
2929
settings.setMainMenuEnabled(true);
3030
settings.setGameMenuEnabled(true);
3131
settings.setFullScreenAllowed(true);
32-
settings.setEnabledMenuItems(EnumSet.of(MenuItem.EXTRA));
32+
settings.setEnabledMenuItems(EnumSet.of(MenuItem.DIFFICULTY, MenuItem.EXTRA));
3333
settings.getCredits().addAll(Arrays.asList(
3434
"Short Name - Lead Programmer",
3535
"LongLongLongLongLongLongLong Name - Programmer",

fxgl/src/main/kotlin/com/almasb/fxgl/app/Settings.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ enum class MenuItem {
6060
/**
6161
* Enables ONLINE (multiplayer).
6262
*/
63-
ONLINE
63+
ONLINE,
64+
65+
/**
66+
* Enables DIFFICULTY -> EASY, MEDIUM, HARD, NIGHTMARE
67+
*/
68+
DIFFICULTY,
6469
}
6570

6671
/**

fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/FXGLDefaultMenu.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.almasb.fxgl.dsl.*
1616
import com.almasb.fxgl.dsl.FXGL.Companion.animationBuilder
1717
import com.almasb.fxgl.dsl.FXGL.Companion.random
1818
import com.almasb.fxgl.dsl.FXGL.Companion.texture
19+
import com.almasb.fxgl.gameplay.GameDifficulty
1920
import com.almasb.fxgl.input.Input
2021
import com.almasb.fxgl.input.InputModifier
2122
import com.almasb.fxgl.input.Trigger
@@ -258,6 +259,12 @@ open class FXGLDefaultMenu(type: MenuType) : FXGLMenu(type) {
258259
itemOptions.setChild(createOptionsMenu())
259260
box.add(itemOptions)
260261

262+
if (enabledItems.contains(MenuItem.DIFFICULTY)) {
263+
val itemDifficulty = MenuButton("menu.difficulty")
264+
itemDifficulty.setMenuContent({ createDifficultyMenu() })
265+
box.add(itemDifficulty)
266+
}
267+
261268
if (enabledItems.contains(MenuItem.EXTRA)) {
262269
val itemExtra = MenuButton("menu.extra")
263270
itemExtra.setChild(createExtraMenu())
@@ -297,6 +304,12 @@ open class FXGLDefaultMenu(type: MenuType) : FXGLMenu(type) {
297304
itemOptions.setChild(createOptionsMenu())
298305
box.add(itemOptions)
299306

307+
if (enabledItems.contains(MenuItem.DIFFICULTY)) {
308+
val itemDifficulty = MenuButton("menu.difficulty")
309+
itemDifficulty.setMenuContent({ createDifficultyMenu() })
310+
box.add(itemDifficulty)
311+
}
312+
300313
if (enabledItems.contains(MenuItem.EXTRA)) {
301314
val itemExtra = MenuButton("menu.extra")
302315
itemExtra.setChild(createExtraMenu())
@@ -343,6 +356,30 @@ open class FXGLDefaultMenu(type: MenuType) : FXGLMenu(type) {
343356
return MenuBox(itemGameplay, itemControls, itemVideo, itemAudio, btnRestore)
344357
}
345358

359+
private fun createDifficultyMenu(): MenuContent {
360+
val difficultyBox = getUIFactoryService().newChoiceBox(
361+
FXCollections.observableArrayList(GameDifficulty.entries)
362+
)
363+
364+
difficultyBox.styleClass.add("fxgl-difficulty-choice-box")
365+
366+
difficultyBox.value = getSettings().gameDifficulty
367+
getSettings().gameDifficultyProperty().bindBidirectional(difficultyBox.valueProperty())
368+
369+
difficultyBox.valueProperty().addListener { _, _, _ ->
370+
switchMenuContentTo(EMPTY)
371+
}
372+
373+
val row = HBox(
374+
25.0,
375+
getUIFactoryService().newText(localizedStringProperty("menu.difficulty").concat(":")),
376+
difficultyBox
377+
)
378+
row.alignment = Pos.CENTER
379+
380+
return MenuContent(row)
381+
}
382+
346383
private fun createExtraMenu(): MenuBox {
347384
log.debug("createExtraMenu()")
348385

fxgl/src/main/resources/fxglassets/ui/css/fxgl_dark.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,40 @@
309309
}
310310

311311

312+
/*
313+
* Styling for the difficulty choice box
314+
*/
315+
.fxgl-difficulty-choice-box {
316+
-fx-background-color: rgba(0, 0, 0, 0.35);
317+
-fx-background-radius: 4;
318+
-fx-background-insets: 0;
319+
-fx-border-color: rgba(255, 255, 255, 0.25);
320+
-fx-border-radius: 4;
321+
-fx-font-size: 18px;
322+
}
323+
324+
.fxgl-difficulty-choice-box:hover,
325+
.fxgl-difficulty-choice-box:focused,
326+
.fxgl-difficulty-choice-box:focused:hover,
327+
.fxgl-difficulty-choice-box:armed {
328+
-fx-background-color: rgba(0, 0, 0, 0.45);
329+
-fx-background-radius: 4;
330+
-fx-background-insets: 0;
331+
-fx-border-color: rgba(255, 255, 255, 0.35);
332+
}
333+
334+
.fxgl-difficulty-choice-box .label {
335+
-fx-text-fill: white;
336+
-fx-background-color: transparent;
337+
}
338+
339+
.fxgl-difficulty-choice-box .context-menu {
340+
-fx-background-color: rgba(20, 20, 20, 0.9);
341+
}
342+
343+
.fxgl-difficulty-choice-box .menu-item:focused {
344+
-fx-background-color: rgba(255, 255, 255, 0.15);
345+
}
312346

313347

314348
/*

0 commit comments

Comments
 (0)