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

@@ -13,6 +13,7 @@
#include "src/Scripting/ScriptingEngine.h"
#include "src/Scripting/ScriptingEngineNet.h"
#include "src/Threading/JobSystem.h"
#include "src/Core/RegisterCoreTypes.h"
#include "src/Modules/Modules.h"
#include <GLFW/glfw3.h>
@@ -20,6 +21,8 @@
#include <imgui/imgui_impl_opengl3.h>
#include <Tracy.hpp>
namespace Nuake
{
Ref<Project> Engine::currentProject;
@@ -48,6 +51,8 @@ namespace Nuake
Renderer2D::Init();
Logger::Log("Engine initialized");
RegisterCoreTypes::RegisterCoreComponents();
Modules::StartupModules();
}

View File

@@ -0,0 +1,5 @@
#include "ClassDB.h"
namespace Nuake
{
}

View File

@@ -0,0 +1,14 @@
#pragma once
namespace Nuake
{
class ClassDB
{
public:
template<class T>
static void RegisterComponent(T klass)
{
T::InternalInitializeClass();
}
};
}

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)

View File

@@ -0,0 +1,47 @@
#include "RegisterCoreTypes.h"
#include "src/Scene/Components/AudioEmitterComponent.h"
#include "src/Scene/Components/BoneComponent.h"
#include "src/Scene/Components/BoxCollider.h"
#include "src/Scene/Components/CameraComponent.h"
#include "src/Scene/Components/CapsuleColliderComponent.h"
#include "src/Scene/Components/CharacterControllerComponent.h"
#include "src/Scene/Components/CylinderColliderComponent.h"
#include "src/Scene/Components/MeshCollider.h"
#include "src/Scene/Components/ModelComponent.h"
#include "src/Scene/Components/NavMeshVolumeComponent.h"
#include "src/Scene/Components/NetScriptComponent.h"
#include "src/Scene/Components/ParticleEmitterComponent.h"
#include "src/Scene/Components/QuakeMap.h"
#include "src/Scene/Components/RigidbodyComponent.h"
#include "src/Scene/Components/SkinnedModelComponent.h"
#include "src/Scene/Components/SphereCollider.h"
#include "src/Scene/Components/SpriteComponent.h"
#include "src/Scene/Components/UIComponent.h"
namespace Nuake
{
void RegisterCoreTypes::RegisterCoreComponents()
{
UIComponent::InternalInitializeClass();
SpriteComponent::InternalInitializeClass();
SphereColliderComponent::InternalInitializeClass();
SkinnedModelComponent::InternalInitializeClass();
RigidBodyComponent::InternalInitializeClass();
QuakeMapComponent::InternalInitializeClass();
ParticleEmitterComponent::InternalInitializeClass();
NetScriptComponent::InternalInitializeClass();
NavMeshVolumeComponent::InternalInitializeClass();
ModelComponent::InternalInitializeClass();
MeshColliderComponent::InternalInitializeClass();
LightComponent::InternalInitializeClass();
CylinderColliderComponent::InternalInitializeClass();
CharacterControllerComponent::InternalInitializeClass();
CapsuleColliderComponent::InternalInitializeClass();
CameraComponent::InternalInitializeClass();
BoxColliderComponent::InternalInitializeClass();
BoneComponent::InternalInitializeClass();
AudioEmitterComponent::InternalInitializeClass();
}
}

View File

