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

class PositionComponent { public var x:Float; public var y:Float; }

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.

Constructor

new (name:String = "")

Variables

read onlycomponentAdded:Signal2<Entity, Class<Dynamic>>

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

read onlycomponentRemoved:Signal2<Entity, Class<Dynamic>>

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

name:String

Optional, give the entity a name. This can help with debugging and with serialising the entity.

nameChanged:Signal2<Entity, String>

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

Methods

add<T> (component:T, ?componentClass:Class<Dynamic>):Entity

Add a component to the entity.

Parameters:

component

The component object to add.

componentClass

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:

A reference to the entity. This enables the chaining of calls to add, to make creating and configuring entities cleaner. e.g. var entity:Entity = new Entity()

.add(new Position(100, 200)
.add(new Display(new PlayerClip());</code>

get<T> (componentClass:Class<Dynamic>):T

Get a component from the entity.

Parameters:

componentClass

The class of the component requested.

Returns:

The component, or null if none was found.

getAll ():Array<Dynamic>

Get all components from the entity.

Returns:

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

has (componentClass:Class<Dynamic>):Bool

Does the entity have a component of a particular type.

Parameters:

componentClass

The class of the component sought.

Returns:

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

remove<T> (componentClass:Class<Dynamic>):T

Remove a component from the entity.

Parameters:

componentClass

The class of the component to be removed.

Returns:

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