@nanoforge-dev/scene splits a game into scenes: a menu, each level, a pause screen. A scene creates what it needs when it is loaded and removes it when it is unloaded.
Write scenes
A scene extendsScene and overrides load(ctx, scene) and unload(ctx). A scene that only spawns entities and adds systems can extend EcsScene from @nanoforge-dev/ecs/scene instead: write its setup(registry, ctx) like your main, and everything it spawns or adds is removed when the scene is unloaded.
parent loads a scene under another one: here Pause keeps Level1 loaded under it.
Register the library
GiveSceneLibrary the first scene, and every scene of the game by id:
scenes are the scene ids. Name them yourself: a bundler can rename classes, never these keys.
Change scene
From a system, or anywhere you have the context:- Only one chain of scenes is loaded at a time, from a root scene to the current one. Loading a scene unloads the scenes it does not share with the new chain, deepest first, then loads the missing ones, root first.
- Pass parameters with
ctx.scenes.load(ctx, Level1, { params }). The scene reads them inload(ctx, { params }). Type them withScene<[params type]>. - Changes are queued and made when no system is running: right after the systems of the current tick, or before the systems of the next one. In a system, don’t wait for the promise
loadreturns.
Shared values
ctx.scenes.vars holds values shared by scenes and systems: init, get, set, has and remove. A value belongs to the scene that created it and goes away when that scene is unloaded, unless you create it with { persistent: true }.
Type your values by augmenting SceneVars: