Khaotic Engine Reborn
Loading...
Searching...
No Matches
ecs::EntityManager Class Reference

Public Member Functions

 EntityManager ()
 Type pour l'ID d'une entité.
 
std::shared_ptr< EntityCreateEntity ()
 
void DestroyEntity (EntityID id)
 
std::shared_ptr< EntityGetEntity (EntityID id)
 
void UpdateEntities (float deltaTime)
 
std::vector< std::shared_ptr< Entity > > GetAllEntities ()
 
template<typename T >
std::vector< std::shared_ptr< Entity > > GetEntitiesWithComponent ()
 
size_t GetEntityCount () const
 
void Clear ()
 

Detailed Description

Definition at line 9 of file entity_manager.h.

Constructor & Destructor Documentation

◆ EntityManager()

ecs::EntityManager::EntityManager ( )
inline

Type pour l'ID d'une entité.

Definition at line 15 of file entity_manager.h.

15: m_NextEntityID(0) {}

Member Function Documentation

◆ Clear()

void ecs::EntityManager::Clear ( )
inline

Clear all entities and reset the EntityManager. This will remove all entities and free their IDs for future use.

Definition at line 121 of file entity_manager.h.

121 {
122 m_Entities.clear();
123
124 // Vider la file des IDs libres
125 std::queue<EntityID> empty;
126 std::swap(m_FreeIDs, empty);
127
128 m_NextEntityID = 0;
129 }

◆ CreateEntity()

std::shared_ptr< Entity > ecs::EntityManager::CreateEntity ( )
inline

Create a new entity.

Returns
A shared pointer to the newly created entity.

Definition at line 22 of file entity_manager.h.

22 {
23 EntityID id;
24
25 // Réutiliser les IDs des entités supprimées si possible
26 if (!m_FreeIDs.empty()) {
27 id = m_FreeIDs.front();
28 m_FreeIDs.pop();
29 } else {
30 id = m_NextEntityID++;
31 }
32
33 auto entity = std::make_shared<Entity>(id);
34 m_Entities[id] = entity;
35
36 return entity;
37 }
uint32_t EntityID
Definition entity.h:13

◆ DestroyEntity()

void ecs::EntityManager::DestroyEntity ( EntityID id)
inline

Destroy an entity by its ID and recycle its ID for future use.

Parameters
idThe ID of the entity to destroy.

Definition at line 43 of file entity_manager.h.

43 {
44 auto it = m_Entities.find(id);
45 if (it != m_Entities.end()) {
46 m_Entities.erase(it);
47 m_FreeIDs.push(id); // Recycler l'ID
48 }
49 }

◆ GetAllEntities()

std::vector< std::shared_ptr< Entity > > ecs::EntityManager::GetAllEntities ( )
inline

Get all entities managed by the EntityManager.

Returns
A vector of shared pointers to all entities.

Definition at line 78 of file entity_manager.h.

78 {
79 std::vector<std::shared_ptr<Entity>> result;
80 result.reserve(m_Entities.size());
81
82 for (const auto& [id, entity] : m_Entities) {
83 result.push_back(entity);
84 }
85
86 return result;
87 }

◆ GetEntitiesWithComponent()

template<typename T >
std::vector< std::shared_ptr< Entity > > ecs::EntityManager::GetEntitiesWithComponent ( )
inline

Get all entities that have a specific component type.

Template Parameters
TThe component type to filter entities by.
Returns
A vector of shared pointers to entities that have the specified component.

Definition at line 95 of file entity_manager.h.

95 {
96 static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
97
98 std::vector<std::shared_ptr<Entity>> result;
99
100 for (auto& [id, entity] : m_Entities) {
101 if (entity->HasComponent<T>()) {
102 result.push_back(entity);
103 }
104 }
105
106 return result;
107 }

◆ GetEntity()

std::shared_ptr< Entity > ecs::EntityManager::GetEntity ( EntityID id)
inline

Get an entity by its ID.

Parameters
idThe ID of the entity to retrieve.
Returns
A shared pointer to the entity, or nullptr if it does not exist.

Definition at line 56 of file entity_manager.h.

56 {
57 auto it = m_Entities.find(id);
58 if (it != m_Entities.end()) {
59 return it->second;
60 }
61 return nullptr;
62 }

◆ GetEntityCount()

size_t ecs::EntityManager::GetEntityCount ( ) const
inline

Get the total number of entities managed by the EntityManager.

Returns
The count of entities.

Definition at line 113 of file entity_manager.h.

113 {
114 return m_Entities.size();
115 }

◆ UpdateEntities()

void ecs::EntityManager::UpdateEntities ( float deltaTime)
inline

Update all entities by calling their UpdateComponents method.

Parameters
deltaTimeThe time elapsed since the last update.

Definition at line 68 of file entity_manager.h.

68 {
69 for (auto& [id, entity] : m_Entities) {
70 entity->UpdateComponents(deltaTime);
71 }
72 }

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