Skip to content

1681-engine / index / Scene

Abstract Class: Scene ​

Defined in: src/engine/Scene.ts:29

One screen of the game: a title, a level, a pause menu. A scene owns a World of entities and systems and a root container in the engine's world layer; both are cleared when it leaves, so nothing it made outlives it.

Override the hooks you need. fixedUpdate runs once per simulation step, update once per rendered frame, both after the world's own systems of that phase.

Two things a scene should hand the engine so they end with it: timers (delay, not stage.time.delayedCall, whose timers belong to the stage and fire after the scene has gone) and its input (useInput, flushed when a scene pushed over this one pops, so presses made in a pause menu are not read again by the level under it).

Names taken: world, root and hud are the scene's own fields, so a subclass cannot declare a field of those names for something else (a HUD class of the game's is best kept as display, overlay, …).

Constructors ​

Constructor ​

ts
new Scene(): Scene;

Returns ​

Scene

Properties ​

hud ​

ts
readonly hud: Container<ContainerChild>;

Defined in: src/engine/Scene.ts:34

Everything the scene draws on the screen (HUD) goes under this.


root ​

ts
readonly root: Container<ContainerChild>;

Defined in: src/engine/Scene.ts:32

Everything the scene draws in the world goes under this (the camera moves it).


world ​

ts
readonly world: World;

Defined in: src/engine/Scene.ts:30

Accessors ​

engine ​

Get Signature ​

ts
get engine(): Engine;

Defined in: src/engine/Scene.ts:40

The engine running this scene; only valid between enter and exit.

Returns ​

Engine

Methods ​

delay() ​

ts
delay(ms, fn): SceneTimer;

Defined in: src/engine/Scene.ts:62

Calls fn after ms on the stage's clock, unless the scene has left by then: its timers are cancelled when it exits. Only valid while the scene is running (between enter and exit).

Parameters ​

ParameterType
msnumber
fn() => void

Returns ​

SceneTimer


fixedUpdate() ​

ts
fixedUpdate(_dt): void;

Defined in: src/engine/Scene.ts:54

Once per simulation step, dt in seconds.

Parameters ​

ParameterType
_dtnumber

Returns ​

void


onEnter() ​

ts
onEnter(): void | Promise<void>;

Defined in: src/engine/Scene.ts:46

Called once when the scene becomes active. May be async: the scene starts updating when it resolves.

Returns ​

void | Promise<void>


onExit() ​

ts
onExit(): void;

Defined in: src/engine/Scene.ts:48

Called when the scene is removed. The world and containers are cleared after it returns.

Returns ​

void


onPause() ​

ts
onPause(): void;

Defined in: src/engine/Scene.ts:50

Called when another scene is pushed over this one.

Returns ​

void


onResume() ​

ts
onResume(): void;

Defined in: src/engine/Scene.ts:52

Called when the scene over this one is popped.

Returns ​

void


update() ​

ts
update(_dt, _alpha): void;

Defined in: src/engine/Scene.ts:56

Once per rendered frame, dt in seconds; alpha is the share of a step since the last one (0–1).

Parameters ​

ParameterType
_dtnumber
_alphanumber

Returns ​

void


useInput() ​

ts
useInput<T>(input): T;

Defined in: src/engine/Scene.ts:82

Registers input the scene reads (an Actions) and returns it, so a field can be written in one line: private readonly input = this.useInput(new Actions(keyboard, bindings));. When a scene pushed over this one pops, every registered input is flushed before onResume, so a key pressed in the menu is not a press here.

Type Parameters ​

Type Parameter
T extends FlushableInput

Parameters ​

ParameterType
inputT

Returns ​

T

Released under the MIT License.