Skip to content

Commit 847bdd3

Browse files
committed
feat: SceneService now provides direct access to intro, loading, game, main menu, game menu scenes, closes #1315
1 parent dba0749 commit 847bdd3

File tree

4 files changed

+68
-45
lines changed

4 files changed

+68
-45
lines changed

fxgl-scene/src/main/kotlin/com/almasb/fxgl/scene/SceneService.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.almasb.fxgl.input.Input
1212
import com.almasb.fxgl.time.Timer
1313
import javafx.beans.property.ReadOnlyDoubleProperty
1414
import javafx.scene.Group
15+
import java.util.Optional
1516

1617
/**
1718
* Provides access to pushing / popping subscene stack, global input, timer, overlay root and application
@@ -71,6 +72,31 @@ abstract class SceneService : EngineService() {
7172
*/
7273
abstract val currentScene: Scene
7374

75+
/**
76+
* @return intro scene constructed for this game if present
77+
*/
78+
abstract val introScene: Optional<Scene>
79+
80+
/**
81+
* @return loading scene constructed for this game if present
82+
*/
83+
abstract val loadingScene: Optional<Scene>
84+
85+
/**
86+
* @return game scene constructed for this game
87+
*/
88+
abstract val gameScene: Scene
89+
90+
/**
91+
* @return main menu scene constructed for this game if present
92+
*/
93+
abstract val mainMenuScene: Optional<Scene>
94+
95+
/**
96+
* @return game menu scene constructed for this game if present
97+
*/
98+
abstract val gameMenuScene: Optional<Scene>
99+
74100
/**
75101
* @return true if [scene] is in this scene service hierarchy
76102
*/

fxgl-scene/src/test/kotlin/com/almasb/fxgl/notification/NotificationServiceTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
2525
import org.junit.jupiter.api.BeforeEach
2626
import org.junit.jupiter.api.Test
2727
import java.lang.invoke.MethodHandles
28+
import java.util.*
2829

