|
Thrive
|
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 |
Manages entities and their components.
The entity manager holds a collection of Component objects, sorted by type and entity.
| 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
| child | |
| parent |
| Component * EntityManager::addComponent | ( | EntityId | entityId, |
| std::unique_ptr< Component > | component | ||
| ) |
Adds a component.
| entityId | The entity to add to |
| component | The component to add |
|
inline |
Adds a component.
| C | The component's class |
| entityId | The entity to add to |
| component | The component to add |
| void EntityManager::clear | ( | ) |
Removes all components.
Usually only used in testing.
| bool EntityManager::exists | ( | EntityId | entityId | ) | const |
Checks whether an entity exists.
| entityId | The id to check for |
true if the entity has at least one component, false otherwise | EntityId EntityManager::generateNewId | ( | ) |
Generates a new, unique entity id.
This function is safe.
| Component * EntityManager::getComponent | ( | EntityId | entityId, |
| ComponentTypeId | typeId | ||
| ) |
Retrieves a component.
| entityId | The component's owner |
| typeId | The component's type id |
nullptr if no such component exists
|
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
| ComponentType | The component subclass to retrieve |
| entityId | The component's owner |
| ComponentCollection & EntityManager::getComponentCollection | ( | ComponentTypeId | typeId | ) |
Returns a component collection.
| typeId | The 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.
| name | The entity's name |
| forceNew | Forces a new entity with the provided name to be created whether it exists or not |
| const std::string * EntityManager::getNameMappingFor | ( | EntityId | entityId | ) |
Gets the name for a given entityId if it exists.
| entityId | The entity to find a name mapping for |
valid pointer to string if such a namemapping was found, nullptr otherwise
|
inline |
Retrieves a component, creating it if necessary.
| C | The component class |
| Args | Constructor arguments in case the component could not be found |
| id | The entity the component belongs to |
| args | Constructor arguments |
| bool EntityManager::hasChildren | ( | EntityId | entityId | ) | const |
Returns whether an entity has any children.
| entity |
| bool EntityManager::isVolatile | ( | EntityId | id | ) | const |
Returns the volatile flag for an entity.
Volatile entities are not serialized into a savegame
| id |
| EntityId EntityManager::loadEntity | ( | StorageContainer | storage, |
| const ComponentFactory & | componentFactory | ||
| ) |
Loads an entity into this entity manager.
| storage | The storage to load the entity from |
| componentFactory | Factory used for loading entity components |
| std::unordered_set< ComponentTypeId > EntityManager::nonEmptyCollections | ( | ) | const |
Returns the set of non-empty collection ids.
| 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().
| entityId | The component's owner |
| typeId | The 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().
| entityId | The entity to remove |
| void EntityManager::restore | ( | const StorageContainer & | storage, |
| const ComponentFactory & | factory | ||
| ) |
Restores the entity manager from a storage container.
| storage | The storage container to restore from |
| factory | The component factory to use |
| void EntityManager::setVolatile | ( | EntityId | id, |
| bool | isVolatile | ||
| ) |
Sets the volatile flag for an entity.
| id | |
| isVolatile |
| void EntityManager::stealName | ( | EntityId | entityId, |
| const std::string & | name | ||
| ) |
Rebinds a name to this entity.
| name | name re rebind |
| StorageContainer EntityManager::storage | ( | const ComponentFactory & | factory | ) | const |
Serializes the current non-volatile components into a storage container.
| factory | The component factory to use for type name lookup |
| StorageContainer EntityManager::storeEntity | ( | EntityId | entityId | ) | const |
Stores a single entity.
| entityId | The entity to store |
1.8.10