CG-Project2
Loading...
Searching...
No Matches
EntityManager Class Reference

This class manages all the entities and the components instanced. More...

#include <EntityManager.hpp>

Public Types

using Index = unsigned int

Public Member Functions

Index createEntity ()
 Create an entity and increment the current index.
void resetIndex ()
 Reset the ECS id, it is usually used when entities are cleaned.
std::vector< Index > getEntities () const
 Retrieves a vector containing all entities in the ECS.
std::string getEntityName (const Index &id) const
 Retrieves the entity name.
void setEntityName (const Index &id, const std::string &name)
 Sets the name of the entity given.
bool removeEntity (const Index &id)
 Removes the entity given from the ECS.
bool isEntityValid (const Index &id)
 Checks if the entity given has components.
unsigned int getEntitiesCount () const
 Retrieves the number of entities in the ECS.
template<typename T, typename... Args>
Shared< T > addComponent (const Index &id, Args &&...args)
 Adds a component to the given entity. It instances a shared pointer of that component and calls the component constructor with the arguments given.
template<typename T>
bool removeComponent (const Index &id)
 Removes a component from the given entity.
template<typename T>
bool entityHasComponent (const Index &id)
 Checks if the given entity has the component.
template<typename T>
std::vector< Index > getEntitiesFromComponent () const
 Retrieves all the entities that have the given component.
template<typename T>
Shared< T > getComponentFromId (const Index &id)
 Retrieves a shared pointer to the component of an entity.
 EntityManager (EntityManager &other)=delete
void operator= (const EntityManager &other)=delete

Static Public Member Functions

static Shared< EntityManagerinstance ()
 Retrieves the instance of the EntityManager. If it's not instanced, it will be instanced automatically.

Detailed Description

This class manages all the entities and the components instanced.

It allows to add entities, add and remove components from entitites, query all entities with a specific component, etc.

It's implemented with singleton so there only be one instance of this class.

Member Function Documentation

◆ addComponent()

template<typename T, typename... Args>
Shared< T > EntityManager::addComponent ( const Index & id,
Args &&... args )
inline

Adds a component to the given entity. It instances a shared pointer of that component and calls the component constructor with the arguments given.

Template Parameters
Tclass of the component
Argslist of arguments
Parameters
identity id
argsarguments list
Returns
a shared pointer to that component of type T

◆ createEntity()

Index EntityManager::createEntity ( )
inline

Create an entity and increment the current index.

Returns
the id of the new entity

◆ entityHasComponent()

template<typename T>
bool EntityManager::entityHasComponent ( const Index & id)
inline

Checks if the given entity has the component.

Template Parameters
Tclass of the component
Parameters
identity id
Returns
true if the entity has that component

◆ getComponentFromId()

template<typename T>
Shared< T > EntityManager::getComponentFromId ( const Index & id)
inline

Retrieves a shared pointer to the component of an entity.

Template Parameters
Tclass of the component
Parameters
identity id
Returns
a shared pointer to the component if the entity has one, if not it returns nullptr

◆ getEntities()

std::vector< Index > EntityManager::getEntities ( ) const
inline

Retrieves a vector containing all entities in the ECS.

Returns
a vector of entities id.

◆ getEntitiesCount()

unsigned int EntityManager::getEntitiesCount ( ) const
inline

Retrieves the number of entities in the ECS.

Returns
the number of entities

◆ getEntitiesFromComponent()

template<typename T>
std::vector< Index > EntityManager::getEntitiesFromComponent ( ) const
inline

Retrieves all the entities that have the given component.

Template Parameters
Tclass of the component
Returns
a vector containing entities id.

◆ getEntityName()

std::string EntityManager::getEntityName ( const Index & id) const
inline

Retrieves the entity name.

Parameters
identity id
Returns
the entity name

◆ instance()

Shared< EntityManager > EntityManager::instance ( )
inlinestatic

Retrieves the instance of the EntityManager. If it's not instanced, it will be instanced automatically.

Returns
a Shared<EntityManager> object

◆ isEntityValid()

bool EntityManager::isEntityValid ( const Index & id)
inline

Checks if the entity given has components.

Parameters
identity id
Returns
true if the entity has components

◆ removeComponent()

template<typename T>
bool EntityManager::removeComponent ( const Index & id)
inline

Removes a component from the given entity.

Template Parameters
Tclass of the component that will be removed
Parameters
identity id
Returns
true if the component is removed

◆ removeEntity()

bool EntityManager::removeEntity ( const Index & id)
inline

Removes the entity given from the ECS.

If the entity is present in the ECS, his component will be cleared.

Parameters
identity id
Returns
true if the entity is removed

◆ setEntityName()

void EntityManager::setEntityName ( const Index & id,
const std::string & name )
inline

Sets the name of the entity given.

Parameters
identity id

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