Thrive
Classes | Public Member Functions | Friends | List of all members
thrive::EntityManager Class Reference

Manages entities and their components. More...

#include <entity_manager.h>

Classes

struct  Implementation
 

Public Member Functions

 EntityManager ()
 Constructor.
 
 ~EntityManager ()
 Destructor.
 
Component * addComponent (EntityId entityId, std::unique_ptr< Component > component)
 Adds a component. More...
 
template<typename C >
C * addComponent (EntityId entityId, std::unique_ptr< C > component)
 Adds a component. More...
 
void clear ()
 Removes all components. More...
 
std::unordered_set< EntityId > entities ()
 Returns a set of entity ids that have at least one components.
 
EntityId generateNewId ()
 Generates a new, unique entity id. More...
 
Component * getComponent (EntityId entityId, ComponentTypeId typeId)
 Retrieves a component. More...
 
template<class ComponentType >
ComponentType * getComponent (EntityId entityId)
 Convenience template overload. More...
 
ComponentCollection & getComponentCollection (ComponentTypeId typeId)
 Returns a component collection. More...
 
EntityId getNamedId (const std::string &name, bool forceNew=false)
 Returns the id of a named entity. More...
 
template<typename C , typename... Args>
C * getOrCreateComponent (EntityId id, Args &&...args)
 Retrieves a component, creating it if necessary. More...
 
bool exists (EntityId entityId) const
 Checks whether an entity exists. More...
 
std::unordered_set< ComponentTypeId > nonEmptyCollections () const
 Returns the set of non-empty collection ids. More...
 
bool isVolatile (EntityId id) const
 Returns the volatile flag for an entity. More...
 
void stealName (EntityId entityId, const std::string &name)
 Rebinds a name to this entity. More...
 
StorageContainer storeEntity (EntityId entityId) const
 Stores a single entity. More...
 
EntityId loadEntity (StorageContainer storage, const ComponentFactory &componentFactory)
 Loads an entity into this entity manager. More...
 
void processRemovals ()
 Removes all components queued for removal.
 
void processTransfers ()
 Transfers all components queued for transfer.
 
void removeComponent (EntityId entityId, ComponentTypeId typeId)
 Removes a component. More...
 
void removeEntity (EntityId entityId)
 Removes all components of an entity. More...
 
void addChild (EntityId child, EntityId parent)
 Sets an entity as another entities child. More...
 
bool hasChildren (EntityId entityId) const
 Returns whether an entity has any children. More...
 
const std::string * getNameMappingFor (EntityId entityId)
 Gets the name for a given entityId if it exists. More...
 
void restore (const StorageContainer &storage, const ComponentFactory &factory)
 Restores the entity manager from a storage container. More...
 
void setVolatile (EntityId id, bool isVolatile)
 Sets the volatile flag for an entity. More...
 
StorageContainer storage (const ComponentFactory &factory) const
 Serializes the current non-volatile components into a storage container. More...
 

Friends

class Engine
 

Detailed Description

Manages entities and their components.

The entity manager holds a collection of Component objects, sorted by type and entity.

Member Function Documentation

void EntityManager::addChild ( EntityId  child,
EntityId  parent 
)

Sets an entity as another entities child.

An entity can be the child of multiple entities, itself or its children so be careful

Note
Parent-child relationships are not serialized
Parameters
child
parent
Component * EntityManager::addComponent ( EntityId  entityId,
std::unique_ptr< Component >  component 
)

Adds a component.

Parameters
entityIdThe entity to add to
componentThe component to add
Returns
The component as a non-owning pointer
Note
: Use the templated version to receive the proper type back
template<typename C >
C* thrive::EntityManager::addComponent ( EntityId  entityId,
std::unique_ptr< C >  component 
)
inline

Adds a component.

Template Parameters
CThe component's class
Parameters
entityIdThe entity to add to
componentThe component to add
Returns
The component as a non-owning pointer
void EntityManager::clear ( )

Removes all components.

Usually only used in testing.

bool EntityManager::exists ( EntityId  entityId) const

Checks whether an entity exists.

Parameters
entityIdThe id to check for
Returns
true if the entity has at least one component, false otherwise
EntityId EntityManager::generateNewId ( )

Generates a new, unique entity id.

