The EntityManager is responsible for managing the lifecycle and relationships of entities and components.
It provides methods for creating, reading, updating and deleting entities and their associated components.
Handles parent-child relationships between entities and enables/disables entities and components.
Acts as the central registry for all game objects and their behaviors.

// Create an entity with components
const entity = entityManager.createEntity([
new Transform({position: new Vector2(100, 100)}),
new SpriteRenderer({sprite: "player.png"})
]);

// Get a component from an entity
const transform = entityManager.getComponent(entity, Transform);

// Get all components from an entity
const components = entityManager.getComponents(entity);

// Find entity by component
const entityWithSprite = entityManager.getEntityForComponent(spriteRenderer);

// Update component data
entityManager.updateComponentData(entity, Transform, (component) => {
component.position = new Vector2(200, 200);
});

// Create parent-child relationship
const child = entityManager.createEntity([Transform], parent);
// or if you already have the child entity
entityManager.setParent(child, parent);

// Enable/disable entities
entityManager.disableEntity(entity);
entityManager.enableEntity(entity);

// Remove components
entityManager.removeComponent(entity, SpriteRenderer);
entityManager.removeComponent(spriteRenderer);

// Search for entities with specific components
const searchResult = entityManager.search(SpriteRenderer);
searchResult.forEach(({component, entity}) => {
// do something with the component and entity
})

const searchResult = entityManager.search(Enemy, (component) => component.status === "alive");
searchResult.forEach(({component, entity}) => {
// do something with the component and entity
})

Constructors

