From e66286ebe30afc9acc4531fc2bea29b7fb924f93 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 30 May 2022 17:03:20 -0700 Subject: Changeset: 56 Buildsystem cleanup: change to layered structure for different targets --- source/30-game/World.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 source/30-game/World.cpp (limited to 'source/30-game/World.cpp') diff --git a/source/30-game/World.cpp b/source/30-game/World.cpp new file mode 100644 index 0000000..d4a8344 --- /dev/null +++ b/source/30-game/World.cpp @@ -0,0 +1,74 @@ +#include "World.hpp" + +#include "GameObject.hpp" +#include "PodVector.hpp" + +#include + +namespace ProjectBrussel_UNITY_ID { +template +void CallGameObjectRecursive(GameObject* start, TFunction&& func) { + PodVector stack; + stack.push_back(start); + + while (!stack.empty()) { + auto obj = stack.back(); + stack.pop_back(); + + for (auto child : obj->GetChildren()) { + stack.push_back(child); + } + + func(obj); + } +} + +struct DrawCall { + GLuint vao; + GLuint vbo; +}; +} // namespace ProjectBrussel_UNITY_ID + +GameWorld::GameWorld() + : mRoot{ new GameObject(this) } { +} + +GameWorld::~GameWorld() { + if (mAwakened) { + Resleep(); + } + + delete mRoot; +} + +const GameObject& GameWorld::GetRoot() const { + return *mRoot; +}; + +void GameWorld::Awaken() { + if (mAwakened) return; + + ProjectBrussel_UNITY_ID::CallGameObjectRecursive(mRoot, [](GameObject* obj) { obj->Awaken(); }); + mAwakened = true; +} + +void GameWorld::Resleep() { + if (!mAwakened) return; + + ProjectBrussel_UNITY_ID::CallGameObjectRecursive(mRoot, [](GameObject* obj) { obj->Resleep(); }); + mAwakened = false; +} + +void GameWorld::Update() { + ProjectBrussel_UNITY_ID::CallGameObjectRecursive(mRoot, [this](GameObject* obj) { + obj->Update(); + }); +} + +GameObject& GameWorld::GetRoot() { + return *mRoot; +} + +bool GameWorld::IsAwake() const { + return mAwakened; +} -- cgit v1.2.3-70-g09d2