rhodonite
    Preparing search index...

    Class EntityRepository

    The repository class responsible for creating, managing, and deleting entities within the framework. Provides entity lifecycle management, component attachment, and entity querying capabilities.

    This class manages entity UIDs, tracks entity relationships, and handles component associations. It also provides functionality for entity copying and cleanup operations.

    Index

    Constructors

    Properties

    _components: Map<number, Component>[] = []

    Accessors

    • get updateCount(): number

      Gets the current update count for the entity repository.

      Returns number

      The current update count

      This counter increments whenever entities are created, deleted, or have components added/removed. Useful for tracking repository state changes and implementing cache invalidation strategies.

    Methods

    • Internal

      Gets all currently alive entities in the repository.

      Returns IEntity[]

      Array of all alive entities

      This method returns a filtered array containing only entities that are not null and are marked as alive. This is an internal method used for repository management and debugging.

    • Internal

      Internal method that performs the core shallow copying logic for an entity.

      Parameters

      • entity: IEntity

        The entity to shallow copy

      Returns IEntity

      A new entity that is a shallow copy of the input entity

      This method creates a new entity, copies tags, and shallow copies all components from the original entity. It processes all well-known component types.

    • Adds a specific component class to an entity and returns the enhanced entity.

      Type Parameters

      • ComponentType extends typeof Component

        The component class type

      • EntityType extends IEntity

        The entity type

      Parameters

      • componentClass: ComponentType

        The component class to instantiate and add

      • entity: EntityType

        The entity to add the component to

      Returns EntityType & ComponentToComponentMethods<ComponentType>

      The entity enhanced with the component's methods

      This method creates a new component instance, associates it with the entity, and returns the entity with enhanced type information that includes the component's methods. If the entity already has this component, a warning is logged and the entity is returned unchanged.

      const entity = EntityRepository.createEntity();
      const enhancedEntity = EntityRepository.addComponentToEntity(
      TransformComponent,
      entity
      );
      // enhancedEntity now has transform-related methods
    • Creates a new entity with a unique entity UID.

      Returns IEntity

      A newly created entity with a unique UID

      This method attempts to reuse UIDs from previously deleted entities to optimize memory usage. If no dead entities exist, it generates a new UID by incrementing the counter.

      const entity = EntityRepository.createEntity();
      console.log(entity.entityUID); // Output: unique entity ID
    • Deletes an entity and all its associated components.

      Parameters

      • entityUid: number

        The unique identifier of the entity to delete

      Returns void

      When deleting an entity with a SceneGraph component, this method automatically deletes all child entities recursively to maintain scene graph integrity.

      const entity = EntityRepository.createEntity();
      EntityRepository.deleteEntity(entity.entityUID);
    • Recursively deletes an entity and all its descendant entities in the scene graph hierarchy.

      Parameters

      • entityUid: number

        The unique identifier of the root entity to delete recursively

      Returns void

      This method traverses the entire scene graph tree starting from the specified entity and deletes all entities in the hierarchy. Use this when you want to remove an entire subtree from the scene graph.

      // Delete a parent entity and all its children
      EntityRepository.deleteEntityRecursively(parentEntity.entityUID);
    • Retrieves a specific component from an entity.

      Parameters

      • entityUid: number

        The unique identifier of the entity

      • componentType: typeof Component

        The component class to retrieve

      Returns Component | null

      The component instance if found, null otherwise

      This method looks up a component of the specified type from the given entity. Returns null if the entity doesn't exist or doesn't have the requested component.

      const transform = EntityRepository.getComponentOfEntity(
      entityUID,
      TransformComponent
      );
      if (transform) {
      // Use the transform component
      }
    • Gets the total count of currently alive entities.

      Returns number

      The number of currently alive entities

      This method counts only entities that are not null and are marked as alive. Useful for performance monitoring and debugging entity lifecycle issues.

      console.log(`Active entities: ${EntityRepository.getEntitiesNumber()}`);
      
    • Instance method to retrieve an entity by its unique identifier.

      Parameters

      • entityUid: number

        The unique identifier of the entity to retrieve

      Returns IEntity

      The entity corresponding to the given UID

      This is an instance method equivalent of the static getEntity method. Useful when working with EntityRepository instances.

      Will throw if the entity with the given UID does not exist

    • Retrieves an entity by its unique name identifier.

      Parameters

      • uniqueName: string

        The unique name of the entity to find

      Returns IEntity | undefined

      The entity with the specified unique name, or undefined if not found

      This method searches through all entities to find one with the specified unique name. Returns undefined if no entity with that name is found.

      const player = EntityRepository.getEntityByUniqueName('Player');
      if (player) {
      // Found the player entity
      }
    • Removes a component from an entity and cleans up associated resources.

      Parameters

      • componentClass: typeof Component

        The component class to remove

      • entity: IEntity

        The entity to remove the component from

      Returns IEntity

      The entity with the component removed (typed as IEntity)

      This method removes the specified component from the entity, calls the component's destroy method, and returns the entity with basic IEntity typing. You may need to cast the returned entity to the appropriate type after component removal.

      const entity = EntityRepository.createEntity();
      // Add and later remove a component
      EntityRepository.removeComponentFromEntity(TransformComponent, entity);
    • Searches for entities that match all of the specified tags.

      Parameters

      • tags: RnTags

        The tags to search for (all must match)

      Returns IEntity[]

      Array of entities that match all specified tags

      This method performs a tag-based search across all entities in the repository. Only entities that match ALL provided tags will be included in the results.

      const entities = EntityRepository.searchByTags({
      category: 'weapon',
      type: 'sword'
      });
    • Creates a shallow copy of an entity, including all its components and tag data.

      Parameters

      • entity: IEntity

        The entity to shallow copy

      Returns IEntity

      A new entity that is a shallow copy of the input entity

      This method performs a shallow copy of the entity, its components, and handles special cases like skeletal joints and tag references. The original entity's _myLatestCopyEntityUID will be updated to reference the new entity.

      const originalEntity = EntityRepository.createEntity();
      const copiedEntity = EntityRepository.shallowCopyEntity(originalEntity);
    • Attempts to add a component to an entity using the component's type ID.

      Parameters

      • componentTID: number

        The component type identifier

      • entity: IEntity

        The entity to add the component to

      Returns IEntity

      The entity (possibly with the new component added)

      This method looks up the component class by its TID and adds it to the entity if the component class exists. If the component class is not found, the entity is returned unchanged.

      const entity = EntityRepository.createEntity();
      EntityRepository.tryToAddComponentToEntityByTID(
      WellKnownComponentTIDs.TransformComponentTID,
      entity
      );