Scene

View source

Represents 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


constructor()

Properties


entities: Map<number, object[]>

View source

Stores 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 source

Stores 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 source

Restores 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.

Parameters

NameDescriptionDefault
world——
instance

Instance receiving restored-entity mappings.

—
typeRegistry

Registry used to restore and map snapshots.

—
instanceEntity

Entity used as the default parent.

—
filter?

Optional filter for component and resource snapshot types.

—

staticfromWorld(world: World, typeRegistry: TypeRegistry): Scene

View source

Captures 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.

Parameters

NameDescriptionDefault
world

World whose entities and resources are captured.

—
typeRegistry

Registry used to snapshot values.

—

Returns

A new scene snapshot asset.

staticserializeWithContext(scene: Scene, typeRegistry: TypeRegistry): object

View source

Converts a scene into its registry-aware, JSON-compatible representation.

Parameters

NameDescriptionDefault
scene

Scene snapshot to serialize.

—
typeRegistry

Registry used for snapshot serialization.

—

Returns

JSON-compatible scene representation.

staticdeserializeWithContext(serial: unknown, typeRegistry: TypeRegistry): Scene

View source

Reconstructs a scene from its registry-aware serialized representation.

Parameters

NameDescriptionDefault
serial

Serialized scene representation.

—
typeRegistry

Registry used for snapshot deserialization.

—

Returns

Reconstructed scene.

staticdefault(): Scene

View source

Creates an empty scene asset.

Returns

A new empty scene.

entityCount(): number

View source

Returns 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.

Returns

The number of stored entity entries.

set(entity: EntityHandle, components: object[]): void

View source

Stores 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.

Parameters

NameDescriptionDefault
entity

Entity whose packed ID identifies the entry.

—
components

Component snapshot array to store directly.

—

[iterator](): Generator<never, Map<number, object[]>, void>

View source

Provides 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.

Returns

An empty generator whose completion value is Scene#entities.