Skip to content

SceneManager

Overview

The SceneManager is responsible for managing game scenes, including loading, transitioning, and handling their lifecycle.
It allows registering scenes, loading a specific scene, and managing the game's startup through an opening scene.

Responsibilities

  • Register and store scenes.
  • Load scenes and handle transitions.
  • Execute asset loading, system registration, and entity setup for each scene.
  • Manage cleanup of resources and entities when switching scenes.

Usage Example

// Register scenes
this.sceneManager.addScene(MainMenuScene, "MainMenu", true);
this.sceneManager.addScene(GameplayScene, "Gameplay");

// Load the opening scene
this.sceneManager.loadOpeningScene();

// Switch to another scene at any time
this.sceneManager.loadScene("Gameplay");

// As a second argument, you can pass a component to keep the entities that have that component, for example, we keep the entity that has the AudioPlayer component so that the music does not stop when changing scenes
this.sceneManager.loadScene("OptionsMenu", AudioPlayer);

Scene Loading Flow

  1. Calls the scene’s loadAssets() to load resources.
  2. Calls registerSystems() to register the systems the scene will use.
  3. Waits until all assets are fully loaded.
  4. Calls createEntities() to create entities and configure the scene’s initial state.
  5. Activates the systems and executes them in the defined order.

Notes

  • The SceneManager ensures that all systems and entities from the previous scene are removed before loading a new scene.
  • The AudioPlayerSystem and VideoRendererSystem remain active between scenes.
  • All time intervals (setInterval) are automatically cleared when switching scenes to prevent unintended behavior.