Archetypes

View source

Registers Archetype instances under zero-based ArchetypeId values in insertion order.

Use Archetypes#set to append an archetype, Archetypes#get to retrieve one by ID, and Archetypes#has or Archetypes#getArchetypeWithOnly to search by component TypeIds.

Examples

import { Archetype, Archetypes } from '@wimaengine/ecs'
import { typeid } from '@wimaengine/type'

class Position {}
class Velocity {}

const archetypes = new Archetypes()
const position = typeid(Position)
const velocity = typeid(Velocity)
const id = archetypes.set(new Archetype(0, [position, velocity]))

archetypes.has([position]) // true: subset lookup allows velocity too
archetypes.getArchetypeWithOnly([position, velocity]) // [id, Archetype]

Constructor


constructor()

Methods


has(types: TypeId[]): boolean

View source

Checks whether any registered archetype contains every requested TypeId.

This is subset lookup through Archetype#has: an archetype may contain additional component types. The method returns only a boolean; use Archetypes#getArchetypeWithOnly when the matching archetype and its ID are needed.

Parameters

NameDescriptionDefault
types

Component TypeIds that must all be present.

—

Returns

true when at least one registered archetype contains the requested types.

get(id: ArchetypeId): Archetype

View source

Returns the archetype stored at an allocated ID.

IDs are zero-based insertion indexes assigned by Archetypes#set. An ID that has not been allocated returns undefined because the lookup reads the backing array directly.

Parameters

NameDescriptionDefault
id

Zero-based archetype ID to look up.

—

Returns

The archetype at id, or undefined when no entry exists there.

set(archetype: Archetype): ArchetypeId

View source

Appends an archetype and assigns its next zero-based ID.

Every call appends the supplied object, even when an equivalent archetype is already registered. The returned ID is the new entry’s index and is also the value accepted by Archetypes#get.

Parameters

NameDescriptionDefault
archetype

Archetype record to append to the registry.

—

Returns

The newly allocated zero-based ID.

values(): readonly Archetype[]

View source

Returns the live, ID-ordered collection of registered archetypes.

The returned collection is the registry’s backing array, typed as read-only. Entries added by Archetypes#set are visible through the same returned reference, so callers should treat it as read-only rather than modifying it directly.

Returns

The live archetype collection.

getArchetypeWithOnly(typeIds: TypeId[]): [ArchetypeId, Archetype]

View source

Finds the first archetype whose component TypeIds exactly match the input.

Matching delegates to Archetype#hasOnly: the lists must have the same length and contain the same TypeIds, regardless of order. Pass a deduplicated set of TypeIds, as the exact check uses length plus membership. The search follows allocation order and returns the matching ID together with its archetype.

Parameters

NameDescriptionDefault
typeIds

Complete, deduplicated component TypeId set to match.

—

Returns

The first matching ID and archetype, or undefined when no exact set is registered.