CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
corgi::EntityManager Class Reference

The EntityManager is the code that manages all Entities and Components in the game. Normally the game will instantiate EntityManager, and then use it to create and control all of its Entities. More...

#include <entity_manager.h>

Detailed Description

The EntityManager is the code that manages all Entities and Components in the game. Normally the game will instantiate EntityManager, and then use it to create and control all of its Entities.

The main uses for this class are:

  • Creating/destroying new Entities.
  • Keeping track of all Components.
  • Spawning and populating new Components from data.
  • Updating Entities/Components. (Usually done once per frame.)

Public Types

typedef VectorPool< EntityEntityStorageContainer
 This is used to track all Entities stored by the EntityManager.
 

Public Member Functions

 EntityManager ()
 Constructor for the EntityManager.
 
const CorgiVersion * GetCorgiVersion ()
 Returns the version of the Corgi entity library.
 
template<typename T >
T * GetComponentData (const EntityRef &entity)
 Helper function for marshalling data from a Component. More...
 
template<typename T >
const T * GetComponentData (const EntityRef &entity) const
 A helper function for marshalling data from a Component. More...
 
template<typename T >
T * GetComponent ()
 A helper function for getting a particular Component, given the Component's data type. More...
 
template<typename T >
const T * GetComponent () const
 A helper function for getting a particular Component, given the Component's data type. More...
 
template<typename T >
void AddEntityToComponent (EntityRef entity)
 A helper function for adding a Component to an Entity, given its data type. More...
 
ComponentInterfaceGetComponent (ComponentId id)
 A helper function for getting a particular Component, given the component ID. More...
 
const ComponentInterfaceGetComponent (ComponentId id) const
 A helper function for getting a particular Component, given a component ID. More...
 
size_t ComponentCount () const
 Returns the number of components that have been registered with the entity manager. More...
 
template<typename T >
ComponentId GetComponentId ()
 A helper function to get the component ID for a given Component. More...
 
EntityRef AllocateNewEntity ()
 Allocates a new Entity (that is registered with no Components). More...
 
void DeleteEntity (EntityRef entity)
 Deletes an Entity by removing it from the EntityManager's list and clearing any Component data associated with it. More...
 
void DeleteEntityImmediately (EntityRef entity)
 Instantly deletes an Entity. More...
 
template<typename T >
ComponentId RegisterComponent (T *new_component)
 Registers a new Component with the EntityManager. More...
 
void RemoveAllComponents (EntityRef entity)
 Removes all Components for an Entity, destroying any data associated with it. More...
 
void UpdateComponents (WorldTime delta_time)
 Iterates through all the registered Components and causes them to update. More...
 
void Clear ()
 Clears all data from all Components, empties the list of Components themselves, and then empties the list of Entities. This basically resets the EntityManager into its original state.
 
EntityStorageContainer::Iterator begin ()
 Returns an iterator to the beginning of the active Entities. This is suitable for iterating over every active Entity. More...
 
EntityStorageContainer::Iterator end ()
 Returns an iterator to the last of the active Entities. More...
 
void set_entity_factory (EntityFactoryInterface *entity_factory)
 Registers an instance of the EntityFactoryInterface as the factory object to be used when Entities are created from arbitrary data. The EntityFactory is responsible for parsing arbitrary data and correctly transforming it into an Entity. More...
 
EntityRef CreateEntityFromData (const void *data)
 Creates an Entity from arbitrary data. This is normally invoked only by classes that inherit from EntityFactoryInterface. More...
 
void AddEntityToComponent (EntityRef entity, ComponentId component_id)
 Registers an Entity with a Component. This causes the Component to allocate data for the Entity and includes the Entity in that Component's update routines. More...
 
void DeleteMarkedEntities ()
 Deletes all the Entities that are marked for deletion. More...
 

Member Function Documentation

template<typename T >
void corgi::EntityManager::AddEntityToComponent ( EntityRef  entity)
inline

A helper function for adding a Component to an Entity, given its data type.

Note
Asserts if the Component does not exist.
Template Parameters
TThe data type of the Component that should have the Entity added to it.
Parameters
[in]entityAn EntityRef that points to the Entity that is being added to the Component.
void corgi::EntityManager::AddEntityToComponent ( EntityRef  entity,
ComponentId  component_id 
)

Registers an Entity with a Component. This causes the Component to allocate data for the Entity and includes the Entity in that Component's update routines.

Parameters
[in]entityAn EntityRef that points to the Entity that should be registered with the Component.
[in]component_idThe component ID of the Component that should be registered with the Entity.
EntityRef corgi::EntityManager::AllocateNewEntity ( )

Allocates a new Entity (that is registered with no Components).

Returns
Returns an EntityRef that points to the new Entity.
EntityStorageContainer::Iterator corgi::EntityManager::begin ( )
inline

Returns an iterator to the beginning of the active Entities. This is suitable for iterating over every active Entity.

Returns
Returns a std::iterator to the first active Entity.
size_t corgi::EntityManager::ComponentCount ( ) const
inline

Returns the number of components that have been registered with the entity manager.

