1681-engine / index / World
Class: World ​
Defined in: src/ecs/World.ts:86
Constructors ​
Constructor ​
new World(): World;Returns ​
World
Accessors ​
size ​
Get Signature ​
get size(): number;Defined in: src/ecs/World.ts:149
Live entities.
Returns ​
number
Methods ​
add() ​
Call Signature ​
add<S>(
e,
type,
values?
): number;Defined in: src/ecs/World.ts:169
Gives an entity a field component; fields left out start at 0. Returns the slot index into its columns.
Type Parameters ​
| Type Parameter |
|---|
S extends FieldSpec |
Parameters ​
| Parameter | Type |
|---|---|
e | number |
type | FieldsType<S> |
values? | FieldValues<S> |
Returns ​
number
Call Signature ​
add<T>(
e,
type,
value
): T;Defined in: src/ecs/World.ts:170
Gives an entity a field component; fields left out start at 0. Returns the slot index into its columns.
Type Parameters ​
| Type Parameter |
|---|
T |
Parameters ​
| Parameter | Type |
|---|---|
e | number |
type | ComponentType<T> |
value | T |
Returns ​
T
addSystem() ​
addSystem(s): this;Defined in: src/ecs/World.ts:343
Parameters ​
| Parameter | Type |
|---|---|
s | System |
Returns ​
this
clear() ​
clear(): void;Defined in: src/ecs/World.ts:376
Every entity, component and system gone.
Returns ​
void
columns() ​
columns<S>(type): Columns<S>;Defined in: src/ecs/World.ts:189
The typed-array columns of a field component, indexed by slot. Fetch them per system run: adding entities may grow them into new arrays.
Type Parameters ​
| Type Parameter |
|---|
S extends FieldSpec |
Parameters ​
| Parameter | Type |
|---|---|
type | FieldsType<S> |
Returns ​
Columns<S>
componentsOf() ​
componentsOf(e): ComponentType<unknown>[];Defined in: src/ecs/World.ts:138
The component types e holds, for tools; allocates.
Parameters ​
| Parameter | Type |
|---|---|
e | number |
Returns ​
ComponentType<unknown>[]
count() ​
count(type): number;Defined in: src/ecs/World.ts:220
How many entities hold type.
Parameters ​
| Parameter | Type |
|---|---|
type | ComponentType<unknown> |
Returns ​
number
create() ​
create(): number;Defined in: src/ecs/World.ts:103
A new entity with no components.
Returns ​
number
destroy() ​
destroy(e): void;Defined in: src/ecs/World.ts:154
Removes an entity and every component it had (deferred while a walk is in progress).
Parameters ​
| Parameter | Type |
|---|---|
e | number |
Returns ​
void
each() ​
each<T>(...args): void;Defined in: src/ecs/World.ts:228
Calls fn(entity, ...components) for every live entity holding all of types. Allocates nothing per entity: the form for systems that walk thousands of bodies every step.
Type Parameters ​
| Type Parameter |
|---|
T extends readonly ComponentType<unknown>[] |
Parameters ​
| Parameter | Type |
|---|---|
...args | [...types: T[], (e, ...values) => void] |
Returns ​
void
entities() ​
entities(): number[];Defined in: src/ecs/World.ts:131
Every live entity, for tools (a debugger, a save); allocates, so not for a per-frame path.
Returns ​
number[]
first() ​
first<T>(...types): [number, ...ComponentValues<T>[]] | undefined;Defined in: src/ecs/World.ts:338
The first entity matching the query, or undefined: for singletons (the player, the camera target).
Type Parameters ​
| Type Parameter |
|---|
T extends readonly ComponentType<unknown>[] |
Parameters ​
| Parameter | Type |
|---|---|
...types | T |
Returns ​
[number, ...ComponentValues<T>[]] | undefined
get() ​
get<T>(e, type): T | undefined;Defined in: src/ecs/World.ts:193
Type Parameters ​
| Type Parameter |
|---|
T |
Parameters ​
| Parameter | Type |
|---|---|
e | number |
type | ComponentType<T> |
Returns ​
T | undefined
has() ​
has(e, type): boolean;Defined in: src/ecs/World.ts:205
Parameters ​
| Parameter | Type |
|---|---|
e | number |
type | ComponentType<unknown> |
Returns ​
boolean
isAlive() ​
isAlive(e): boolean;Defined in: src/ecs/World.ts:125
Parameters ​
| Parameter | Type |
|---|---|
e | number |
Returns ​
boolean
query() ​
query<T>(...types): IterableIterator<[number, ...ComponentValues<T>[]]>;Defined in: src/ecs/World.ts:312
Every live entity holding all of types, with those components in the same order. Yields a fresh row per entity: convenient, but each is the form for hot loops.
Type Parameters ​
| Type Parameter |
|---|
T extends readonly ComponentType<unknown>[] |
Parameters ​
| Parameter | Type |
|---|---|
...types | T |
Returns ​
IterableIterator<[number, ...ComponentValues<T>[]]>
remove() ​
remove(e, type): void;Defined in: src/ecs/World.ts:210
Takes one component off an entity (deferred while a walk is in progress).
Parameters ​
| Parameter | Type |
|---|---|
e | number |
type | ComponentType<unknown> |
Returns ​
void
removeSystem() ​
removeSystem(name): void;Defined in: src/ecs/World.ts:349
Parameters ​
| Parameter | Type |
|---|---|
name | string |
Returns ​
void
require() ​
require<T>(e, type): T;Defined in: src/ecs/World.ts:199
The component, or an error naming what is missing: for code that knows the entity has it.
Type Parameters ​
| Type Parameter |
|---|
T |
Parameters ​
| Parameter | Type |
|---|---|
e | number |
type | ComponentType<T> |
Returns ​
T
run() ​
run(
phase,
ctx,
onError?
): void;Defined in: src/ecs/World.ts:357
Runs every system of phase in the order added. A system that throws is reported and the rest still run.
Parameters ​
| Parameter | Type |
|---|---|
phase | SystemPhase |
ctx | Omit<SystemContext, "world"> |
onError? | (system, err) => void |
Returns ​
void
slots() ​
slots(...types): Int32Array;Defined in: src/ecs/World.ts:283
The slot index of every entity holding all of types, as a typed array: the fastest walk there is, made for field components (defineFields), whose columns are indexed by slot.
const p = world.columns(Position), v = world.columns(Velocity);
const slots = world.slots(Position, Velocity);
for (let k = 0; k < slots.length; k++) { const i = slots[k]!; p.x[i]! += v.x[i]! * dt; }The result is cached and rebuilt only when an entity joins or leaves one of those components, so asking every step is cheap. It is a view the world owns: read it, don't keep it past the step, and fetch it again after adding or removing components.
Parameters ​
| Parameter | Type |
|---|---|
...types | ComponentType<unknown>[] |
Returns ​
Int32Array
spawn() ​
spawn(...components): number;Defined in: src/ecs/World.ts:119
A new entity with these components.
Parameters ​
| Parameter | Type |
|---|---|
...components | [ComponentType<unknown>, unknown][] |
Returns ​
number