# Khaotic Engine Reborn: ecs::EntityManager Class Reference

Loading...
Searching...
No Matches
ecs::EntityManager Class Reference

## Public Member Functions

| EntityManager ()
| Type pour l'ID d'une entité.
std::shared_ptr< Entity > | CreateEntity ()
void | DestroyEntity (EntityID id)
std::shared_ptr< Entity > | GetEntity (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.

: 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.

{
m_Entities.clear();
// Vider la file des IDs libres
std::queue<EntityID> empty;
std::swap(m_FreeIDs, empty);
m_NextEntityID = 0;
}

## 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.

{
EntityID id;
// Réutiliser les IDs des entités supprimées si possible
if (!m_FreeIDs.empty()) {
id = m_FreeIDs.front();
m_FreeIDs.pop();
} else {
id = m_NextEntityID++;
}
auto entity = std::make_shared<Entity>(id);
m_Entities[id] = entity;
return entity;
}
ecs::EntityID
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

  id | The ID of the entity to destroy.

Definition at line 43 of file entity_manager.h.

{
auto it = m_Entities.find(id);
if (it != m_Entities.end()) {
m_Entities.erase(it);
m_FreeIDs.push(id); // Recycler l'ID
}
}

## 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.

{
std::vector<std::shared_ptr<Entity>> result;
result.reserve(m_Entities.size());
for (const auto& [id, entity] : m_Entities) {
result.push_back(entity);
}
return result;
}

## 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

  T | The 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.

{
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
std::vector<std::shared_ptr<Entity>> result;
for (auto& [id, entity] : m_Entities) {
if (entity->HasComponent<T>()) {
result.push_back(entity);
}
}
return result;
}

## GetEntity()

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

inline

Get an entity by its ID.

Parameters

  id | The 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.

{
auto it = m_Entities.find(id);
if (it != m_Entities.end()) {
return it->second;
}
return nullptr;
}

## 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.

{
return m_Entities.size();
}

## UpdateEntities()

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

inline

Update all entities by calling their UpdateComponents method.

Parameters

  deltaTime | The time elapsed since the last update.

Definition at line 68 of file entity_manager.h.

{
for (auto& [id, entity] : m_Entities) {
entity->UpdateComponents(deltaTime);
}
}

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

- enginecustom/src/inc/system/ecs/entity_manager.h