Class Entity

An entity is composed from components. As such, it is essentially a collection object for components. Sometimes, the entities in a game will mirror the actual characters and objects in the game, but this is not necessary.

Components are simple value objects that contain data relevant to the entity. Entities with similar functionality will have instances of the same components. So we might have a position component

Example

class PositionComponent {
public x:number;
public y:number;
}

All entities that have a position in the game world, will have an instance of the position component. Systems operate on entities based on the components they have.

Hierarchy

  • Entity

Constructors

  • The constructor

    Parameters

    • name: string = ''

      The name for the entity. If left blank, a default name is assigned with the form _entityN where N is an integer.

    Returns Entity

Properties

componentAdded: Signal<[Entity, Class<any>]>

This signal is dispatched when a component is added to the entity.

componentRemoved: Signal<[Entity, Class<any>]>

This signal is dispatched when a component is removed from the entity.

components: Map<Class<any>, any>
nameChanged: Signal<[Entity, string]>

Dispatched when the name of the entity changes. Used internally by the engine to track entities based on their names.

next: null | Entity = null
previous: null | Entity = null

Accessors

  • get name(): string
  • All entities have a name. If no name is set, a default name is used. Names are used to fetch specific entities from the engine, and can also help to identify an entity when debugging.

    Returns string

  • set name(value): void
  • Parameters

    • value: string

    Returns void

Methods

  • Add a component to the entity.

    Type Parameters

    • T extends Record<string, any>

    Parameters

    • component: T

      The component object to add.

    • componentClass: Class<T> = ...

      The class of the component. This is only necessary if the component extends another component class and you want the framework to treat the component as of the base class type. If not set, the class type is determined directly from the component.

    Returns Entity

    A reference to the entity. This enables the chaining of calls to add, to make creating and configuring entities cleaner. e.g.

    Example

    const entity:Entity = new Entity()
    .add(new Position(100, 200)
    .add(new Display(new PlayerClip());
  • Get a component from the entity.

    Type Parameters

    • T

    Parameters

    • componentClass: Class<T>

      The class of the component requested.

    Returns null | T

    The component, or null if none was found.

  • Get all components from the entity.

    Returns any[]

    An array containing all the components that are on the entity.

  • Does the entity have a component of a particular type.

    Type Parameters

    • T

    Parameters

    • componentClass: Class<T>

      The class of the component sought.

    Returns boolean

    true if the entity has a component of the type, false if not.

  • Remove a component from the entity.

    Type Parameters

    • T

    Parameters

    • componentClass: Class<T>

      The class of the component to be removed.

    Returns null | T

    the component, or null if the component doesn't exist in the entity

Generated using TypeDoc