feat: allow game registering of components

This commit is contained in:
2026-08-14 02:41:59 +02:00
parent 405ef0baeb
commit 7f65a151b5
8 changed files with 111 additions and 1 deletions
@@ -22,6 +22,6 @@
X(Rigidbody) \
X(BoxCollider) \
X(SphereCollider) \
X(CapsuleCollider)
X(CapsuleCollider) \
#endif // DESTRUM_ENGINECOMPONENTLIST_H
+3
View File
@@ -6,6 +6,9 @@ set(GAME_SRC
src/main.cpp
src/Lightkeeper.cpp
src/components/GameComponentRegistry.cpp
src/components/PrintComponent.cpp
)
add_executable(lightkeeper ${GAME_SRC})
@@ -0,0 +1,9 @@
#ifndef LIGHTKEEPER_GAMECOMPONENTLIST_H
#define LIGHTKEEPER_GAMECOMPONENTLIST_H
#include <components/PrintComponent.h>
#define LIGHTKEEPER_GAME_COMPONENTS(X) \
X(PrintComponent)
#endif // LIGHTKEEPER_GAMECOMPONENTLIST_H
@@ -0,0 +1,6 @@
#ifndef LIGHTKEEPER_GAMECOMPONENTREGISTRY_H
#define LIGHTKEEPER_GAMECOMPONENTREGISTRY_H
void RegisterGameComponents();
#endif // LIGHTKEEPER_GAMECOMPONENTREGISTRY_H
@@ -0,0 +1,28 @@
#ifndef DESTRUM_PRINTCOMPONENT_H
#define DESTRUM_PRINTCOMPONENT_H
#include "destrum/ObjectModel/Component.h"
class PrintComponent: public Component {
public:
PrintComponent(GameObject& parent);
std::string GetTypeName() const override { return "PrintComponent"; }
void Start() override;
void Update(float dt) override;
void ImGuiInspector() override;
nlohmann::json Serialize() const override;
void Deserialize(const nlohmann::json&) override;
void SetMessage(std::string_view message)
{
m_message = message;
}
private:
std::string m_message{};
};
#endif //DESTRUM_PRINTCOMPONENT_H
+10
View File
@@ -24,6 +24,8 @@
#include <string_view>
#include "imgui.h"
#include "components/GameComponentRegistry.h"
#include "components/PrintComponent.h"
#include "destrum/Components/Physics/SphereCollider.h"
#include "destrum/Util/ModelDocUtils.h"
@@ -60,6 +62,8 @@ LightKeeper::~LightKeeper()
void LightKeeper::customInit()
{
RegisterGameComponents();
resources.init(gfxDevice);
renderer.init(gfxDevice, resources, m_params.renderSize);
@@ -426,6 +430,12 @@ void LightKeeper::customInit()
sphereMaterial = resources.materials().addSimpleColorMaterial({0.5f, 0.3f, 0.8f}, "Blue");
auto obj = scene.CreateGameObject("GameObject");
auto printComp = obj->AddComponent<PrintComponent>();
printComp->SetMessage("Testing");
}
void LightKeeper::customUpdate(float dt)
@@ -0,0 +1,20 @@
#include <components/GameComponentRegistry.h>
#include <destrum/Serialization/ComponentFactory.h>
#include <components/GameComponentList.h>
void RegisterGameComponents()
{
static bool registered = false;
if (registered) {
return;
}
registered = true;
#define LIGHTKEEPER_REGISTER_COMPONENT(ComponentType) \
ComponentFactory::Register<ComponentType>(#ComponentType);
LIGHTKEEPER_GAME_COMPONENTS(LIGHTKEEPER_REGISTER_COMPONENT)
#undef LIGHTKEEPER_REGISTER_COMPONENT
}
@@ -0,0 +1,34 @@
#include <components/PrintComponent.h>
#include "spdlog/spdlog.h"
PrintComponent::PrintComponent(GameObject& parent) : Component(parent, "PrintComponent")
{
}
void PrintComponent::Start()
{
}
void PrintComponent::Update(float dt)
{
spdlog::debug(m_message);
}
void PrintComponent::ImGuiInspector()
{
Component::ImGuiInspector();
}
nlohmann::json PrintComponent::Serialize() const
{
return {"message", m_message};
}
void PrintComponent::Deserialize(const nlohmann::json& basic_jsons)
{
if (basic_jsons.contains("message"))
{
m_message = basic_jsons.at("message").get<std::string>();
}
}