Entity
In the ECS pattern, an Entity
is in essence just a unique id that is used to associate different Components
together. Components
ARE NOT stored "on" or "in" an Entity
.
Ecstatic's Entity
objects allow manipulation and access to info associated to the Entity
's id.
#
Creating an Entity.There are two methods of creating a new Entity
.
world.createEntity()
#
1. Creating a new Entity
in the way is a little easier, in that you do not need to import the Entity
class constructor.
new Entity(world)
#
2. The largest benefit to being able to just "new up" an Entity
, is that you may now easily extend the Entity
class. This is the way in which you can add lifecycle methods to your Entity
(more info below). It's also nice to be able to add more context to, or "init" an Entity
by creating a wrapper class.
#
Destroying an EntityWhen an Entity
has outlived its usefulness, it needs to be destroyed. There are currently two ways to destroy an Entity
:
entity.destroy()
#
entity.destroy()
will destroy the entity in a deferred manor. The state of the entity will be changed to destroying
and actual removal of the entity will happen at the end of the next pass of systems. This allows all systems a chance to run any cleanup logic that is needed.
entity.destroyImmediately()
#
entity.destroyImmediately()
will destroy the entity immediately. The state of the entity will be set to destroyed
, and systems will not run over the entity when they run next.
#
Adding and Removing ComponentsComponents
may be added and removed via the add
and remove
methods.
#
Entity TagsTags may be added to entities in much the same way as components. A tag is just a string.
Entities can be queried by tag
If you are going to use tags to help in locating entities across an app, it's recommended that you use constant or enum values as the keys so that if they change, they can be updated in a central location
#
State of an EntityIn order to support things like deferred destruction, an Entity
is essential a state machine. The states are:
#
creatingThis is the initial state of the Entity
. Can be used is systems to run initialization code.
#
createdThe state of the Entity
after all Systems
have run over the entity a single time.
#
destroyingIt's often helpful to defer the actual destruction of an entity until the Systems
have had a chance to run cleanup logic over it. This state is triggered only if the Entity
is destroyed via the entity.destroy()
method, and NOT with the entity.destoryImmediately()
method.
#
destroyedThe entity is destroyed, and should probably be released and left for the GC to clean up.
#
errorThis state is reached when something wrong has happend...
#
Entity Lifecycle MethodsThat's right, An Entity
can have lifecycle methods! Each one is added by extending the Entity
class.
#
onCreateCalled when the entity is created, after it has been added to the world.
#
onComponentAddCalled when a Component
is added to the Entity
. The world and the added component are passed in an arg object.
#
onTrackedComponentUpdateCalled when something on a TrackedComponent
is updated. The world and the added component are passed in an arg object.
#
onComponentRemoveCalled when a Component
is removed from the entity. The world and the added component are passed in an arg object.
#
onDestroyCalled when the Entity
is destroyed. Currently this happens when the entity is in the destroying
state.