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

Public Member Functions

 Entity (EntityID id)
 
 Entity (const Entity &)=delete
 
Entityoperator= (const Entity &)=delete
 
 Entity (Entity &&)=default
 
Entityoperator= (Entity &&)=default
 
EntityID GetID () const
 
template<typename T , typename... Args>
std::shared_ptr< T > AddComponent (Args &&... args)
 
template<typename T >
std::shared_ptr< T > GetComponent ()
 
template<typename T >
bool HasComponent () const
 
template<typename T >
void RemoveComponent ()
 
void UpdateComponents (float deltaTime)
 

Detailed Description

Definition at line 15 of file entity.h.

Constructor & Destructor Documentation

◆ Entity() [1/3]

ecs::Entity::Entity ( EntityID id)
inlineexplicit

Builder for an Entity with a unique ID.

Definition at line 20 of file entity.h.

20: m_ID(id) {}

◆ Entity() [2/3]

ecs::Entity::Entity ( const Entity & )
delete

No copy constructor or assignment operator to prevent copying.

◆ Entity() [3/3]

ecs::Entity::Entity ( Entity && )
default

Move constructor and assignment operator to allow moving entities.

Member Function Documentation

◆ AddComponent()

template<typename T , typename... Args>
std::shared_ptr< T > ecs::Entity::AddComponent ( Args &&... args)
inline

Add a component of type T to the entity. If the component already exists, it returns the existing component.

Template Parameters
T
Parameters
argsArguments to construct the component.
Returns
A shared pointer to the added or existing component.

Definition at line 49 of file entity.h.

49 {
50 static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
51
53
54 // Vérifier si le composant existe déjà
55 if (m_Components.find(typeID) != m_Components.end()) {
56 return std::static_pointer_cast<T>(m_Components[typeID]);
57 }
58
59 // Créer et ajouter le composant
60 auto component = std::make_shared<T>(std::forward<Args>(args)...);
61 m_Components[typeID] = component;
62
63 // Initialiser le composant
64 component->Initialize();
65
66 return component;
67 }
std::type_index ComponentTypeID
Definition component.h:48
ComponentTypeID GetComponentTypeID()
Definition component.h:54

◆ GetComponent()

template<typename T >
std::shared_ptr< T > ecs::Entity::GetComponent ( )
inline

Get a component of type T from the entity.

Template Parameters
T
Returns
A shared pointer to the component if it exists, nullptr otherwise.

Definition at line 75 of file entity.h.

75 {
76 static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
77
79
80 auto it = m_Components.find(typeID);
81 if (it != m_Components.end()) {
82 return std::static_pointer_cast<T>(it->second);
83 }
84
85 return nullptr;
86 }

◆ GetID()

EntityID ecs::Entity::GetID ( ) const
inline

Get the unique identifier for the entity.

Returns
The unique ID of the entity.

Definition at line 39 of file entity.h.

39{ return m_ID; }

◆ HasComponent()

template<typename T >
bool ecs::Entity::HasComponent ( ) const
inline

Check if the entity has a component of type T.

Template Parameters
T
Returns
true if the entity has the component, false otherwise.

Definition at line 94 of file entity.h.

94 {
95 static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
96
98 return m_Components.find(typeID) != m_Components.end();
99 }

◆ RemoveComponent()

template<typename T >
void ecs::Entity::RemoveComponent ( )
inline

Remove a component of type T from the entity.

Template Parameters
T

Definition at line 106 of file entity.h.

106 {
107 static_assert(std::is_base_of<Component, T>::value, "T must derive from Component");
108
110 auto it = m_Components.find(typeID);
111
112 if (it != m_Components.end()) {
113 m_Components.erase(it);
114 }
115 }

◆ UpdateComponents()

void ecs::Entity::UpdateComponents ( float deltaTime)
inline

Update all components of the entity.

Parameters
deltaTime

Definition at line 121 of file entity.h.

121 {
122 for (auto& [typeID, component] : m_Components) {
123 component->Update(deltaTime);
124 }
125 }

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