World

View source

Represents an ECS world containing entities, their component values, and world-scoped resources.

Create entities with World#spawn, change their component sets with World#insert and World#remove, and read them directly or through a Query. Use the resource methods for world-scoped state and World#setComponentHooks to install lifecycle callbacks for component types.

Examples

import { ComponentHooks, Query, World } from '@wimaengine/ecs'

class Position {}
class Health {}

const world = new World()
world.setComponentHooks(Position, new ComponentHooks(() => {
  console.log('position added')
}))

const entity = world.spawn([new Position()])
world.insert(entity, [new Health()])
const position = world.get(entity, Position)

const query = new Query(world, [Position])
query.each(([currentPosition]) => console.log(currentPosition))

Constructor


Methods


getEntities(): Readonly<Entities>

View source

Returns the world’s entity-location registry.

This low-level view supports entity helpers. Entity validity still depends on matching an EntityHandle index and generation.

spawn<T extends object[]>(components: [...T[]]): EntityHandle

View source

Creates an entity with the supplied component instances.

The returned EntityHandle identifies the new entity by slot index and generation. The handle is also stored as an EntityHandle component, and this method appends it to the supplied components array. Registered component add hooks run synchronously after the row is stored.

Type Parameters

T extends object[]

Parameters

NameDescriptionDefault
components

Component instances to store; the array is appended with the entity handle.

—

Returns

The new entity’s generation-safe handle.

insert<T extends object[]>(entity: EntityHandle, components: [...T[]]): void

View source

Adds or replaces component values on a live entity.

A stale handle makes this method return without changing the world. The supplied component instances are stored before their registered add hooks run. Registered insert hooks then run for the component types that were already present, even when the supplied types use the entity’s existing table. The method returns undefined.

Type Parameters

T extends object[]

Parameters

NameDescriptionDefault
entity——
components

Component instances to add or replace.

—

remove<T extends unknown[]>(entity: EntityHandle, components: TupleConstructor<T>): void

View source

Removes component types from a live entity.

A stale handle makes this method return without changing the world. Remove hooks run synchronously before the entity row moves, followed by insert hooks for the types that remain in the new layout. A requested type that is not present changes no storage, but its registered remove hook is still called. The method returns undefined.

Type Parameters

T extends unknown[]

Parameters

NameDescriptionDefault
entity——
components

Component constructors to remove.

—

createMany<T extends object[]>(entities: [...T[]][]): void

View source

Creates one entity for every component list in entities.

This calls World#spawn for each list in order. Each input list is therefore appended with its entity handle, and the handles are not returned by this method.

Type Parameters

T extends object[]

Parameters

NameDescriptionDefault
entities

Component lists to pass to World#spawn.

—

despawn(entity: EntityHandle): void

View source

Removes an entity and all of its components from the world.

Remove hooks run synchronously while the entity row still exists, before the row is deleted. A stale handle is ignored. The entity’s slot is then recycled; a later World#spawn may reuse its index with a new generation, so the old EntityHandle cannot access the replacement.

Parameters

NameDescriptionDefault
entity

The entity to remove.

—

get<T>(entity: EntityHandle, type: Constructor<T>): T

View source

Reads a component from an entity handle.

Returns null when the index is unknown or the handle’s generation is stale. Returns undefined when the handle matches a location that has no table, including immediately after World#despawn, or when the live entity does not have the requested component.

Type Parameters

T

Parameters

NameDescriptionDefault
entity

Handle identifying the entity to read.

—
type

Component constructor to read.

—

Returns

The component, or the missing/stale sentinel.

getEntity(entity: EntityHandle): EntityCell

View source

Creates an EntityCell view for an entity handle.

A stale or unknown handle produces an invalid cell. Check EntityCell#exists before reading from that cell.

Parameters

NameDescriptionDefault
entity

Handle identifying the entity to view.

—

Returns

A cell backed by the matching entity location, when one exists.

getTables(): Tables

View source

Returns the world’s low-level table store.

Queries and entity cells use this store to read component rows. Mutating the returned store bypasses the world’s entity-location bookkeeping.

Returns

The table store.

getResourceAliases(): ReadonlyMap<TypeId, TypeId>

View source

Returns the resource alias map.

Each key is an alias type ID and each value is the resource type ID used for the underlying lookup.

Returns

Alias-to-resource type IDs.

getArchetypes(): Archetypes

View source

Returns the world’s low-level archetype store.

Archetypes describe the exact component layouts used by the table store. Mutating the returned store bypasses the world’s entity-location bookkeeping.

Returns

The archetype store.

