# Khaotic Engine Reborn: ecs::Entity Class Reference

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

## Public Member Functions

| Entity (EntityID id)
| Entity (const Entity &)=delete
Entity & | operator= (const Entity &)=delete
| Entity (Entity &&)=default
Entity & | operator= (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.

: 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

  args | Arguments to construct the component.

Returns
  A shared pointer to the added or existing component.

Definition at line 49 of file entity.h.

{
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;
}
ecs::ComponentTypeID
std::type_index ComponentTypeID
Definition component.h:48
ecs::GetComponentTypeID
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.

{
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;
}

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

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

{
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();
}

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

{
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);
}
}

## UpdateComponents()

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

inline

Update all components of the entity.

Parameters

  deltaTime |

Definition at line 121 of file entity.h.

{
for (auto& [typeID, component] : m_Components) {
component->Update(deltaTime);
}
}

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

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