This function is safe.

Returns
A new entity id
Component * EntityManager::getComponent ( EntityId  entityId,
ComponentTypeId  typeId 
)

Retrieves a component.

Parameters
entityIdThe component's owner
typeIdThe component's type id
Returns
A non-owning pointer to the component or nullptr if no such component exists
template<class ComponentType >
ComponentType* thrive::EntityManager::getComponent ( EntityId  entityId)
inline

Convenience template overload.

This is the same as EntityManager::getComponent(EntityId, ComponentTypeId), but includes a cast to the expected type. The cast is a static cast for performance reasons. Unless there is a serious error in the way component type ids are generated or components are stored in the entity manager, the cast should always be correct.

The component id is read from ComponentType::TYPE_ID

Template Parameters
ComponentTypeThe component subclass to retrieve
Parameters
entityIdThe component's owner
Returns
ComponentCollection & EntityManager::getComponentCollection ( ComponentTypeId  typeId)

Returns a component collection.

Parameters
typeIdThe component type the collection is holding
EntityId EntityManager::getNamedId ( const std::string &  name,
bool  forceNew = false 
)

Returns the id of a named entity.

If the name is unknown, a new entity id is created. This function always returns the same entity id for the same name during the same application instance.

Parameters
nameThe entity's name
forceNewForces a new entity with the provided name to be created whether it exists or not
Returns
The named entity's id
const std::string * EntityManager::getNameMappingFor ( EntityId  entityId)

Gets the name for a given entityId if it exists.

Parameters
entityIdThe entity to find a name mapping for
Returns
valid pointer to string if such a namemapping was found, nullptr otherwise
template<typename C , typename... Args>
C* thrive::EntityManager::getOrCreateComponent ( EntityId  id,
Args &&...  args 
)
inline

Retrieves a component, creating it if necessary.

Template Parameters
CThe component class
ArgsConstructor arguments in case the component could not be found
Parameters
idThe entity the component belongs to
argsConstructor arguments
Returns
A non-owning pointer to the component
bool EntityManager::hasChildren ( EntityId  entityId) const

Returns whether an entity has any children.

Parameters
entity
Returns
bool EntityManager::isVolatile ( EntityId  id) const

Returns the volatile flag for an entity.

Volatile entities are not serialized into a savegame

Parameters
id
Returns
EntityId EntityManager::loadEntity ( StorageContainer  storage,
const ComponentFactory &  componentFactory 
)

Loads an entity into this entity manager.

Parameters
storageThe storage to load the entity from
componentFactoryFactory used for loading entity components
Returns
The new entity id in the new gamestate
std::unordered_set< ComponentTypeId > EntityManager::nonEmptyCollections ( ) const

Returns the set of non-empty collection ids.

Returns
void EntityManager::removeComponent ( EntityId  entityId,
ComponentTypeId  typeId 
)

Removes a component.

If the component doesn't exist, this function does nothing.

To allow self-removing components such as script handles, the component is only removed with the next call to EntityManager::processRemovals().

Parameters
entityIdThe component's owner
typeIdThe component's type id
void EntityManager::removeEntity ( EntityId  entityId)

Removes all components of an entity.

If the entity has no components, this function does nothing.

To allow self-removing components such as script handles, the component is only removed with the next call to EntityManager::processRemovals().

Parameters
entityIdThe entity to remove
void EntityManager::restore ( const StorageContainer &  storage,
const ComponentFactory &  factory 
)

Restores the entity manager from a storage container.

Parameters
storageThe storage container to restore from
factoryThe component factory to use
void EntityManager::setVolatile ( EntityId  id,
bool  isVolatile 
)

Sets the volatile flag for an entity.

Parameters
id
isVolatile
void EntityManager::stealName ( EntityId  entityId,
const std::string &  name 
)

Rebinds a name to this entity.

Parameters
namename re rebind
StorageContainer EntityManager::storage ( const ComponentFactory &  factory) const

Serializes the current non-volatile components into a storage container.

Parameters
factoryThe component factory to use for type name lookup
Returns
StorageContainer EntityManager::storeEntity ( EntityId  entityId) const

Stores a single entity.

Parameters
entityIdThe entity to store
Returns
A container holding the entity.

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