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

Loading...
Searching...
No Matches
entity.h
#pragma once
#include "component.h"
#include <unordered_map>
#include <vector>
#include <memory>
#include <algorithm>
#include <cassert>
namespace ecs {
using EntityID = uint32_t;
class Entity {
public:
explicit Entity(EntityID id) : m_ID(id) {}
~Entity() = default;
Entity(const Entity&) = delete;
Entity& operator=(const Entity&) = delete;
Entity(Entity&&) = default;
Entity& operator=(Entity&&) = default;
EntityID GetID() const { return m_ID; }
template<typename T, typename... Args>
std::shared_ptr<T> AddComponent(Args&&... args) {
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
ComponentTypeID typeID = GetComponentTypeID<T>();
// Vérifier si le composant existe déjà
if (m_Components.find(typeID) != m_Components.end()) {
return std::static_pointer_cast<T>(m_Components[typeID]);
}
// Créer et ajouter le composant
auto component = std::make_shared<T>(std::forward<Args>(args)...);
m_Components[typeID] = component;
// Initialiser le composant
component->Initialize();
return component;
}
template<typename T>
std::shared_ptr<T> GetComponent() {
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
ComponentTypeID typeID = GetComponentTypeID<T>();
auto it = m_Components.find(typeID);
if (it != m_Components.end()) {
return std::static_pointer_cast<T>(it->second);
}
return nullptr;
}
template<typename T>
bool HasComponent() const {
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
ComponentTypeID typeID = GetComponentTypeID<T>();
return m_Components.find(typeID) != m_Components.end();
}
template<typename T>
void RemoveComponent() {
static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
ComponentTypeID typeID = GetComponentTypeID<T>();
auto it = m_Components.find(typeID);
if (it != m_Components.end()) {
m_Components.erase(it);
}
}
void UpdateComponents(float deltaTime) {
for (auto& [typeID, component] : m_Components) {
component->Update(deltaTime);
}
}
private:
EntityID m_ID;
std::unordered_map<ComponentTypeID, ComponentPtr> m_Components;
};
} // namespace ecs
ecs::Entity
Definition entity.h:15
ecs::Entity::Entity
Entity(const Entity &)=delete
ecs::Entity::GetComponent
std::shared_ptr< T > GetComponent()
Definition entity.h:75
ecs::Entity::UpdateComponents
void UpdateComponents(float deltaTime)
Definition entity.h:121
ecs::Entity::AddComponent
std::shared_ptr< T > AddComponent(Args &&... args)
Definition entity.h:49
ecs::Entity::HasComponent
bool HasComponent() const
Definition entity.h:94
ecs::Entity::Entity
Entity(Entity &&)=default
ecs::Entity::RemoveComponent
void RemoveComponent()
Definition entity.h:106
ecs::Entity::GetID
EntityID GetID() const
Definition entity.h:39
ecs::Entity::Entity
Entity(EntityID id)
Definition entity.h:20
ecs
Definition component.h:9
ecs::ComponentTypeID
std::type_index ComponentTypeID
Definition component.h:48
ecs::GetComponentTypeID
ComponentTypeID GetComponentTypeID()
Definition component.h:54
ecs::EntityID
uint32_t EntityID
Definition entity.h:13