Returns
The total number of components that are currently registered with the entity manager.
EntityRef corgi::EntityManager::CreateEntityFromData ( const void *  data)

Creates an Entity from arbitrary data. This is normally invoked only by classes that inherit from EntityFactoryInterface.

Any class implementing CreateEntityFromData should establish the input format for the 'data' and a way to parse that input 'data' into an Entity.

Parameters
[in]dataA void pointer to the data to be used to create the Entity.
Returns
Returns an EntityRef that points to the newly created Entity.
void corgi::EntityManager::DeleteEntity ( EntityRef  entity)

Deletes an Entity by removing it from the EntityManager's list and clearing any Component data associated with it.

Note
Deletion is deferred until the end of the frame. If you want to delete something instantly, use DeleteEntityImmediately.
Parameters
[in]entityAn EntityRef that points to the Entity that will be deleted at the end of the frame.
void corgi::EntityManager::DeleteEntityImmediately ( EntityRef  entity)

Instantly deletes an Entity.

Note
In general, you should use DeleteEntity (which defers deletion until the end of the update cycle) unless you have a very good reason for doing so.
Parameters
[in]entityAn EntityRef that points to the Entity that will be immediately deleted.
void corgi::EntityManager::DeleteMarkedEntities ( )

Deletes all the Entities that are marked for deletion.

Warning
Do NOT call this function during any form of Entity update!
EntityStorageContainer::Iterator corgi::EntityManager::end ( )
inline

Returns an iterator to the last of the active Entities.

Returns
Returns a std::iterator to the last active Entity.
template<typename T >
T* corgi::EntityManager::GetComponent ( )
inline

A helper function for getting a particular Component, given the Component's data type.

Note
Asserts if the Component does not exist.
Template Parameters
TThe data type of the Component to be returned.
Returns
Returns a pointer to the Component given its data type.
template<typename T >
const T* corgi::EntityManager::GetComponent ( ) const
inline

A helper function for getting a particular Component, given the Component's data type.

Note
Asserts if the Component does not exist.
Template Parameters
TThe data type of the Component to be returned.
Returns
Returns a const pointer to the Component given its datatype.
ComponentInterface* corgi::EntityManager::GetComponent ( ComponentId  id)
inline

A helper function for getting a particular Component, given the component ID.

Parameters
[in]idThe component ID for the desired Component.
Note
Asserts if the id is less than kMaxComponentCount.
Returns
Returns a pointer to the Component at the given id, which inherits from the ComponentInterface.
const ComponentInterface* corgi::EntityManager::GetComponent ( ComponentId  id) const
inline

A helper function for getting a particular Component, given a component ID.

Parameters
[in]idThe component ID for the desired Component.
Note
Asserts if the id is less than kMaxComponentCount.
Returns
Returns a const pointer to the Component at the given id, which inherits from the ComponentInterface.
template<typename T >
T* corgi::EntityManager::GetComponentData ( const EntityRef entity)
inline

Helper function for marshalling data from a Component.

Template Parameters
TThe data type of the Component data to be returned.
Parameters
[in]entityThe Entity associated with the desired data.
Returns
Returns a pointer to the Component data, or returns a nullptr if no such data exists.
template<typename T >
const T* corgi::EntityManager::GetComponentData ( const EntityRef entity) const
inline

A helper function for marshalling data from a Component.

Template Parameters
TThe data type of the Component data to be returned.
Parameters
[in]entityThe Entity associated with the desired data.
Returns
Returns a const pointer to the Component data, or returns a nullptr if no such data exists.
template<typename T >
ComponentId corgi::EntityManager::GetComponentId ( )
inline

A helper function to get the component ID for a given Component.

Template Parameters
TThe data type of the Component whose ID should be returned.
Returns
Returns the component ID for a given Component.
template<typename T >
ComponentId corgi::EntityManager::RegisterComponent ( T *  new_component)
inline

Registers a new Component with the EntityManager.

Template Parameters
TThe data type of the Component that is being registered with the EntityManager.
Parameters
[in]new_componentA Component to be registered with to the EntityManager.
Note
The new_component must inherit from the ComponentInterface.
Returns
Returns the component ID for the new Component.
void corgi::EntityManager::RemoveAllComponents ( EntityRef  entity)

Removes all Components for an Entity, destroying any data associated with it.

Note
Normally called by EntityManager prior to deleting an Entity.
Parameters
[in]entityAn EntityRef that points to the Entity whose Components should be removed.
void corgi::EntityManager::set_entity_factory ( EntityFactoryInterface entity_factory)
inline

Registers an instance of the EntityFactoryInterface as the factory object to be used when Entities are created from arbitrary data. The EntityFactory is responsible for parsing arbitrary data and correctly transforming it into an Entity.

Note
This should be set before attempting to load Entities from data files.
Parameters
[in]entity_factoryA pointer to a class that inherits from the EntityFactoryInterface, which will be registered with the EntityManager.
void corgi::EntityManager::UpdateComponents ( WorldTime  delta_time)

Iterates through all the registered Components and causes them to update.

Parameters
[in]delta_timeA WorldTime that represents the timestep since the last update.

The documentation for this class was generated from the following file: