feat: implement contact listeners

This commit is contained in:
2026-09-02 21:42:49 +02:00
parent 287e5c6885
commit d515a36403
20 changed files with 595 additions and 95 deletions
+1
View File
@@ -9,6 +9,7 @@ set(GAME_SRC
src/components/GameComponentRegistry.cpp
src/components/PrintComponent.cpp
src/components/TriggerLoggerComponent.cpp
)
add_executable(lightkeeper ${GAME_SRC})
@@ -2,8 +2,10 @@
#define LIGHTKEEPER_GAMECOMPONENTLIST_H
#include <components/PrintComponent.h>
#include <components/TriggerLoggerComponent.h>
#define LIGHTKEEPER_GAME_COMPONENTS(X) \
X(PrintComponent)
X(PrintComponent) \
X(TriggerLoggerComponent)
#endif // LIGHTKEEPER_GAMECOMPONENTLIST_H
@@ -0,0 +1,22 @@
#ifndef DESTRUM_TRIGGERLOGGERCOMPONENT_H
#define DESTRUM_TRIGGERLOGGERCOMPONENT_H
#include "destrum/ObjectModel/Component.h"
class TriggerLoggerComponent: public Component {
public:
TriggerLoggerComponent(GameObject& parent);
std::string GetTypeName() const override { return "TriggerLoggerComponent"; }
void OnTriggerEnter(GameObject* other) override;
void OnTriggerExit(GameObject* other) override;
nlohmann::json Serialize() const override;
void Deserialize(const nlohmann::json&) override;
int triggerEnterCount{0};
int triggerExitCount{0};
std::string lastOtherName;
};
#endif //DESTRUM_TRIGGERLOGGERCOMPONENT_H
+10 -3
View File
@@ -18,6 +18,7 @@
#include "destrum/ObjectModel/GameObject.h"
#include "destrum/Util/ModelDoc.h"
#include "destrum/Components/Animator.h"
#include <components/TriggerLoggerComponent.h>
#include <filesystem>
@@ -433,9 +434,15 @@ 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");
// Trigger zone: a static box with a TriggerLoggerComponent.
// Spheres spawned via the "Spawn ball" button fall through it.
auto triggerObj = scene.CreateGameObject("TriggerBox");
auto triggerBox = triggerObj->AddComponent<BoxCollider>(glm::vec3{3.0f, 3.0f, 3.0f});
triggerBox->SetTrigger(true);
auto triggerRb = triggerObj->AddComponent<Rigidbody>();
triggerRb->SetType(RigidbodyType::Static);
triggerObj->AddComponent<TriggerLoggerComponent>();
triggerObj->GetTransform().SetWorldPosition({0.0f, 0.0f, 0.0f});
}
@@ -0,0 +1,45 @@
#include <components/TriggerLoggerComponent.h>
#include "spdlog/spdlog.h"
#include <destrum/ObjectModel/GameObject.h>
TriggerLoggerComponent::TriggerLoggerComponent(GameObject& parent)
: Component(parent, "TriggerLoggerComponent") {}
void TriggerLoggerComponent::OnTriggerEnter(GameObject* other) {
++triggerEnterCount;
lastOtherName = other ? other->GetName() : "null";
spdlog::info(
"TriggerLogger: ENTER {} -> {} (enters: {}, exits: {})",
GetGameObject()->GetName(),
lastOtherName,
triggerEnterCount,
triggerExitCount);
}
void TriggerLoggerComponent::OnTriggerExit(GameObject* other) {
++triggerExitCount;
lastOtherName = other ? other->GetName() : "null";
spdlog::info(
"TriggerLogger: EXIT {} -> {} (enters: {}, exits: {})",
GetGameObject()->GetName(),
lastOtherName,
triggerEnterCount,
triggerExitCount);
}
nlohmann::json TriggerLoggerComponent::Serialize() const {
return {
{"triggerEnterCount", triggerEnterCount},
{"triggerExitCount", triggerExitCount}
};
}
void TriggerLoggerComponent::Deserialize(const nlohmann::json& data) {
if (data.contains("triggerEnterCount")) {
triggerEnterCount = data.at("triggerEnterCount").get<int>();
}
if (data.contains("triggerExitCount")) {
triggerExitCount = data.at("triggerExitCount").get<int>();
}
}