sceneManager.js

SceneManager

State machine for game scenes. Handles transitions between loading, menu, gameplay, and game-over states.

SceneManager

A simple state machine for switching between game scenes. Each scene is a class that extends Scene — the manager calls init() and end() on transitions and delegates update() and render() to the active scene.

Include

<script src="/js/dn/scene.js"></script>
<script src="/js/dn/sceneManager.js"></script>

SceneManager depends on Scene (the base class all scenes extend).

Setup Pattern

// 1. Create the manager
const scenes = new SceneManager();

// 2. Register scenes — each becomes an instance of its class
scenes.registerScene('loading', LoadingScene);
scenes.registerScene('menu',    MenuScene);
scenes.registerScene('game',    GameScene);
scenes.registerScene('gameOver', GameOverScene);

// 3. Wire to game loop
const loop = new GameLoop({
  updateCallback: (dt) => scenes.update(dt),
  renderCallback: ()   => scenes.render(),
});

// 4. Start on the first scene
scenes.gotoScene({ name: 'loading' });
loop.startLoop();

Methods

MethodDescription
registerScene(name, SceneClass)Instantiate and store a scene. Call before gotoScene.
gotoScene({ name?, index? })Transition to a scene. Calls end() on current, init() on next.
getCurrentScene()Returns the active Scene instance.
getCurrentSceneName()Returns the name string of the active scene.
indexByName(name)Returns the registration index of a named scene, or -1.
update(dt)Call from GameLoop.updateCallback. Delegates to active scene.
render()Call from GameLoop.renderCallback. Delegates to active scene.

Scenes can call scenes.gotoScene() directly. Pass the scenes object into scenes that need to trigger transitions:

class GameScene extends Scene {
  constructor(name, scenes) {
    super(name);
    this.scenes = scenes;
  }
  update(dt) {
    if (this.lives <= 0) {
      this.scenes.gotoScene({ name: 'gameOver' });
    }
  }
}

// Register with the scenes reference passed in:
scenes.registerScene('game', () => new GameScene('game', scenes));

Or keep a global scenes reference — both patterns work fine.

Source

public/js/dn/sceneManager.js + public/js/dn/scene.js