Major - ECS - V12.0.0
This commit is contained in:
101
enginecustom/src/inc/system/ecs/entity.h
Normal file
101
enginecustom/src/inc/system/ecs/entity.h
Normal file
@@ -0,0 +1,101 @@
|
||||
#pragma once
|
||||
#include "component.h"
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
namespace ecs {
|
||||
|
||||
// Identifiant unique pour les entit<69>s
|
||||
using EntityID = uint32_t;
|
||||
|
||||
class Entity {
|
||||
public:
|
||||
explicit Entity(EntityID id) : m_ID(id) {}
|
||||
~Entity() = default;
|
||||
|
||||
// Emp<6D>cher la copie
|
||||
Entity(const Entity&) = delete;
|
||||
Entity& operator=(const Entity&) = delete;
|
||||
|
||||
// Permettre le d<>placement
|
||||
Entity(Entity&&) = default;
|
||||
Entity& operator=(Entity&&) = default;
|
||||
|
||||
// Getter pour l'ID
|
||||
EntityID GetID() const { return m_ID; }
|
||||
|
||||
// Ajouter un composant
|
||||
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<EFBFBD>
|
||||
if (m_Components.find(typeID) != m_Components.end()) {
|
||||
return std::static_pointer_cast<T>(m_Components[typeID]);
|
||||
}
|
||||
|
||||
// Cr<43>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;
|
||||
}
|
||||
|
||||
// R<>cup<75>rer un composant
|
||||
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;
|
||||
}
|
||||
|
||||
// V<>rifier si l'entit<69> poss<73>de un composant
|
||||
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();
|
||||
}
|
||||
|
||||
// Supprimer un composant
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre <20> jour tous les composants
|
||||
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
|
Reference in New Issue
Block a user