Worlds

View source

ExtendsMap

Worlds is the app’s world registry. It extends Map and stores each World under the TypeId returned by typeid for its constructor label.

App configuration adds worlds through App#setWorld and selects the default with App#defaultWorld. API users can call Worlds#getWorld without a label to retrieve the selected default world, or pass a label to retrieve its registered world. A missing default or world causes lookup to assert instead of returning undefined.

Examples

import { Worlds } from '@wimaengine/app'
import { World } from '@wimaengine/ecs'
import { typeid } from '@wimaengine/type'

class MainWorld {}
const worlds = new Worlds()
const mainWorld = new World()

worlds.set(typeid(MainWorld), mainWorld)
worlds.defaultWorldId = typeid(MainWorld)

worlds.getWorld() === mainWorld // true
worlds.getWorld(MainWorld) === mainWorld // true

Constructor


constructor()

View source

Parameters

NameDescriptionDefault
entries?——

Parameters

NameDescriptionDefault
iterable?——

Properties


defaultWorldId: TypeId

View source

The TypeId used to select the world returned by Worlds#getWorld when no label is supplied.

It starts as undefined. App#defaultWorld assigns it, but that assignment does not create a map entry or verify that the world exists.

Default: undefined

Methods


getWorld(label?: ConstructorFn): World

View source

Return the selected default World or the world identified by a label.

With no label, this method reads Worlds#defaultWorldId and returns the matching map entry. With a label, it converts the constructor to a TypeId with typeid and looks up that key.

The lookup asserts when the default ID is null or undefined, or when the selected map entry is null or undefined. The first failure uses The default world is not set.; a missing selected world uses The world "<TypeId>" is not set..

Parameters

NameDescriptionDefault
label?——

Throws

If no default is selected, or if the requested world is not registered.