feat: implement contact listeners
This commit is contained in:
@@ -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>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user