Parent

View source

Links an entity to one parent entity in a hierarchy.

Attach Parent to a child entity to identify the entity that owns it in the hierarchy. The relationship is represented by an EntityHandle to the parent, and the component implements TraverseEntities for relationship traversal.

Use Children on a parent entity for its ordered direct-child relationship. Register HierarchyPlugin to keep the parent and child relationships synchronized.

Examples

import { World } from '@wimaengine/ecs'
import { Parent } from '@wimaengine/hierarchy'

const world = new World()
const root = world.spawn([])
const child = world.spawn([new Parent(root)])
const parent = world.get(child, Parent)

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

Constructor


constructor(entity: EntityHandle)

View source

Creates a parent relationship for an entity.

The supplied handle is stored directly, so the component and caller share the same EntityHandle object.

Parameters

NameDescriptionDefault
entity

Handle of the parent entity.

—

Properties


entity: EntityHandle

View source

Handle of the entity that owns this entity in the hierarchy.

This is the same handle supplied to the constructor or assigned by Parent.copy. It is a value containing an entity slot and generation; reading this field does not query a world or guarantee that the entity is alive.

Methods


visit(): EntityHandle[]

View source

Returns the parent as the only relationship target.

The returned array is newly created for each call, but its element is the component’s current entity handle.

Returns

A one-element array containing the parent handle.

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

View source

Remaps this relationship to an entity in a copied or restored world.

The current packed handle id is used as the map key. The mapped packed id is then passed to EntityHandle.from, and entity is replaced with the newly reconstructed handle. The map must contain an entry for the current parent id; without one, handle reconstruction fails.

Parameters

NameDescriptionDefault
entityMap

Packed source-to-target entity ids.

—

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

View source

Copies a parent relationship into a component.

The target receives the source’s EntityHandle by reference. This method does not create a second handle object; use Parent#map when the relationship must be remapped to another entity identity.

Parameters

NameDescriptionDefault
source

Component to copy.

—
target?

Component to populate.

...

Returns

The populated target component.

staticclone(target: Parent): Parent

View source

Creates a shallow clone of a parent relationship.

The returned component shares the original component’s EntityHandle object because cloning delegates to Parent.copy.

Parameters

NameDescriptionDefault
target

Component to clone.

—

Returns

A new component sharing the source handle.

staticserialize(value: Parent): number

View source

Serializes a parent relationship to a packed entity identity.

This returns the number from EntityHandle#id. It is different from EntityHandle.serialize, which returns an object containing index and generation.

Parameters

NameDescriptionDefault
value

Component to serialize.

—

Returns

Packed numeric parent entity id.

staticdeserialize(value: number, out?: Parent): Parent

View source

Deserializes a packed entity identity into a parent component.

If out is provided, its existing entity handle is updated in place by EntityHandle.deserialize, and the same component is returned. Without an output component, a Parent with a placeholder handle is created first and that handle is populated.

Parameters

NameDescriptionDefault
value

Packed numeric parent entity id.

—
out?

Component to populate and return.

...

Returns

The populated component.

staticvalidateSerial(value: unknown): value is number

View source

Checks whether a value has the supported serialized representation.

This delegates to EntityHandle.validateSerial, which currently accepts any JavaScript number. It does not verify that the packed id maps to a live entity or to a particular world.

Parameters

NameDescriptionDefault
value

Value to validate.

—

Returns

Whether value is numeric.