(Entity) Changed template name from C to ComponentType

Former-commit-id: 780c96238f5f976b9caa86ec982bec7b5fd9ee8a
This commit is contained in:
Lynix 2015-03-01 11:19:54 +01:00
parent 699b580516
commit 8b77825fb8
2 changed files with 32 additions and 31 deletions

View File

@ -29,23 +29,23 @@ namespace Ndk
Entity(Entity&& entity); Entity(Entity&& entity);
~Entity(); ~Entity();
template<typename Component, typename... Args> Component& AddComponent(Args&&... args); template<typename ComponentType, typename... Args> ComponentType& AddComponent(Args&&... args);
EntityHandle CreateHandle(); EntityHandle CreateHandle();
template<typename Component> Component& GetComponent(); template<typename ComponentType> ComponentType& GetComponent();
template<typename Component> const Component& GetComponent() const; template<typename ComponentType> const ComponentType& GetComponent() const;
Id GetId() const; Id GetId() const;
World* GetWorld() const; World* GetWorld() const;
template<typename Component> bool HasComponent() const; template<typename ComponentType> bool HasComponent() const;
void Kill(); void Kill();
bool IsValid() const; bool IsValid() const;
void RemoveAllComponent(); void RemoveAllComponent();
template<typename Component> void RemoveComponent(); template<typename ComponentType> void RemoveComponent();
Entity& operator=(const Entity&) = delete; Entity& operator=(const Entity&) = delete;
Entity& operator=(Entity&&) = delete; Entity& operator=(Entity&&) = delete;

View File

@ -6,6 +6,7 @@
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <algorithm> #include <algorithm>
#include <stdexcept> #include <stdexcept>
#include <type_traits>
#include <utility> #include <utility>
namespace Ndk namespace Ndk
@ -16,58 +17,58 @@ namespace Ndk
{ {
} }
template<typename C, typename... Args> template<typename ComponentType, typename... Args>
C& Entity::AddComponent(Args&&... args) ComponentType& Entity::AddComponent(Args&&... args)
{ {
static_assert(std::is_base_of<BaseComponent, C>(), "C is not a component"); static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
// Nous supprimons l'ancien component, s'il existe // Nous supprimons l'ancien component, s'il existe
RemoveComponent<C>(); RemoveComponent<ComponentType>();
// Récupération de l'identification du component, qui va nous servir d'indice // Récupération de l'identification du component, qui va nous servir d'indice
nzUInt32 componentId = C::ComponentId; 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);
// Allocation et affectation du component // Allocation et affectation du component
std::unique_ptr<C> ptr(new C(std::forward(args)...)); std::unique_ptr<ComponentType> ptr(new ComponentType(std::forward(args)...));
C* component = ptr.get(); ComponentType* component = ptr.get();
m_components[componentId] = std::move(ptr); m_components[componentId] = std::move(ptr);
return *component; return *component;
} }
template<typename C> template<typename ComponentType>
C& Entity::GetComponent() ComponentType& Entity::GetComponent()
{ {
///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, C>(), "C is not a component"); static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
if (!HasComponent<C>()) if (!HasComponent<ComponentType>())
throw std::runtime_error("Tried to get a non-present component"); throw std::runtime_error("Tried to get a non-present component");
BaseComponent* component = m_components[C::ComponentId].get(); BaseComponent* component = m_components[ComponentType::ComponentId].get();
NazaraAssert(component, "Invalid component pointer"); NazaraAssert(component, "Invalid component pointer");
return *static_cast<C*>(component); return *static_cast<ComponentType*>(component);
} }
template<typename C> template<typename ComponentType>
const C& Entity::GetComponent() const const ComponentType& Entity::GetComponent() const
{ {
///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, C>(), "C is not a component"); static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
if (!HasComponent<C>()) if (!HasComponent<ComponentType>())
throw std::runtime_error("Tried to get a non-present component"); throw std::runtime_error("Tried to get a non-present component");
BaseComponent* component = m_components[C::ComponentId].get(); BaseComponent* component = m_components[ComponentType::ComponentId].get();
NazaraAssert(component, "Invalid component pointer"); NazaraAssert(component, "Invalid component pointer");
return *static_cast<C*>(component); return *static_cast<ComponentType*>(component);
} }
inline Entity::Id Entity::GetId() const inline Entity::Id Entity::GetId() const
@ -80,12 +81,12 @@ namespace Ndk
return m_world; return m_world;
} }
template<typename C> template<typename ComponentType>
bool Entity::HasComponent() const bool Entity::HasComponent() const
{ {
static_assert(std::is_base_of<BaseComponent, C>(), "C is not a component"); static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
nzUInt32 componentId = C::ComponentId; nzUInt32 componentId = ComponentType::ComponentId;
return m_components.size() > componentId && m_components[componentId]; return m_components.size() > componentId && m_components[componentId];
} }
@ -94,13 +95,13 @@ namespace Ndk
m_components.clear(); m_components.clear();
} }
template<typename C> template<typename ComponentType>
void Entity::RemoveComponent() void Entity::RemoveComponent()
{ {
static_assert(std::is_base_of<BaseComponent, C>(), "C is not a component"); static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
if (HasComponent<C>()) if (HasComponent<ComponentType>())
m_components[C::ComponentId].reset(); m_components[ComponentType::ComponentId].reset();
} }
inline void Entity::RegisterHandle(EntityHandle* handle) inline void Entity::RegisterHandle(EntityHandle* handle)