Class
Scene
View sourceRepresents a scene snapshot asset that can capture world state and restore it as new entities and resources.
Examples
import { Scene } from '@wimaengine/scene'
const scene = Scene.fromWorld(world, typeRegistry)
console.log(scene.entityCount())
for (const [entityId, components] of scene.entities) {
console.log(entityId, components.length)
}Constructor
Properties
entities: Map<number, object[]>
View sourceStores component snapshots by the packed ID of their source entity.
This map is the scene’s live entity storage. Each value is the component snapshot array for its source entity, and the map and arrays are stored directly so mutations are visible to the scene immediately. Capturing a world keeps an entry even when none of that entity’s component snapshots can be stored, so an entry may have an empty array.
Default: ...
resources: Map<TypeId, object>
View sourceStores resource snapshots by their live type ID.
This map is the scene’s live resource storage. Keys identify the snapshot
types used for registry lookup and filtering during Scene#toWorld.
Snapshot objects are stored directly without cloning or validation.
Default: ...
Methods
toWorld(world: World, instance: SceneInstance, typeRegistry: TypeRegistry, instanceEntity: EntityHandle, filter?: SceneFilter): void
View sourceRestores this scene snapshot into a world.
One new world entity is spawned for each entry in Scene#entities,
even when filtering removes every snapshot in that entry. Stored entity
IDs identify the source entities only; the method maps each new world ID to
its source ID in SceneInstance#entityMap and passes the reverse
mapping to registered snapshot mapping methods. Existing entries in
SceneInstance#entityMap are not cleared.
EntityHandle snapshots are ignored because each spawned entity already
has its live handle. Other component snapshots are restored through the
supplied TypeRegistry. Snapshots whose type is disallowed by
filter, is not registered, or produces no restored value are skipped.
When a restored entity has no Parent and no filter is supplied or
filter allows Parent’s type ID, a parent pointing to instanceEntity
is added. A filter that denies Parent prevents this automatic parent
component.
Resource snapshots are filtered by their map keys. A registered patch
method handles a resource when it returns true; otherwise the resource
is restored through its registered fromSnapshot or clone method and
set in the world when restoration produces a value. Registered map
methods are called for restored components and resources with the
scene-to-world entity mapping. The instance’s asset ID, when available,
is supplied to registered snapshot restoration and resource patch methods.
staticfromWorld(world: World, typeRegistry: TypeRegistry): Scene
View sourceCaptures a scene snapshot from a world.
Each entity selected by the ECS Query for EntityHandle
receives an entry in Scene#entities, keyed by its packed ID.
Each world resource with a matching TypeRegistry entry is added
to Scene#resources when its snapshot succeeds. A registered
toSnapshot method is preferred; when none is registered, the registered
clone method is used. Unknown types and values without either method are
skipped. Resource entries are keyed by the live resource type ID, and an
entity remains in the map even if its component array is empty.
The source world is not modified.
staticserializeWithContext(scene: Scene, typeRegistry: TypeRegistry): object
View sourceConverts a scene into its registry-aware, JSON-compatible representation.
staticdeserializeWithContext(serial: unknown, typeRegistry: TypeRegistry): Scene
View sourceReconstructs a scene from its registry-aware serialized representation.
staticdefault(): Scene
View sourceCreates an empty scene asset.
entityCount(): number
View sourceReturns the number of entity entries stored in this scene.
This is the size of Scene#entities. It counts entries even when
their component snapshot arrays are empty, but does not count individual
component snapshots or entries in Scene#resources.
set(entity: EntityHandle, components: object[]): void
View sourceStores component snapshots for an entity ID.
The entity’s packed EntityId is used as the map key; the handle
itself is not stored. An existing entry for the same ID is replaced. The
supplied array is stored by reference and is not cloned, merged, or
validated.
[iterator](): Generator<never, Map<number, object[]>, void>
View sourceProvides the scene iterator protocol method.
This generator yields no values. Its first next() call completes with
Scene#entities as the completion value, equivalent to
{ value: scene.entities, done: true }. Consequently, for...of, spread,
and other iterable consumers see no entity entries; use
scene.entities directly to iterate the stored snapshots.
