constructor(entity: EntityHandle)
View sourceCreates a parent relationship for an entity.
The supplied handle is stored directly, so the component and caller share
the same EntityHandle object.
Class
ImplementsTraverseEntities
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.
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)) // trueconstructor(entity: EntityHandle)Creates a parent relationship for an entity.
The supplied handle is stored directly, so the component and caller share
the same EntityHandle object.
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.
visit(): EntityHandle[]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.
map(entityMap: Map<number, number>): voidRemaps 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.
copy(source: Parent, target?: Parent): ParentCopies 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.
clone(target: Parent): ParentCreates a shallow clone of a parent relationship.
The returned component shares the original component’s EntityHandle
object because cloning delegates to Parent.copy.
default(): Parentserialize(value: Parent): numberSerializes 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.
deserialize(value: number, out?: Parent): ParentDeserializes 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.
validateSerial(value: unknown): value is numberChecks 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.