Children

View source

Stores an entity’s ordered direct-child relationship.

Attach Children to a parent entity to list its direct children in order. Each entry is an EntityHandle for one child, and the component implements TraverseEntities for relationship traversal.

Use Parent on a child entity for the reverse relationship. Register HierarchyPlugin to keep the parent and child relationships synchronized.

Examples

import { EntityHandle } from '@wimaengine/ecs'
import { Children, Parent } from '@wimaengine/hierarchy'

const root = new EntityHandle(1, 0)
const child = new EntityHandle(2, 0)
const parent = new Parent(root)
const children = new Children([child])

console.log(parent.entity.equals(root)) // true
console.log(children.visit()[0].equals(child)) // true

Constructor


constructor(children?: EntityHandle[])

View source

Creates a children relationship from an optional ordered list.

The supplied array is stored by reference; the constructor does not make a copy. Use Children.copy when an independent list is required.

Parameters

NameDescriptionDefault
children?

Direct child handles in traversal order.

[]

Properties


Methods


staticcopy(source: Children, target?: Children): Children

View source

Copies a children relationship into a target component.

The target receives a new array with the same handles in the same order. Handles themselves are shared; this is a shallow copy of the relationship list. When target is omitted, a new Children is created.

Parameters

NameDescriptionDefault
source

Relationship to copy.

—
target?

Component to populate.

...

Returns

The populated target component.

staticclone(target: Children): Children

View source

Creates a shallow clone with an independent list array.

The cloned component keeps the same entity-handle objects as the source, but changing either component’s list does not change the other list.

Parameters

NameDescriptionDefault
target

Relationship to clone.

—

Returns

The cloned relationship.

staticdeserialize(value: ChildrenSerial, out?: Children): Children

View source

Deserializes packed child identities into a relationship component.

A new EntityHandle is created for each numeric identity with EntityHandle.deserialize, and the original order is preserved. When out is supplied, its list is replaced with a new array; existing handles and the previous list array are not reused.

Parameters

NameDescriptionDefault
value

Packed child identities in list order.

—
out?

Component to populate.

...

Returns

The populated output component.

staticvalidateSerial(value: unknown): value is ChildrenSerial

View source

Checks whether a value has the serialized children shape.

Validation requires an array whose entries pass EntityHandle.validateSerial. It checks only that each entry is a number; it does not check that an identity is finite, currently alive, or present in a particular world.

Parameters

NameDescriptionDefault
value

Value to validate.

—

Returns

Whether the value can be deserialized.

add(entity: EntityHandle): void

View source

Appends a child handle to the end of the ordered list.

No duplicate or liveness check is performed. The method mutates Children#list and returns undefined.

Parameters

NameDescriptionDefault
entity

Handle for the direct child to append.

—

remove(entity: EntityHandle): void

View source

Removes the first matching handle from the list.

Matching uses the array’s strict object identity through indexOf; it does not use EntityHandle#equals. If the exact handle object is not present in a non-empty list, the current implementation passes -1 to splice and removes the last list entry. The method mutates Children#list and returns undefined.

Parameters

NameDescriptionDefault
entity

Exact handle object to remove.

—

visit(): EntityHandle[]

View source

Returns the live ordered list of direct child handles.

No copy is made. Relationship queries use this array for traversal, and changes made through the returned reference affect the component.

Returns

The component’s live child list.

map(entityMap: Map<number, number>): void

View source

Replaces each child handle with its mapped packed entity identity.

For every current handle, the method looks up entityMap.get(handle.id()) and passes the result to EntityHandle.from. Map values must therefore be packed numeric EntityId values, not EntityHandle objects, and every current child ID must have an entry. The list is replaced with a new array of newly constructed handles; the method returns undefined.

Parameters

NameDescriptionDefault
entityMap

Old-to-new packed IDs.

—

clear(): void

View source

Removes all child entries while preserving the backing array.

References previously returned by Children#visit observe the list becoming empty. The method does not despawn entities or modify their Parent components and returns undefined.