52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
#include <components/Checkpoint.h>
|
|
|
|
#include <destrum/ObjectModel/GameObject.h>
|
|
#include <destrum/Serialization/GlmJson.h>
|
|
|
|
#include <components/PlayerRespawn.h>
|
|
|
|
#include <imgui.h>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <spdlog/spdlog.h>
|
|
|
|
Checkpoint::Checkpoint(GameObject& parent)
|
|
: Component(parent, "Checkpoint")
|
|
{
|
|
}
|
|
|
|
glm::vec3 Checkpoint::GetSpawnPoint() const
|
|
{
|
|
return GetTransform().GetWorldPosition() + m_SpawnOffset;
|
|
}
|
|
|
|
void Checkpoint::OnTriggerEnter(GameObject* other)
|
|
{
|
|
if (other == nullptr) {
|
|
return;
|
|
}
|
|
|
|
auto* respawn = other->GetComponent<PlayerRespawn>();
|
|
const glm::vec3 spawnPoint = GetSpawnPoint();
|
|
if (respawn == nullptr || respawn->GetRespawnPoint() == spawnPoint) {
|
|
return;
|
|
}
|
|
|
|
respawn->SetRespawnPoint(spawnPoint);
|
|
spdlog::info("{} reached checkpoint {}", other->GetName(), GetGameObject()->GetName());
|
|
}
|
|
|
|
nlohmann::json Checkpoint::Serialize() const
|
|
{
|
|
return {{"spawnOffset", m_SpawnOffset}};
|
|
}
|
|
|
|
void Checkpoint::Deserialize(const nlohmann::json& data)
|
|
{
|
|
m_SpawnOffset = data.value("spawnOffset", m_SpawnOffset);
|
|
}
|
|
|
|
void Checkpoint::ImGuiInspector()
|
|
{
|
|
ImGui::DragFloat3("Spawn offset", glm::value_ptr(m_SpawnOffset), 0.05f);
|
|
}
|