constructor()
View sourceCreates an empty world and registers EntityHandle as its built-in
entity component type.
Class
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.
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()Creates an empty world and registers EntityHandle as its built-in
entity component type.
getEntities(): Readonly<Entities>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[]]): EntityHandleCreates 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.
insert<T extends object[]>(entity: EntityHandle, components: [...T[]]): voidAdds 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.
remove<T extends unknown[]>(entity: EntityHandle, components: TupleConstructor<T>): voidRemoves 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.
createMany<T extends object[]>(entities: [...T[]][]): voidCreates 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.
despawn(entity: EntityHandle): voidRemoves 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.
get<T>(entity: EntityHandle, type: Constructor<T>): TReads 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.
getEntity(entity: EntityHandle): EntityCellCreates an EntityCell view for an entity handle.
A stale or unknown handle produces an invalid cell. Check
EntityCell#exists before reading from that cell.
getTables(): TablesReturns 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.
getResources(): ReadonlyMap<TypeId, unknown>Returns the world’s resource map keyed by resource type ID.
This low-level view does not resolve aliases; use
World#getResource or World#getOptionalResource for
typed lookup.
getResourceAliases(): ReadonlyMap<TypeId, TypeId>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.
getArchetypes(): ArchetypesReturns 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.
getResource<T>(resourceType: Constructor<T>): TLooks 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.
getOptionalResource<T>(resourceType: Constructor<T>): TLooks 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.
getResourceByTypeId<T>(id: TypeId): TLooks 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.
getOptionalResourceByTypeId<T>(id: TypeId): TLooks 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.
hasResource<T>(resourceType: Constructor<T>): booleanChecks whether a resource or alias resolves to a stored resource value.
The constructor is converted to its type ID before direct or alias lookup.
hasResourceByTypeId<T>(id: TypeId): booleanChecks whether a raw resource or alias type ID resolves to a stored value.
setResourceByTypeId<T>(id: TypeId, resource: T): voidStores 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.
setResource<T extends object>(resource: T): voidStores 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.
removeResource<T>(type: Constructor<T>): voidRemoves 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.
removeResourceByTypeId(typeId: TypeId): voidRemoves 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.
setResourceAlias<T>(id: TypeId, alias: Constructor<T>): voidRegisters 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.
setComponentHooks<T>(component: Constructor<T>, hooks: ComponentHooks): voidInstalls 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.
clear(): voidClears 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.