Methods

  • Adds a component to the entity

    Type Parameters

    Parameters

    • entity: number

      The Entity to which the component will be added

    • componentType: ComponentType<T>

      The class of the component

    Returns T

    The instance of the component

    const spriteRenderer = entityManager.addComponent(entity, SpriteRenderer);
    
  • Adds an instance of a component to an entity

    Type Parameters

    Parameters

    • entity: number

      The Entity to which the component will be added

    • component: T

      The instance of the component

    Returns T

    The instance of the component

    const spriteRenderer = new SpriteRenderer();
    entityManager.addComponent(entity, spriteRenderer);
  • Creates an entity without component

    Returns number

    The created Entity

    const entity = entityManager.createEntity();
    
  • Creates an Entity with the given components.
    Since the components are not cloned, the collection of components must not be reused.

    Parameters

    • components: (Component | ComponentType)[]

      A collection of component instances and component classes

    • Optionalparent: number

      The parent entity (optional)

    Returns number

    The created Entity

    const entity = entityManager.createEntity([
    new Transform({position: new Vector2(100, 100)}),
    SpriteRenderer
    ]);
  • Creates an Entity based on an Archetype.
    Since the components are cloned, the archetype can be reused to create multiple entities.

    Parameters

    • archetype: Archetype

      The archetype to create the entity from

    • Optionalparent: number

      The parent entity (optional)

    Returns number

    The created Entity

    const archetype = {
    components: [
    new Transform({position: new Vector2(100, 100)}),
    SpriteRenderer
    ],
    children: [childArchetype],
    enabled: true
    }
    const entity = entityManager.createEntityFromArchetype(archetype);
  • Disables a component instance

    Parameters

    Returns void

    entityManager.disableComponent(spriteRenderer);
    
  • Disables a component by its entity and type

    Type Parameters

    Parameters

    • entity: number

      The target entity

    • componentType: ComponentType<T>

      The component class

    Returns void

    entityManager.disableComponent(entity, SpriteRenderer);
    
  • Disable all Entities that have a component of the given type

    Parameters

    Returns void

    entityManager.disableEntitiesByComponent(SpriteRenderer);
    
  • Disables an entity.
    The entity's components will not be included in the search results
    and therefore will not be processed by their respective systems
    (the engine built-in systems use the search method to retrieve the entities). If the entity has children, they will also be disabled.

    Parameters

    • entity: number

      The entity to be disabled

    Returns void

    entityManager.disableEntity(entity);
    
  • Enables a component instance

    Parameters

    Returns void

    entityManager.enableComponent(spriteRenderer);
    
  • Enables a component by its entity and type

    Type Parameters

    Parameters

    • entity: number

      The target entity

    • componentType: ComponentType<T>

      The component class

    Returns void

    entityManager.enableComponent(entity, SpriteRenderer);
    
  • Enable all Entities that have a component of the given type

    Parameters

    Returns void

    entityManager.enableEntitiesByComponent(SpriteRenderer);
    
  • Enables an Entity.
    The entity's components will be included in the search results
    and therefore will be processed by their respective systems
    (the engine built-in systems use the search method to retrieve the entities). If the entity has children, they will also be enabled, except for those that have been manually disabled.

    Parameters

    • entity: number

      The entity to be enabled

    Returns void

    entityManager.enableEntity(entity);
    
  • Returns all child entities of the parent entity

    Parameters

    • parent: number

    Returns number[]

    A collection of child entities

    const children = entityManager.getChildren(parent);
    
  • Returns the component of the given type belonging to the entity

    Type Parameters

    Parameters

    • entity: number

      The entity

    • componentType: ComponentType<T>

      The class of the component

    Returns T

    The instance of the component

    const spriteRenderer = entityManager.getComponent(entity, SpriteRenderer);
    
  • Returns the component of a parent entity, if it exists.

    Type Parameters

    Parameters

    • parent: number

      The parent entity

    • componentType: ComponentType<T>

      The component class to search

    Returns T

    The instance of the component

    const spriteRenderer = entityManager.getComponentFromParent(parent, SpriteRenderer);
    
  • Returns all the component belonging to the entity

    Parameters

    • entity: number

      The entity

    Returns Component[]

    A collection of component instances

    const components = entityManager.getComponents(entity);
    
  • Searches for and returns an entity for the component instance

    Parameters

    Returns number

    The found Entity

    const entity = entityManager.getEntityForComponent(spriteRenderer);
    
  • Returns the parent entity of the child entity

    Parameters

    • child: number

    Returns number

    The parent entity

    const parent = entityManager.getParent(child);
    
  • Returns TRUE if the Entity has a component of the given type, otherwise it returns FALSE.

    Parameters

    • entity: number

      The entity to verify

    • componentType: ComponentType

      The class of the component

    Returns boolean

    boolean

    const hasSpriteRenderer = entityManager.hasComponent(entity, SpriteRenderer);
    
  • Returns TRUE if the component is enabled, otherwise it returns FALSE

    Parameters

    Returns boolean

    boolean

    entityManager.isComponentEnabled(spriteRenderer)
    
  • Returns TRUE if the component is enabled, otherwise it returns FALSE

    Type Parameters

    Parameters

    • entity: number

      The target entity

    • componentType: ComponentType<T>

      The component class

    Returns boolean

    boolean

    entityManager.isComponentEnabled(entity, SpriteRenderer)
    
  • If the Entity exists, returns TRUE, otherwise it returns FALSE

    Parameters

    • entity: number

    Returns boolean

    boolean

    const isEntity = entityManager.isEntity(entity);
    
  • If the Entity is enabled, returns TRUE, otherwise it returns FALSE

    Parameters

    • entity: number

      The entity to verify

    Returns boolean

    boolean

    const entityEnabled = entityManager.isEntityEnabled(entity);
    
  • Removes all Entities and all their Components

    Parameters

    • OptionalpreserveComponentType: ComponentType

      Optional component type to preserve entities that have this component

    Returns void

    // Remove all entities
    entityManager.removeAllEntities();

    // Remove all entities except those that have a SpriteRenderer component
    entityManager.removeAllEntities(SpriteRenderer);
  • Removes a child entity from the parent entity

    Parameters

    • parent: number

      The parent entity

    • child: number

      The child entity

    Returns void

    entityManager.removeChild(parent, child);
    
  • Removes the component instance from its Entity

    Parameters

    Returns void

    entityManager.removeComponent(spriteRenderer)
    
  • Removes a component from the entity according to the given type

    Parameters

    • entity: number

      The target entity

    • componentType: ComponentType

      The component class

    Returns void

    entityManager.removeComponent(entity, SriteRenderer)
    
  • Removes an Entity and all its Components

    Parameters

    • entity: number

      The entity to remove

    Returns void

    entityManager.removeEntity(entity);
    
  • Removes the parent-child relationship between two entities

    Parameters

    • child: number

      The child entity

    Returns void

    entityManager.removeParent(child);
    
  • Performs a search for entities given a component type.
    This method returns a collection of objects of type SearchResult, which has the entity found, and the instance of the component.
    This search can be filtered by passing as a second argument a filter function.
    The third argument determines if disabled entities or components are included in the search result,\its default value is FALSE.

    Type Parameters

    Parameters

    • componentType: ComponentType<T>

      The component class

    • Optionalfilter: ((component: T, entity?: number) => boolean)

      The filter function

        • (component, entity?): boolean
        • Parameters

          • component: T
          • Optionalentity: number

          Returns boolean

    • OptionalincludeDisabled: boolean

      TRUE to incluide disabled entities and components. Default is FALSE.

    Returns SearchResult<T>[]

    SearchResult

    const searchResult = entityManager.search(SpriteRenderer);
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
    const searchResult = entityManager.search(Enemy, (component) => component.status === "alive");
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
    // include disabled entities and components
    const searchResult = entityManager.search(Enemy, (component) => component.status === "dead", true);
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
  • Performs a search for entities given a component type.
    This method returns a collection of objects of type SearchResult, which has the entity found, and the instance of the component.
    The second argument determines if disabled entities or components are included in the search result,\its default value is FALSE.

    Type Parameters

    Parameters

    • componentType: ComponentType<T>

      The component class

    • OptionalincludeDisabled: boolean

      TRUE to incluide disabled entities and components. Default is FALSE.

    Returns SearchResult<T>[]

    SearchResult

    const searchResult = entityManager.search(SpriteRenderer);
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
    // include disabled entities and components
    const searchResult = entityManager.search(Enemy, true);
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
  • Performs an entity search given a collection of component types.
    The entities found must have an instance of all the given component types.
    This method returns a collection of Entities.

    Parameters

    • componentTypes: ComponentType[]

      A collection of component classes

    Returns number[]

    A collection of entities

    const entities = entityManager.searchEntitiesByComponents([Transform, SpriteRenderer, Animator]);
    
  • Performs a search for entities that have a component of the given type and are children of the parent entity.
    This method returns a collection of objects of type SearchResult, which has the entity found, and the instance of the component.
    The third argument determines if disabled entities or components are included in the search result,\its default value is FALSE.

    Type Parameters

    Parameters

    • parent: number

      The parent entity

    • componentType: ComponentType<T>

      The component class

    • includeDisabled: boolean = false

      TRUE to incluide disabled entities and components. Default is FALSE.

    Returns SearchResult<T>[]

    SearchResult

    const searchResult = entityManager.searchInChildren(parent, SpriteRenderer);
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
    const searchResult = entityManager.searchInChildren(parent, SpriteRenderer, true);
    searchResult.forEach(({component, entity}) => {
    // do something with the component and entity
    })
  • Sets a parent-child relationship between two entities

    Parameters

    • child: number

      The child entity

    • parent: number

      The parent entity

    Returns void

    entityManager.setParent(child, parent);
    
  • Updates the data of a component instance

    Type Parameters

    Parameters

    • entity: number

      The entity

    • componentType: ComponentType<T>

      The component type

    • updateCallback: ((component: T) => void)

      The callback to update the component

        • (component): void
        • Parameters

          • component: T

          Returns void

    Returns void

    entityManager.updateComponentData(entity, Transform, (component) => {
    component.position = new Vector2(100, 100);
    });