Architecture rework for components to accomodate for reflection

This commit is contained in:
WiggleWizard
2024-09-12 18:37:08 +01:00
parent bf6fb0f8b6
commit 7d66a19ab2
31 changed files with 302 additions and 90 deletions

View File

@@ -1,16 +1,46 @@
#pragma once
#include <entt/entt.hpp>
#include <type_traits>
#define NK_HASHED_FN_NAME_IMPL(name) inline static constexpr entt::hashed_string name = entt::hashed_string(#name);
#define NK_HASHED_STATIC_STR(name) inline static constexpr entt::hashed_string name = entt::hashed_string(#name);
namespace Nuake
{
struct HashedFnName
{
NK_HASHED_FN_NAME_IMPL(GetComponentName)
NK_HASHED_FN_NAME_IMPL(AddToEntity)
NK_HASHED_STATIC_STR(GetComponentName)
NK_HASHED_STATIC_STR(AddToEntity)
};
struct HashedName
{
NK_HASHED_STATIC_STR(DisplayName)
};
enum class ComponentTypeTrait : uint16_t
{
None = 0,
InspectorExposed = 1 << 0,
};
template <typename Enum>
constexpr std::underlying_type_t<Enum> ToUnderlying(Enum e)
{
return static_cast<std::underlying_type_t<Enum>>(e);
}
inline ComponentTypeTrait operator|(ComponentTypeTrait lhs, ComponentTypeTrait rhs)
{
return static_cast<ComponentTypeTrait>(ToUnderlying(lhs) | ToUnderlying(rhs));
}
inline ComponentTypeTrait operator&(ComponentTypeTrait lhs, ComponentTypeTrait rhs)
{
return static_cast<ComponentTypeTrait>(ToUnderlying(lhs) & ToUnderlying(rhs));
}
}
#define NUAKECOMPONENT(klass, componentName) \
@@ -32,7 +62,33 @@ public:
enttRegistry->emplace_or_replace<klass>(entity); \
} \
\
inline static auto ComponentFactory = entt::meta<klass>() \
.type(entt::hashed_string(#klass)) \
.func<&klass::ComponentName>(HashedFnName::GetComponentName) \
.func<&klass::AddToEntity>(HashedFnName::AddToEntity);
\
static void (*GetInitializeComponentClass())() \
{ \
return &klass::InitializeComponentClass; \
} \
\
inline static auto ComponentFactory = entt::meta<klass>(); \
static void InternalInitializeClass() \
{ \
static bool initialized = false; \
if (initialized) \
return; \
\
ComponentFactory.type(entt::hashed_string(#klass)) \
.traits(ComponentTypeTrait::InspectorExposed); \
ComponentFactory.func<&klass::ComponentName>(HashedFnName::GetComponentName); \
ComponentFactory.func<&klass::AddToEntity>(HashedFnName::AddToEntity); \
\
if (klass::GetInitializeComponentClass() != Component::GetInitializeComponentClass()) \
{ \
GetInitializeComponentClass()(); \
} \
\
initialized = true; \
}
#define BINDCOMPONENTFIELD(field, displayName) \
ComponentFactory \
.data<&field>(entt::hashed_string(#field)) \
.prop(HashedName::DisplayName, displayName)