# Khaotic Engine Reborn: enginecustom/src/inc/system/ecs/entity_manager.h Source File

Loading...
Searching...
No Matches
entity_manager.h
#pragma once
#include "entity.h"
#include <vector>
#include <unordered_map>
#include <queue>
namespace ecs {
class EntityManager {
public:
EntityManager() : m_NextEntityID(0) {}
~EntityManager() = default;
std::shared_ptr<Entity> CreateEntity() {
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;
}
void DestroyEntity(EntityID id) {
auto it = m_Entities.find(id);
if (it != m_Entities.end()) {
m_Entities.erase(it);
m_FreeIDs.push(id); // Recycler l'ID
}
}
std::shared_ptr<Entity> GetEntity(EntityID id) {
auto it = m_Entities.find(id);
if (it != m_Entities.end()) {
return it->second;
}
return nullptr;
}
void UpdateEntities(float deltaTime) {
for (auto& [id, entity] : m_Entities) {
entity->UpdateComponents(deltaTime);
}
}
std::vector<std::shared_ptr<Entity>> GetAllEntities() {
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;
}
template<typename T>
std::vector<std::shared_ptr<Entity>> GetEntitiesWithComponent() {
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;
}
size_t GetEntityCount() const {
return m_Entities.size();
}
void Clear() {
m_Entities.clear();
// Vider la file des IDs libres
std::queue<EntityID> empty;
std::swap(m_FreeIDs, empty);
m_NextEntityID = 0;
}
private:
EntityID m_NextEntityID;
std::unordered_map<EntityID, std::shared_ptr<Entity>> m_Entities;
std::queue<EntityID> m_FreeIDs; // IDs à réutiliser
};
} // namespace ecs
ecs::EntityManager
Definition entity_manager.h:9
ecs::EntityManager::GetEntitiesWithComponent
std::vector< std::shared_ptr< Entity > > GetEntitiesWithComponent()
Definition entity_manager.h:95
ecs::EntityManager::DestroyEntity
void DestroyEntity(EntityID id)
Definition entity_manager.h:43
ecs::EntityManager::CreateEntity
std::shared_ptr< Entity > CreateEntity()
Definition entity_manager.h:22
ecs::EntityManager::Clear
void Clear()
Definition entity_manager.h:121
ecs::EntityManager::GetEntity
std::shared_ptr< Entity > GetEntity(EntityID id)
Definition entity_manager.h:56
ecs::EntityManager::UpdateEntities
void UpdateEntities(float deltaTime)
Definition entity_manager.h:68
ecs::EntityManager::EntityManager
EntityManager()
Type pour l'ID d'une entité.
Definition entity_manager.h:15
ecs::EntityManager::GetAllEntities
std::vector< std::shared_ptr< Entity > > GetAllEntities()
Definition entity_manager.h:78
ecs::EntityManager::GetEntityCount
size_t GetEntityCount() const
Definition entity_manager.h:113
ecs
Definition component.h:9
ecs::EntityID
uint32_t EntityID
Definition entity.h:13