Skip to content

Setup Scene for Fleks world and render views

Marko Koschak edited this page Mar 31, 2024 · 1 revision

This page describes the setup of a Korge Scene which contains the Fleks ECS WorldConfiguration and the various RenderView's.

class GameScene : Scene() {
    private lateinit var gameWorld: World

    override suspend fun SContainer.sceneInit() {
        // TODO Create game controller
        // and let game controller decide which assets needs to be loaded
        [...]

        val estimatedNumberOfEntities = 1024

        // This is the world object of the entity component system (ECS)
        // It contains all ECS related system and component configuration
        gameWorld = configureWorld(entityCapacity = estimatedNumberOfEntities) {

            // Register all needed systems of the entity component system
            // The order of systems here also define the order in which the systems are called inside Fleks ECS
            systems {
                add(TweenSequenceSystem())
                add(AnimateAppearanceSystem())
                add(AnimateSwitchLayerVisibilitySystem())
                add(AnimatePositionShapeSystem())
                add(AnimateSpriteSystem())
                add(AnimateSpawnerSystem())
                add(AnimateLifeCycleSystem())
                add(AnimateSoundSystem())

                add(PositionSystem())
                add(SpawnerSystem())
                add(LifeCycleSystem())

                add(SoundSystem())
            }
        }

        with(gameWorld) {
            // Run the update of the Fleks ECS - this will periodically call all update functions of the systems
            // (e.g. onTick(), onTickEntity(), etc.)
            addUpdater { dt -> update(dt.seconds.toFloat()) }

            // Background layers
            [...]

            // Main layers
            spriteRenderView("playground",
                family { all(PLAYGROUND, SpriteComponent, PositionComponent) }
            )

            // Foreground layers
            spriteRenderView("foreground",
                family { all(FOREGROUND, SpriteComponent, PositionComponent) }
            )

            // Debug shape layers
            [...]
        }
    }

    override suspend fun SContainer.sceneMain() {
        // Create game objects here - Trigger game controller to start game
        [...]
    }
}
Clone this wiki locally