getResource<T>(resourceType: Constructor<T>): T

View source

Looks up a resource by its constructor or a registered alias constructor.

A missing resource or alias causes an assertion error. Use World#getOptionalResource when absence is a valid state.

Type Parameters

T

Parameters

NameDescriptionDefault
resourceType

Resource or alias constructor.

—

Throws

If the resource or alias is not available.

Returns

The stored resource instance.

getOptionalResource<T>(resourceType: Constructor<T>): T

View source

Looks up a resource by its constructor or a registered alias constructor without asserting when it is absent.

An alias resolves to the resource stored under the type ID supplied to World#setResourceAlias.

Type Parameters

T

Parameters

NameDescriptionDefault
resourceType

Resource or alias constructor.

—

Returns

The resource, or undefined when it is missing.

getResourceByTypeId<T>(id: TypeId): T

View source

Looks up a resource by raw type ID and asserts when it is unavailable.

The ID may be a resource type ID or an alias type ID. This is the raw-ID form of World#getResource.

Type Parameters

T

Parameters

NameDescriptionDefault
id

Resource or alias type ID.

—

Throws

If the resource or alias is not available.

Returns

The stored resource instance.

getOptionalResourceByTypeId<T>(id: TypeId): T

View source

Looks up a resource by raw type ID without asserting when it is absent.

If id is an alias type ID, the alias is followed to the resource type ID stored in the world. Missing IDs and aliases return undefined.

Type Parameters

T

Parameters

NameDescriptionDefault
id

Resource or alias type ID.

—

Returns

The resource, or undefined when it is missing.

hasResource<T>(resourceType: Constructor<T>): boolean

View source

Checks whether a resource or alias resolves to a stored resource value.

The constructor is converted to its type ID before direct or alias lookup.

Type Parameters

T

Parameters

NameDescriptionDefault
resourceType

Resource or alias constructor.

—

Returns

Whether a resource value is available.

hasResourceByTypeId<T>(id: TypeId): boolean

View source

Checks whether a raw resource or alias type ID resolves to a stored value.

Type Parameters

T

Parameters

NameDescriptionDefault
id

Resource or alias type ID.

—

Returns

Whether a resource value is available.

setResourceByTypeId<T>(id: TypeId, resource: T): void

View source

Stores a resource value under an explicit type ID.

An existing value under the same ID is replaced. This method does not create or validate resource aliases.

Type Parameters

T

Parameters

NameDescriptionDefault
id

Type ID used as the storage key.

—
resource

Resource value to store.

—

setResource<T extends object>(resource: T): void

View source

Stores a resource under the type ID of its constructor.

An existing resource with the same constructor type ID is replaced. Use World#setResourceByTypeId for an explicit ID.

Type Parameters

T extends object

Parameters

NameDescriptionDefault
resource

Resource instance to store.

—

removeResource<T>(type: Constructor<T>): void

View source

Removes the resource stored under a constructor’s type ID.

This also deletes an alias entry whose key is the same type ID. It does not remove other aliases that point to the resource ID; those aliases simply stop resolving until that resource is stored again.

Type Parameters

T

Parameters

NameDescriptionDefault
type

Resource constructor whose value is removed.

—

removeResourceByTypeId(typeId: TypeId): void

View source

Removes a resource and alias entry stored under a raw type ID.

Aliases whose keys differ from typeId are retained, even when they point to the removed resource ID.

Parameters

NameDescriptionDefault
typeId——

setResourceAlias<T>(id: TypeId, alias: Constructor<T>): void

View source

Registers an alias constructor for a resource type ID.

The alias may be registered before the resource exists. A later alias registration with the same alias constructor replaces the previous target.

Type Parameters

T

Parameters

NameDescriptionDefault
id

Type ID of the resource the alias names.

—
alias

Constructor used to look up the resource.

—

setComponentHooks<T>(component: Constructor<T>, hooks: ComponentHooks): void

View source

Installs lifecycle hooks for a component type in this world.

The component is registered in the world’s type store automatically. A later call for the same component replaces its previous ComponentHooks value. The hooks run synchronously during entity mutations; see ComponentHooks for each callback’s timing.

Type Parameters

T

Parameters

NameDescriptionDefault
component

Component constructor to configure.

—
hooks

Lifecycle callbacks for the component.

—

clear(): void

View source

Clears the world’s table list.

This low-level operation does not clear resources, resource aliases, archetypes, component metadata, or entity locations. Existing entity handles therefore no longer have readable table rows after this call, and callers should treat the world as requiring reinitialization rather than as a fully reset resource container.