diff --git a/SDK/include/NDK/Entity.hpp b/SDK/include/NDK/Entity.hpp index eedbc4082..8a0e09f78 100644 --- a/SDK/include/NDK/Entity.hpp +++ b/SDK/include/NDK/Entity.hpp @@ -8,7 +8,7 @@ #define NDK_ENTITY_HPP #include -#include +#include #include #include @@ -29,15 +29,17 @@ namespace Ndk Entity(Entity&& entity); ~Entity(); + BaseComponent& AddComponent(std::unique_ptr&& component); template ComponentType& AddComponent(Args&&... args); EntityHandle CreateHandle(); + BaseComponent& GetComponent(nzUInt32 componentId); template ComponentType& GetComponent(); - template const ComponentType& GetComponent() const; Id GetId() const; World* GetWorld() const; + bool HasComponent(nzUInt32 componentId) const; template bool HasComponent() const; void Kill(); @@ -45,6 +47,7 @@ namespace Ndk bool IsValid() const; void RemoveAllComponent(); + void RemoveComponent(nzUInt32 componentId); template void RemoveComponent(); Entity& operator=(const Entity&) = delete; diff --git a/SDK/include/NDK/Entity.inl b/SDK/include/NDK/Entity.inl index 7f8786bf2..c50903b5d 100644 --- a/SDK/include/NDK/Entity.inl +++ b/SDK/include/NDK/Entity.inl @@ -17,26 +17,42 @@ namespace Ndk { } - template - ComponentType& Entity::AddComponent(Args&&... args) + inline BaseComponent& Entity::AddComponent(std::unique_ptr&& component) { - static_assert(std::is_base_of(), "ComponentType is not a component"); + NazaraAssert(component, "Component must be valid"); + + nzUInt32 componentId = component->GetId(); // Nous supprimons l'ancien component, s'il existe - RemoveComponent(); - - // Récupération de l'identification du component, qui va nous servir d'indice - nzUInt32 componentId = ComponentType::ComponentId; + RemoveComponent(componentId); // Nous nous assurons que le vecteur de component est suffisamment grand pour contenir le nouveau component if (m_components.size() <= componentId) m_components.resize(componentId + 1); + // Affectation et retour du component + m_components[componentId] = std::move(component); + + return *m_components[componentId].get(); + } + + template + ComponentType& Entity::AddComponent(Args&&... args) + { + static_assert(std::is_base_of(), "ComponentType is not a component"); + // Allocation et affectation du component std::unique_ptr ptr(new ComponentType(std::forward(args)...)); - ComponentType* component = ptr.get(); + return static_cast(AddComponent(std::move(ptr))); + } - m_components[componentId] = std::move(ptr); + inline BaseComponent& Entity::GetComponent(nzUInt32 componentId) + { + if (!HasComponent(componentId)) + throw std::runtime_error("Tried to get a non-present component"); + + BaseComponent* component = m_components[componentId].get(); + NazaraAssert(component, "Invalid component pointer"); return *component; } @@ -47,28 +63,8 @@ namespace Ndk ///DOC: Lance une exception si le component n'est pas présent static_assert(std::is_base_of(), "ComponentType is not a component"); - if (!HasComponent()) - throw std::runtime_error("Tried to get a non-present component"); - - BaseComponent* component = m_components[ComponentType::ComponentId].get(); - NazaraAssert(component, "Invalid component pointer"); - - return *static_cast(component); - } - - template - const ComponentType& Entity::GetComponent() const - { - ///DOC: Lance une exception si le component n'est pas présent - static_assert(std::is_base_of(), "ComponentType is not a component"); - - if (!HasComponent()) - throw std::runtime_error("Tried to get a non-present component"); - - BaseComponent* component = m_components[ComponentType::ComponentId].get(); - NazaraAssert(component, "Invalid component pointer"); - - return *static_cast(component); + nzUInt32 componentId = GetComponentId(); + return static_cast(GetComponent(componentId)); } inline Entity::Id Entity::GetId() const @@ -81,13 +77,18 @@ namespace Ndk return m_world; } + inline bool Entity::HasComponent(nzUInt32 componentId) const + { + return m_components.size() > componentId && m_components[componentId]; + } + template bool Entity::HasComponent() const { static_assert(std::is_base_of(), "ComponentType is not a component"); - nzUInt32 componentId = ComponentType::ComponentId; - return m_components.size() > componentId && m_components[componentId]; + nzUInt32 componentId = GetComponentId(); + return HasComponent(componentId); } inline void Entity::RemoveAllComponent() @@ -95,13 +96,19 @@ namespace Ndk m_components.clear(); } + inline void Entity::RemoveComponent(nzUInt32 componentId) + { + if (HasComponent(componentId)) + m_components[componentId].reset(); + } + template void Entity::RemoveComponent() { static_assert(std::is_base_of(), "ComponentType is not a component"); - if (HasComponent()) - m_components[ComponentType::ComponentId].reset(); + nzUInt32 componentId = GetComponentId(); + RemoveComponent(componentId); } inline void Entity::RegisterHandle(EntityHandle* handle)