2930
/**
3031
*
@@ -64,6 +65,21 @@ class NotificationServiceTest {
6465
override val currentScene: Scene
6566
get() = object : Scene() {}
6667

68+
override val introScene: Optional<Scene>
69+
get() = Optional.ofNullable(object : Scene() {})
70+
71+
override val loadingScene: Optional<Scene>
72+
get() = Optional.ofNullable(object : Scene() {})
73+
74+
override val mainMenuScene: Optional<Scene>
75+
get() = Optional.ofNullable(object : Scene() {})
76+
77+
override val gameMenuScene: Optional<Scene>
78+
get() = Optional.ofNullable(object : Scene() {})
79+
80+
override val gameScene: Scene
81+
get() = object : Scene() {}
82+
6783
override fun isInHierarchy(scene: Scene): Boolean {
6884
return false
6985
}

fxgl-scene/src/test/kotlin/com/almasb/fxgl/scene/SceneTest.kt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77
package com.almasb.fxgl.scene
88

99
import com.almasb.fxgl.core.Updatable
10-
import com.almasb.fxgl.event.EventBus
11-
import com.almasb.fxgl.input.Input
12-
import com.almasb.fxgl.time.Timer
13-
import javafx.beans.property.ReadOnlyDoubleProperty
14-
import javafx.beans.property.ReadOnlyDoubleWrapper
1510
import javafx.beans.property.SimpleDoubleProperty
16-
import javafx.scene.Group
1711
import javafx.scene.Node
1812
import javafx.scene.layout.Region
1913
import javafx.scene.shape.Rectangle
@@ -62,36 +56,6 @@ class SceneTest {
6256

6357
@Test
6458
fun `Default methods are noop`() {
65-
val sceneService = object : SceneService() {
66-
override val overlayRoot: Group
67-
get() = Group()
68-
override fun prefWidthProperty(): ReadOnlyDoubleProperty {
69-
return ReadOnlyDoubleWrapper(600.0).readOnlyProperty
70-
}
71-
override fun prefHeightProperty(): ReadOnlyDoubleProperty {
72-
return ReadOnlyDoubleWrapper(600.0).readOnlyProperty
73-
}
74-
75-
override val eventBus: EventBus
76-
get() = EventBus()
77-
override val input: Input
78-
get() = Input()
79-
override val timer: Timer
80-
get() = Timer()
81-
82-
override val currentScene: Scene
83-
get() = object : Scene() {}
84-
85-
override fun isInHierarchy(scene: Scene): Boolean {
86-
return false
87-
}
88-
89-
override fun pushSubScene(subScene: SubScene) {
90-
}
91-
92-
override fun popSubScene() {
93-
}
94-
}
9559
val scene = object : Scene() {}
9660

9761
scene.onCreate()

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ import javafx.util.Duration
5858
import java.nio.file.Files
5959
import java.nio.file.Paths
6060
import java.time.LocalDateTime
61+
import java.util.*
6162
import javax.imageio.ImageIO
63+
import kotlin.collections.HashMap
6264
import kotlin.system.measureNanoTime
6365

6466
/**
@@ -377,13 +379,28 @@ class FXGLApplication : Application() {
377379
override val currentScene: Scene
378380
get() = mainWindow.currentScene
379381

380-
internal lateinit var gameScene: GameScene
382+
private lateinit var gameSceneRef: GameScene
381383
private lateinit var loadScene: LoadingScene
382384

383385
private var intro: FXGLScene? = null
384386
private var mainMenu: SubScene? = null
385387
private var gameMenu: SubScene? = null
386388

389+
override val introScene: Optional<Scene>
390+
get() = Optional.ofNullable(intro)
391+
392+
override val loadingScene: Optional<Scene>
393+
get() = Optional.ofNullable(loadScene)
394+
395+
override val mainMenuScene: Optional<Scene>
396+
get() = Optional.ofNullable(mainMenu)
397+
398+
override val gameMenuScene: Optional<Scene>
399+
get() = Optional.ofNullable(gameMenu)
400+
401+
override val gameScene: GameScene
402+
get() = gameSceneRef
403+
387404
internal val window: MainWindow
388405
get() = mainWindow
389406

@@ -432,13 +449,13 @@ class FXGLApplication : Application() {
432449
val sceneFactory = settings.sceneFactory
433450

434451
loadScene = sceneFactory.newLoadingScene()
435-
gameScene = GameScene(settings.width, settings.height,
452+
gameSceneRef = GameScene(settings.width, settings.height,
436453
GameWorld(),
437454
PhysicsWorld(settings.height, settings.pixelsPerMeter, settings.collisionDetectionStrategy),
438455
settings.is3D
439456
)
440457

441-
gameScene.isSingleStep = settings.isSingleStep
458+
gameSceneRef.isSingleStep = settings.isSingleStep
442459

443460
if (settings.isClickFeedbackEnabled) {
444461
addClickFeedbackHandler()
@@ -475,7 +492,7 @@ class FXGLApplication : Application() {
475492
canSwitchGameMenu = false
476493
popSubScene()
477494

478-
} else if (mainWindow.currentScene === gameScene) {
495+
} else if (mainWindow.currentScene === gameSceneRef) {
479496
canSwitchGameMenu = false
480497
pushSubScene(gameMenu!!)
481498
}
@@ -489,7 +506,7 @@ class FXGLApplication : Application() {
489506
}
490507
}
491508

492-
gameScene.input.addEventHandler(KeyEvent.ANY, menuKeyHandler)
509+
gameSceneRef.input.addEventHandler(KeyEvent.ANY, menuKeyHandler)
493510
gameMenu!!.input.addEventHandler(KeyEvent.ANY, menuKeyHandler)
494511
}
495512

@@ -505,8 +522,8 @@ class FXGLApplication : Application() {
505522
}
506523

507524
private fun addClickFeedbackHandler() {
508-
gameScene.input.addEventHandler(MouseEvent.MOUSE_PRESSED, EventHandler {
509-
val circle = Circle(gameScene.input.mouseXUI, gameScene.input.mouseYUI, 5.0, null)
525+
gameSceneRef.input.addEventHandler(MouseEvent.MOUSE_PRESSED, EventHandler {
526+
val circle = Circle(gameSceneRef.input.mouseXUI, gameSceneRef.input.mouseYUI, 5.0, null)
510527
circle.stroke = Color.GOLD
511528
circle.strokeWidth = 2.0
512529
circle.opacityProperty().bind(SimpleDoubleProperty(1.0).subtract(circle.radiusProperty().divide(35.0)))
@@ -618,7 +635,7 @@ class FXGLApplication : Application() {
618635
private fun clearPreviousGame() {
619636
log.debug("Clearing previous game")
620637

621-
gameScene.reset()
638+
gameSceneRef.reset()
622639
}
623640

624641
fun saveGame(dataFile: DataFile) {
@@ -681,7 +698,7 @@ class FXGLApplication : Application() {
681698
}
682699

683700
fun gotoPlay() {
684-
mainWindow.setScene(gameScene)
701+
mainWindow.setScene(gameSceneRef)
685702
}
686703

687704
/**

0 commit comments

Comments
 (0)