(NDK) Modified component/system registration interface
Let's hope it's the last time I do this Former-commit-id: 75f8e63e90e067a7d21efa6a1226ae4bbe050ab9
This commit is contained in:
parent
28b0e26a88
commit
6a99b38be1
|
|
@ -26,14 +26,11 @@ namespace Ndk
|
||||||
|
|
||||||
ComponentIndex GetIndex() const;
|
ComponentIndex GetIndex() const;
|
||||||
|
|
||||||
template<typename ComponentType, unsigned int N>
|
|
||||||
static ComponentIndex Register(const char (&name)[N]);
|
|
||||||
|
|
||||||
static ComponentIndex Register(ComponentId id, Factory factoryFunc);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ComponentIndex m_componentIndex;
|
ComponentIndex m_componentIndex;
|
||||||
|
|
||||||
|
static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct ComponentEntry
|
struct ComponentEntry
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Ndk/Algorithm.hpp>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
|
|
@ -18,25 +16,7 @@ namespace Ndk
|
||||||
return m_componentIndex;
|
return m_componentIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ComponentType, unsigned int N>
|
inline ComponentIndex BaseComponent::RegisterComponent(ComponentId id, Factory factoryFunc)
|
||||||
ComponentIndex BaseComponent::Register(const char (&name)[N])
|
|
||||||
{
|
|
||||||
// Il faut que notre composant possède un constructeur par défaut (pour la factory)
|
|
||||||
static_assert(std::is_default_constructible<ComponentType>::value, "ComponentType should be default-constructible");
|
|
||||||
|
|
||||||
// On récupère la chaîne de caractère sous la forme d'un nombre qui servira d'identifiant unique
|
|
||||||
ComponentId id = BuildComponentId(name);
|
|
||||||
|
|
||||||
// On utilise les lambda pour créer une fonction factory
|
|
||||||
auto factory = []() -> BaseComponent*
|
|
||||||
{
|
|
||||||
return new ComponentType;
|
|
||||||
};
|
|
||||||
|
|
||||||
return Register(id, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline ComponentIndex BaseComponent::Register(ComponentId id, Factory factoryFunc)
|
|
||||||
{
|
{
|
||||||
// Nous allons rajouter notre composant à la fin
|
// Nous allons rajouter notre composant à la fin
|
||||||
ComponentIndex index = s_entries.size();
|
ComponentIndex index = s_entries.size();
|
||||||
|
|
|
||||||
|
|
@ -34,13 +34,13 @@ namespace Ndk
|
||||||
|
|
||||||
bool HasEntity(const Entity* entity) const;
|
bool HasEntity(const Entity* entity) const;
|
||||||
|
|
||||||
static SystemIndex GetNextIndex();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
template<typename ComponentType> void Excludes();
|
template<typename ComponentType> void Excludes();
|
||||||
template<typename ComponentType1, typename ComponentType2, typename... Rest> void Excludes();
|
template<typename ComponentType1, typename ComponentType2, typename... Rest> void Excludes();
|
||||||
void ExcludesComponent(ComponentIndex index);
|
void ExcludesComponent(ComponentIndex index);
|
||||||
|
|
||||||
|
static SystemIndex GetNextIndex();
|
||||||
|
|
||||||
template<typename ComponentType> void Requires();
|
template<typename ComponentType> void Requires();
|
||||||
template<typename ComponentType1, typename ComponentType2, typename... Rest> void Requires();
|
template<typename ComponentType1, typename ComponentType2, typename... Rest> void Requires();
|
||||||
void RequiresComponent(ComponentIndex index);
|
void RequiresComponent(ComponentIndex index);
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,6 @@ namespace Ndk
|
||||||
return m_entityBits.UnboundedTest(entity->GetId());
|
return m_entityBits.UnboundedTest(entity->GetId());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline SystemIndex BaseSystem::GetNextIndex()
|
|
||||||
{
|
|
||||||
return s_nextIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ComponentType>
|
template<typename ComponentType>
|
||||||
void BaseSystem::Excludes()
|
void BaseSystem::Excludes()
|
||||||
{
|
{
|
||||||
|
|
@ -60,6 +55,11 @@ namespace Ndk
|
||||||
m_excludedComponents.UnboundedSet(index);
|
m_excludedComponents.UnboundedSet(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline SystemIndex BaseSystem::GetNextIndex()
|
||||||
|
{
|
||||||
|
return s_nextIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename ComponentType>
|
template<typename ComponentType>
|
||||||
void BaseSystem::Requires()
|
void BaseSystem::Requires()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,11 @@ namespace Ndk
|
||||||
virtual ~Component();
|
virtual ~Component();
|
||||||
|
|
||||||
BaseComponent* Clone() const override;
|
BaseComponent* Clone() const override;
|
||||||
|
|
||||||
|
static ComponentIndex RegisterComponent(ComponentId id);
|
||||||
|
|
||||||
|
template<unsigned int N>
|
||||||
|
static ComponentIndex RegisterComponent(const char (&name)[N]);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// This file is part of the "Nazara Development Kit"
|
// This file is part of the "Nazara Development Kit"
|
||||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||||
|
|
||||||
|
#include <Ndk/Algorithm.hpp>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
|
|
@ -23,4 +24,31 @@ namespace Ndk
|
||||||
|
|
||||||
return new ComponentType(static_cast<const ComponentType&>(*this));
|
return new ComponentType(static_cast<const ComponentType&>(*this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ComponentType>
|
||||||
|
ComponentIndex Component<ComponentType>::RegisterComponent(ComponentId id)
|
||||||
|
{
|
||||||
|
// Il faut que notre composant possède un constructeur par défaut (pour la factory)
|
||||||
|
static_assert(std::is_default_constructible<ComponentType>::value, "ComponentType should be default-constructible");
|
||||||
|
|
||||||
|
// On utilise les lambda pour créer une fonction factory
|
||||||
|
auto factory = []() -> BaseComponent*
|
||||||
|
{
|
||||||
|
return new ComponentType;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Je ne sais pas si c'est un bug de GCC ou si c'est quelque chose que j'ai mal compris
|
||||||
|
// mais le fait est que ça ne compile pas si je ne précise pas Basecomponent::
|
||||||
|
return BaseComponent::RegisterComponent(id, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ComponentType>
|
||||||
|
template<unsigned int N>
|
||||||
|
ComponentIndex Component<ComponentType>::RegisterComponent(const char (&name)[N])
|
||||||
|
{
|
||||||
|
// On récupère la chaîne de caractère sous la forme d'un nombre qui servira d'identifiant unique
|
||||||
|
ComponentId id = BuildComponentId(name);
|
||||||
|
return RegisterComponent(id);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ namespace Ndk
|
||||||
virtual ~System();
|
virtual ~System();
|
||||||
|
|
||||||
BaseSystem* Clone() const override;
|
BaseSystem* Clone() const override;
|
||||||
|
|
||||||
|
static SystemIndex RegisterSystem();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,4 +24,11 @@ namespace Ndk
|
||||||
|
|
||||||
return new SystemType(static_cast<const SystemType&>(*this));
|
return new SystemType(static_cast<const SystemType&>(*this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename SystemType>
|
||||||
|
SystemIndex System<SystemType>::RegisterSystem()
|
||||||
|
{
|
||||||
|
return GetNextIndex();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue