#pragma once #include "component.h" #include #include #include #include #include namespace ecs { /** * Type alias for a unique identifier for an entity. */ using EntityID = uint32_t; class Entity { public: /** * Builder for an Entity with a unique ID. */ explicit Entity(EntityID id) : m_ID(id) {} ~Entity() = default; /** * No copy constructor or assignment operator to prevent copying. */ Entity(const Entity&) = delete; Entity& operator=(const Entity&) = delete; /** * Move constructor and assignment operator to allow moving entities. */ Entity(Entity&&) = default; Entity& operator=(Entity&&) = default; /** * Get the unique identifier for the entity. * @return The unique ID of the entity. */ EntityID GetID() const { return m_ID; } /** * Add a component of type T to the entity. * If the component already exists, it returns the existing component. * @tparam T * @param args Arguments to construct the component. * @return A shared pointer to the added or existing component. */ template std::shared_ptr AddComponent(Args&&... args) { static_assert(std::is_base_of::value, "T must derive from Component"); ComponentTypeID typeID = GetComponentTypeID(); // Vérifier si le composant existe déjà if (m_Components.find(typeID) != m_Components.end()) { return std::static_pointer_cast(m_Components[typeID]); } // Créer et ajouter le composant auto component = std::make_shared(std::forward(args)...); m_Components[typeID] = component; // Initialiser le composant component->Initialize(); return component; } /** * Get a component of type T from the entity. * @tparam T * @return A shared pointer to the component if it exists, nullptr otherwise. */ template std::shared_ptr GetComponent() { static_assert(std::is_base_of::value, "T must derive from Component"); ComponentTypeID typeID = GetComponentTypeID(); auto it = m_Components.find(typeID); if (it != m_Components.end()) { return std::static_pointer_cast(it->second); } return nullptr; } /** * Check if the entity has a component of type T. * @tparam T * @return true if the entity has the component, false otherwise. */ template bool HasComponent() const { static_assert(std::is_base_of::value, "T must derive from Component"); ComponentTypeID typeID = GetComponentTypeID(); return m_Components.find(typeID) != m_Components.end(); } /** * Remove a component of type T from the entity. * @tparam T */ template void RemoveComponent() { static_assert(std::is_base_of::value, "T must derive from Component"); ComponentTypeID typeID = GetComponentTypeID(); auto it = m_Components.find(typeID); if (it != m_Components.end()) { m_Components.erase(it); } } /** * Update all components of the entity. * @param deltaTime */ void UpdateComponents(float deltaTime) { for (auto& [typeID, component] : m_Components) { component->Update(deltaTime); } } /** * Get all components of the entity. * @return A constant reference to the map of components. */ const std::unordered_map& GetAllComponents() const { return m_Components; } private: /** * Unique identifier for the entity. */ EntityID m_ID; /** * Map to hold components associated with the entity. * The key is the type ID of the component, and the value is a shared pointer to the component. */ std::unordered_map m_Components; }; } // namespace ecs