@@ -0,0 +1,11 @@
#pragma once
namespace Nuake
{
class RegisterCoreTypes
{
public:
static void RegisterCoreComponents();
};
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include "Components/AudioEmitterComponent.h"
#include "Components/BaseComponent.h"
#include "Components/VisibilityComponent.h"
#include "Components/BoneComponent.h"
#include "Components/BoxCollider.h"
#include "Components/BSPBrushComponent.h"

View File

@@ -1,12 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/Core.h"
#include "src/Resource/Serializable.h"
namespace Nuake {
class AudioEmitterComponent
class AudioEmitterComponent : public Component
{
NUAKECOMPONENT(AudioEmitterComponent, "Audio Emitter")

View File

@@ -1,4 +1,7 @@
#pragma once
#include "Component.h"
#include "src/Core/Core.h"
#include "src/Core/Maths.h"
@@ -7,7 +10,7 @@
#include "src/Physics/Rigibody.h"
namespace Nuake {
class BSPBrushComponent
class BSPBrushComponent : public Component
{
public:
//std::vector<Ref<Mesh>> Meshes;

View File

@@ -1,13 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/Core.h"
#include "src/Resource/Serializable.h"
namespace Nuake
{
class BoneComponent
class BoneComponent : public Component
{
NUAKECOMPONENT(BoneComponent, "Bone")

View File

@@ -1,22 +1,41 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
namespace Nuake {
namespace Nuake
{
class BoxColliderComponent : public Component
{
NUAKECOMPONENT(BoxColliderComponent, "Box Collider")
class BoxColliderComponent
{
NUAKECOMPONENT(BoxColliderComponent, "Box Collider")
static void InitializeComponentClass()
{
BINDCOMPONENTFIELD(BoxColliderComponent::IsTrigger, "Is Trigger");
public:
Ref<Physics::PhysicShape> Box;
Vector3 Size = Vector3(0.5f, 0.5f, 0.5f);
bool IsTrigger = false;
ComponentFactory
.data<&BoxColliderComponent::SetSize, &BoxColliderComponent::GetSize>(entt::hashed_string("size"))
.prop(HashedName::DisplayName, "Size");
}
json Serialize();
bool Deserialize(const json& j);
};
public:
Ref<Physics::PhysicShape> Box;
Vector3 Size = Vector3(0.5f, 0.5f, 0.5f);
bool IsTrigger = true;
void SetSize(const Vector3& newSize)
{
Size = newSize;
}
Vector3 GetSize()
{
return Size;
}
json Serialize();
bool Deserialize(const json& j);
};
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "TransformComponent.h"
#include "src/Core/Core.h"
@@ -10,7 +10,7 @@
namespace Nuake
{
class CameraComponent
class CameraComponent : public Component
{
NUAKECOMPONENT(CameraComponent, "Camera")

View File

@@ -1,12 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
namespace Nuake
{
class CapsuleColliderComponent
class CapsuleColliderComponent : public Component
{
NUAKECOMPONENT(CapsuleColliderComponent, "Capsule Collider")

View File

@@ -1,11 +1,12 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Physics/CharacterController.h"
namespace Nuake
{
class CharacterControllerComponent
class CharacterControllerComponent : public Component
{
NUAKECOMPONENT(CharacterControllerComponent, "Character Controller")

View File

@@ -0,0 +1,15 @@
#include "Component.h"
using namespace Nuake;
std::string Component::GetName(const entt::meta_type& componentMeta)
{
// TODO: [WiggleWizard] Needs some error handling
if (entt::meta_func func = componentMeta.func(HashedFnName::GetComponentName))
{
entt::meta_any ret = func.invoke(componentMeta);
return ret.cast<std::string>();
}
return "Unknown Component";
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "src/Core/Object/Object.h"
namespace Nuake
{
class Component
{
protected:
static void InitializeComponentClass() {}
static void (* GetInitializeComponentClass(void))()
{
return &Component::InitializeComponentClass;
}
public:
static std::string GetName(const entt::meta_type& componentMeta);
};
}

View File

@@ -1,12 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
namespace Nuake
{
class CylinderColliderComponent
class CylinderColliderComponent : public Component
{
NUAKECOMPONENT(CylinderColliderComponent, "Cylinder Collider")

View File

@@ -1,13 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include <glm/ext/vector_float3.hpp>
#include <glm/ext/vector_float2.hpp>
#include "TransformComponent.h"
#include "../Rendering/Camera.h"
#include "src/Rendering/Buffers/Framebuffer.h"
#include "BaseComponent.h"
#include "VisibilityComponent.h"
#include "../Resource/Serializable.h"
#include <glm/ext/matrix_clip_space.hpp>
@@ -20,7 +20,7 @@ namespace Nuake
};
const int CSM_AMOUNT = 4;
class LightComponent
class LightComponent : public Component
{
NUAKECOMPONENT(LightComponent, "Light")

View File

@@ -1,11 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
namespace Nuake {
class MeshColliderComponent
namespace Nuake
{
class MeshColliderComponent : public Component
{
NUAKECOMPONENT(MeshColliderComponent, "Mesh Collider")

View File

@@ -1,6 +1,7 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/String.h"
#include "src/Resource/Model.h"
#include "src/Resource/ResourceLoader.h"
@@ -12,7 +13,7 @@
namespace Nuake
{
struct ModelComponent
struct ModelComponent : public Component
{
NUAKECOMPONENT(ModelComponent, "Model")

View File

@@ -1,13 +1,14 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "../Entities/Entity.h"
#include "Engine.h"
#include <src/AI/NavMesh.h>
namespace Nuake
{
struct NavMeshVolumeComponent
struct NavMeshVolumeComponent : public Component
{
NUAKECOMPONENT(NavMeshVolumeComponent, "Nav Mesh Volume")

View File

@@ -1,6 +1,7 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/Core.h"
#include "src/Core/Maths.h"
#include "src/FileSystem/FileSystem.h"
@@ -34,7 +35,7 @@ namespace Nuake {
NetScriptExposedVarType Type;
};
class NetScriptComponent
class NetScriptComponent : public Component
{
NUAKECOMPONENT(NetScriptComponent, "Net Script")

View File

@@ -1,6 +1,7 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/Core.h"
#include "src/Core/Maths.h"
#include "src/Resource/Serializable.h"
@@ -10,7 +11,7 @@
namespace Nuake
{
class ParticleEmitterComponent
class ParticleEmitterComponent : public Component
{
NUAKECOMPONENT(ParticleEmitterComponent, "Particle Emitter")

View File

@@ -1,17 +1,19 @@
#pragma once
#include <string>
#include <vector>
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Rendering/Mesh/Mesh.h"
#include "src/Resource/Serializable.h"
#include "src/Scene/Systems/QuakeMapBuilder.h"
#include "src/Scene/Entities/Entity.h"
#include "Engine.h"
#include <string>
#include <vector>
namespace Nuake {
class QuakeMapComponent
class QuakeMapComponent : public Component
{
NUAKECOMPONENT(QuakeMapComponent, "Quake Map")

View File

@@ -1,8 +1,9 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "TransformComponent.h"
#include "BaseComponent.h"
#include "VisibilityComponent.h"
#include "src/Core/Core.h"
namespace Nuake {
@@ -11,7 +12,7 @@ namespace Nuake {
class RigidBody;
};
class RigidBodyComponent
class RigidBodyComponent : public Component
{
NUAKECOMPONENT(RigidBodyComponent, "Rigid Body")

View File

@@ -1,16 +1,16 @@
#pragma once
#include <vector>
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Resource/Serializable.h"
#include "src/Resource/SkinnedModel.h"
#include <string>
#include <vector>
namespace Nuake
{
struct SkinnedModelComponent
class SkinnedModelComponent : public Component
{
NUAKECOMPONENT(SkinnedModelComponent, "Skinned Model")

View File

@@ -1,11 +1,13 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Physics/PhysicsShapes.h"
#include "src/Core/Core.h"
namespace Nuake {
class SphereColliderComponent
namespace Nuake
{
class SphereColliderComponent : public Component
{
NUAKECOMPONENT(SphereColliderComponent, "Sphere Component")

View File

@@ -1,6 +1,7 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/Core.h"
#include "src/Resource/Serializable.h"
#include "src/Rendering/Textures/Texture.h"
@@ -8,7 +9,7 @@
namespace Nuake
{
class SpriteComponent
class SpriteComponent : public Component
{
NUAKECOMPONENT(SpriteComponent, "Sprite")

View File

@@ -1,33 +1,35 @@
#pragma once
#include "src/Core/Object/Object.h"
#include "Component.h"
#include "src/Core/Logger.h"
#include "src/Resource/UUID.h"
#include "src/Resource/Serializable.h"
namespace Nuake
{
struct UIComponent
{
NUAKECOMPONENT(UIComponent, "UI Component");
struct UIComponent : public Component
{
NUAKECOMPONENT(UIComponent, "UI")
UUID UIResource = UUID(0);
std::string CSharpUIController;
std::string UIFilePath;
bool IsWorldSpace;
// TODO: Z-Ordering
public:
UUID UIResource = UUID(0);
std::string CSharpUIController;
std::string UIFilePath;
bool IsWorldSpace;
// TODO: Z-Ordering
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(UIFilePath);
SERIALIZE_VAL(IsWorldSpace);
END_SERIALIZE();
}
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(UIFilePath);
END_SERIALIZE();
}
bool Deserialize(const json& j)
{
DESERIALIZE_VAL(UIFilePath);
DESERIALIZE_VAL(IsWorldSpace);
return true;
}
};
}
bool Deserialize(const json& j)
{
DESERIALIZE_VAL(UIFilePath);
return true;
}
};
}

View File

@@ -4,7 +4,6 @@
#include "src/Core/Logger.h"
#include "../Scene.h"
#include "../Components/BaseComponent.h"
#include "../Resource/Serializable.h"
#include "../Components/NameComponent.h"