Class ComponentRepository

The repository class that manages all component classes and their instances. This class provides functionality to register component classes, create component instances, and manage the lifecycle of components within the ECS (Entity-Component-System) architecture.

Constructors

Properties

__componentClasses: Map<number, typeof Component> = ...
invalidComponentSID: -1 = -1

Methods

  • Internal

    Gets an array of all component instances of the specified type. This is an internal method that includes undefined slots in the array.

    Parameters

    • componentClass: typeof Component

      The component class to retrieve instances for

    Returns undefined | Component[]

    Array of component instances with potential undefined elements, or undefined if type not found

  • Internal

    Gets an array of all component instances including deleted/dead components. This internal method provides access to the raw component array with undefined slots.

    Parameters

    • componentClass: typeof Component

      The component class to retrieve instances for

    Returns undefined | Component[]

    Array of component instances including dead components, or undefined if type not found

  • Creates a new component instance for the specified entity. This method handles ComponentSID allocation, including reusing SIDs from deleted components.

    Parameters

    • componentTid: number

      The ComponentTID of the component type to create

    • entityUid: number

      The EntityUID of the entity that will own this component

    • entityRepository: EntityRepository

      Reference to the entity repository for entity management

    Returns Component

    The newly created component instance

    If the component class is not registered or invalid

    const component = ComponentRepository.createComponent(
    MyComponent.componentTID,
    entityUID,
    entityRepository
    );
  • Deletes a component instance from the repository. This marks the component's slot as available for reuse and removes it from the active components.

    Parameters

    • component: Component

      The component instance to delete

    Returns void

    ComponentRepository.deleteComponent(myComponent);
    
  • Unregisters a component class from the repository. This removes the component class associated with the given ComponentTID.

    Parameters

    • componentTID: number

      The ComponentTID of the component class to unregister

    Returns void

    ComponentRepository.deregisterComponentClass(MyComponent.componentTID);
    
  • Retrieves a specific component instance by its class and ComponentSID.

    Parameters

    • componentClass: typeof Component

      The component class to search for

    • componentSid: number

      The ComponentSID of the specific component instance

    Returns undefined | Component

    The component instance, or undefined if not found

    const component = ComponentRepository.getComponent(MyComponent, componentSID);
    
  • Retrieves the component class constructor associated with the specified ComponentTID.

    Parameters

    • componentTid: number

      The ComponentTID to look up

    Returns undefined | typeof Component

    The component class constructor, or undefined if not found

    const ComponentClass = ComponentRepository.getComponentClass(componentTID);
    if (ComponentClass) {
    // Use the component class
    }
  • Retrieves a specific component instance by ComponentTID and ComponentSID.

    Parameters

    • componentTid: number

      The ComponentTID of the component type

    • componentSid: number

      The ComponentSID of the specific component instance

    Returns undefined | Component

    The component instance, or undefined if not found

    const component = ComponentRepository.getComponentFromComponentTID(componentTID, componentSID);
    
  • Retrieves all registered component type IDs in sorted order. This provides access to all ComponentTIDs that have been registered with the repository.

    Returns number[]

    Array of all ComponentTIDs currently registered, sorted in ascending order

    const allComponentTIDs = ComponentRepository.getComponentTIDs();
    console.log(`Total component types: ${allComponentTIDs.length}`);
  • Retrieves all active (non-null) component instances of the specified type. This method filters out deleted components and returns only valid instances.

    Parameters

    • componentType: typeof Component

      The component class to retrieve instances for

    Returns Component[]

    Array of active component instances (never includes undefined elements)

    const activeComponents = ComponentRepository.getComponentsWithType(MyComponent);
    activeComponents.forEach(component => {
    // Process each active component
    });
  • Calculates the memory begin index for a given component type. This is used for memory layout calculations in the component system.

    Parameters

    • componentTid: number

      The ComponentTID to calculate the memory index for

    Returns number

    The starting memory index for the component type

    const memoryIndex = ComponentRepository.getMemoryBeginIndex(componentTID);
    
  • Retrieves all rendering-related component type IDs. This returns ComponentTIDs for components that are involved in the rendering pipeline.

    Returns number[]

    Array of ComponentTIDs for rendering components

    const renderingTIDs = ComponentRepository.getRenderingComponentTIDs();
    // Process rendering components during render loop
  • Registers a component class with the repository. This method associates a component class with its unique ComponentTID for later instantiation.

    Parameters

    • componentClass: typeof Component

      The component class constructor to register

    Returns void

    If the component class is invalid or already registered

    ComponentRepository.registerComponentClass(MyCustomComponent);