1.3.0 • Published 10 months ago

@basementuniverse/scene-manager v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Game Component: Scene Manager

A component for managing a stack of scenes in a game.

Installation

npm install @basementuniverse/scene-manager

How to use

Initialise the scene manager before use:

import SceneManager from '@basementuniverse/scene-manager';

SceneManager.initialise();

Update and draw the scene manager:

class Game {
  // ...

  public update(dt: number) {
    SceneManager.update(dt);
  }

  public draw(context: CanvasRenderingContext2D) {
    SceneManager.draw(context);
  }
}

Create and start a scene:

SceneManager.push(new MyScene());

End/remove a scene:

const scene = SceneManager.pop();

Clear scenes:

SceneManager.clear();

Only the top-most scene will be updated.

Scenes will be drawn bottom-to-top, but with some scenes culled depending on transparency.

Implementing a scene

Define a class that inherits from Scene:

import SceneManager, { Scene } from '@basementuniverse/scene-manager';

export class MyScene extends Scene {
  public constructor() {
    super({ transitionTime: 2.5 });
  }

  public initialise() {
    // Called when the scene is pushed onto the SceneManager
  }

  public update(dt: number) {
    // Called on every SceneManager.update() while this is the top-most scene

    console.log(this.transitionAmount); // between 0...1

    // Start another scene when a key is pressed
    if (InputManager.keyPressed('Enter')) {
      SceneManager.push(new MyOtherScene());
    }

    // Finish this scene when a key is pressed
    if (InputManager.keyPressed('Escape')) {
      SceneManager.pop();
    }
  }

  public draw(context: CanvasRenderingContext2D) {
    // Called on every SceneManager.draw() while this scene is visible
    // (depends on scenes above this one in the stack)
  }
}

Scene options

const options = { ... };
const myScene = new Scene(options);
OptionTypeDefaultDescription
transitionTimenumber2The number of seconds it takes to transition in/out
transparentbooleantrueIf true, this scene will show other scenes below it in the stack
onTransitionedIn() => voidundefinedCalled when the scene has fully transitioned in
onTransitionedOut() => voidundefinedCalled when the scene has fully transitioned out
1.3.0

10 months ago

1.1.1

12 months ago

1.1.0

1 year ago

1.2.1

12 months ago

1.1.2

12 months ago

1.0.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago