From 17a62d95e0bcefdd47c467fa9a529aae7ad65064 Mon Sep 17 00:00:00 2001 From: wincent Date: Mon, 20 Nov 2023 19:52:27 +0100 Subject: [PATCH] Resolved some comments --- dGame/dCinema/Play.cpp | 26 ++++++++++++-------------- dGame/dCinema/Play.h | 2 +- dGame/dCinema/Prefab.cpp | 34 ++++++++++++++++++++++++++-------- dGame/dCinema/Prefab.h | 13 ++----------- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/dGame/dCinema/Play.cpp b/dGame/dCinema/Play.cpp index b810745c..389fa7d6 100644 --- a/dGame/dCinema/Play.cpp +++ b/dGame/dCinema/Play.cpp @@ -6,7 +6,7 @@ using namespace Cinema; -void Cinema::Play::Conclude() { +void Play::Conclude() { auto* player = Game::entityManager->GetEntity(this->player); if (player == nullptr) { @@ -16,7 +16,7 @@ void Cinema::Play::Conclude() { scene->Conclude(player); } -void Cinema::Play::SetupCheckForAudience() { +void Play::SetupCheckForAudience() { if (m_CheckForAudience) { return; } @@ -26,7 +26,7 @@ void Cinema::Play::SetupCheckForAudience() { CheckForAudience(); } -void Cinema::Play::CheckForAudience() { +void Play::CheckForAudience() { auto* player = Game::entityManager->GetEntity(this->player); if (player == nullptr) { @@ -51,13 +51,13 @@ void Cinema::Play::CheckForAudience() { return; } - // Still don't care + // As the scene isn't associated with a specifc objects, we'll use the zone control entity to setup a callback. Game::entityManager->GetZoneControlEntity()->AddCallbackTimer(1.0f, [this]() { CheckForAudience(); }); } -void Cinema::Play::CleanUp() { +void Play::CleanUp() { LOG("Cleaning up play with %d entities", entities.size()); for (const auto& entity : entities) { @@ -65,25 +65,23 @@ void Cinema::Play::CleanUp() { } } -void Cinema::Play::SetupBarrier(const std::string& barrier, std::function callback) { +void Play::SetupBarrier(const std::string& barrier, const std::function& callback) { // Add the callback to the barrier - if (m_Barriers.find(barrier) == m_Barriers.end()) { - m_Barriers[barrier] = std::vector>(); - } - m_Barriers[barrier].push_back(callback); } -void Cinema::Play::SignalBarrier(const std::string& barrier) { - if (m_Barriers.find(barrier) == m_Barriers.end()) { +void Play::SignalBarrier(const std::string& barrier) { + const auto& it = m_Barriers.find(barrier); + + if (it == m_Barriers.end()) { return; } - for (const auto& callback : m_Barriers[barrier]) { + for (const auto& callback : it->second) { callback(); } - m_Barriers.erase(barrier); + m_Barriers.erase(it); } diff --git a/dGame/dCinema/Play.h b/dGame/dCinema/Play.h index 37b20f2c..95450b9d 100644 --- a/dGame/dCinema/Play.h +++ b/dGame/dCinema/Play.h @@ -60,7 +60,7 @@ public: * @param barrier The name of the barrier. * @param callback The callback to call when the barrier is signaled. */ - void SetupBarrier(const std::string& barrier, std::function callback); + void SetupBarrier(const std::string& barrier, const std::function& callback); /** * @brief Signal a barrier. diff --git a/dGame/dCinema/Prefab.cpp b/dGame/dCinema/Prefab.cpp index c22bf7a1..151eb37b 100644 --- a/dGame/dCinema/Prefab.cpp +++ b/dGame/dCinema/Prefab.cpp @@ -9,13 +9,31 @@ using namespace Cinema; -std::unordered_map Prefab::m_Prefabs; -std::unordered_map Prefab::m_Instances; +struct PrefabInstance +{ + std::vector m_Entities; +}; + + +namespace { + + + std::unordered_map m_Prefabs; + std::unordered_map m_Instances; + + float m_AngleToRadians = 0.0174532925f; +} size_t Prefab::AddObject(LOT lot, NiPoint3 position, NiQuaternion rotation, float scale) { const auto id = ObjectIDManager::GenerateRandomObjectID(); - m_Pieces.emplace(id, Prefab::Piece { lot, position, rotation, scale, {} }); + m_Pieces.emplace(id, Prefab::Piece { + lot, + position, + rotation, + scale, + std::vector() + }); return id; } @@ -55,9 +73,9 @@ const Prefab& Prefab::LoadFromFile(std::string file) { // Check if the qx attribute exists, if so the rotation is a quaternion, otherwise it's a vector if (!element->Attribute("qx")) { rotation = NiQuaternion::FromEulerAngles( { - element->FloatAttribute("rx") * 0.0174532925f, - element->FloatAttribute("ry") * 0.0174532925f, - element->FloatAttribute("rz") * 0.0174532925f + element->FloatAttribute("rx") * m_AngleToRadians, + element->FloatAttribute("ry") * m_AngleToRadians, + element->FloatAttribute("rz") * m_AngleToRadians } ); } else { @@ -141,7 +159,7 @@ size_t Prefab::Instantiate(NiPoint3 position, float scale) const { // Generate random name std::string effectName = "Effect_"; for (int i = 0; i < 10; ++i) { - effectName += std::to_string(rand() % 10); + effectName += std::to_string(GeneralUtils::GenerateRandomNumber(0, 10)); } renderComponent->PlayEffect(effect, u"create", effectName); @@ -152,7 +170,7 @@ size_t Prefab::Instantiate(NiPoint3 position, float scale) const { Game::entityManager->ConstructEntity(entity); } - m_Instances.emplace(id, Prefab::Instance { entities }); + m_Instances.emplace(id, PrefabInstance { entities }); return id; } diff --git a/dGame/dCinema/Prefab.h b/dGame/dCinema/Prefab.h index 1e0dc659..602e1a0b 100644 --- a/dGame/dCinema/Prefab.h +++ b/dGame/dCinema/Prefab.h @@ -18,6 +18,8 @@ class Prefab public: Prefab() = default; + ~Prefab() = default; + /** * @brief Adds an object to the prefab. * @@ -87,8 +89,6 @@ public: static void DestroyInstance(size_t instanceID); - ~Prefab() = default; - private: struct Piece { @@ -99,16 +99,7 @@ private: std::vector m_Effects; }; - struct Instance - { - std::vector m_Entities; - }; - std::unordered_map m_Pieces; - - static std::unordered_map m_Prefabs; - - static std::unordered_map m_Instances; }; } \ No newline at end of file