(Entity) Added non-template version of components handling

Former-commit-id: 0f9795a0b130e6e5ebb26782459d79bde1b0efa1
This commit is contained in:
Lynix 2015-03-01 13:42:17 +01:00
parent 7c73407fa4
commit c71c050831
2 changed files with 47 additions and 37 deletions

View File

@ -8,7 +8,7 @@
#define NDK_ENTITY_HPP #define NDK_ENTITY_HPP
#include <NDK/Prerequesites.hpp> #include <NDK/Prerequesites.hpp>
#include <NDK/BaseComponent.hpp> #include <NDK/Component.hpp>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -29,15 +29,17 @@ namespace Ndk
Entity(Entity&& entity); Entity(Entity&& entity);
~Entity(); ~Entity();
BaseComponent& AddComponent(std::unique_ptr<BaseComponent>&& component);
template<typename ComponentType, typename... Args> ComponentType& AddComponent(Args&&... args); template<typename ComponentType, typename... Args> ComponentType& AddComponent(Args&&... args);
EntityHandle CreateHandle(); EntityHandle CreateHandle();
BaseComponent& GetComponent(nzUInt32 componentId);
template<typename ComponentType> ComponentType& GetComponent(); template<typename ComponentType> ComponentType& GetComponent();
template<typename ComponentType> const ComponentType& GetComponent() const;
Id GetId() const; Id GetId() const;
World* GetWorld() const; World* GetWorld() const;
bool HasComponent(nzUInt32 componentId) const;
template<typename ComponentType> bool HasComponent() const; template<typename ComponentType> bool HasComponent() const;
void Kill(); void Kill();
@ -45,6 +47,7 @@ namespace Ndk
bool IsValid() const; bool IsValid() const;
void RemoveAllComponent(); void RemoveAllComponent();
void RemoveComponent(nzUInt32 componentId);
template<typename ComponentType> void RemoveComponent(); template<typename ComponentType> void RemoveComponent();
Entity& operator=(const Entity&) = delete; Entity& operator=(const Entity&) = delete;

View File

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