Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Scene.h
Go to the documentation of this file.
1/**
2 * @file Scene.h
3 * @ingroup mnd_scene
4 * @brief Owns the game object tree and provides per-frame update + light collection.
5 *
6 * The Scene is the top-level container for all GameObjects. It owns them
7 * via unique_ptr — GameObjects must be created through Scene::CreateObject
8 * so that the scene always holds the authoritative lifetime.
9 *
10 * ## Relationship with Engine
11 * The Engine holds one active Scene (Engine::SetScene / Engine::GetScene).
12 * Each frame, Engine::Run() calls Scene-level queries to gather CameraData
13 * and LightData before handing them to RenderQueue::Draw().
14 *
15 * ## Creating objects
16 * @code
17 * Scene *scene = new Scene();
18 * engine.SetScene(scene);
19 *
20 * auto *player = scene->CreateObject("Player");
21 * player->AddComponent(new PlayerControllerComponent());
22 *
23 * auto *camera = scene->CreateObject("Camera", player); // child of player
24 * camera->AddComponent(new CameraComponent());
25 * scene->SetMainCamera(camera);
26 * @endcode
27 *
28 * @see GameObject, Engine::SetScene, CameraData, LightData
29 */
30
31#pragma once
32
33#include <memory>
34#include <string>
35#include <type_traits>
36#include <utility>
37#include <vector>
38
39#include "Common.h"
40#include "Types.h"
41#include "nlohmann/json.hpp"
42#include "scene/GameObject.h"
43
44namespace mnd
45{
46
47/**
48 * @brief Container for the entire game object graph.
49 *
50 * Owns root-level GameObjects and provides the three services that
51 * Engine::Run() needs each frame:
52 * 1. Update() — propagate deltaTime to every active object and component.
53 * 2. GetMainCamera() — return the designated camera object so the engine can
54 * extract CameraData.
55 * 3. CollectLight() — walk the hierarchy and gather LightData from every
56 * LightComponent.
57 */
58class Scene
59{
60public:
61 /**
62 * @brief Update all root objects (which recursively update their children).
63 * @param deltaTime Seconds since the previous frame.
64 */
65 static void RegisterTypes();
66 void Update(f32 deltaTime);
67
68 /// Destroy all objects and reset the scene to an empty state.
69 void Clear();
70
71 /**
72 * @brief Create a plain GameObject owned by this scene.
73 * @param name Display name for the new object.
74 * @param parent Optional parent; makes the object a child rather than a root.
75 * @return Non-owning pointer to the new object.
76 */
77 GameObject *CreateObject(const std::string &name, GameObject *parent = nullptr);
78 GameObject *CreateObject(const std::string &type, const std::string &name, GameObject *parent = nullptr);
79
80 /**
81 * @brief Create a typed GameObject subclass owned by this scene.
82 *
83 * Use this overload when you need to store extra data directly on the
84 * GameObject instead of in a Component.
85 *
86 * @tparam T A class derived from GameObject.
87 * @param name Display name.
88 * @param parent Optional parent node.
89 * @return Typed non-owning pointer to the new object.
90 */
91 template<typename T, typename = typename std::enable_if_t<std::is_base_of_v<GameObject, T>>>
92 T *CreateObject(const str &name, GameObject *parent = nullptr)
93 {
94 auto obj = new T();
95 obj->SetName(name);
96 obj->m_scene = this;
97 if (m_isUpdating)
98 {
99 m_objectsToAdd.push_back({obj, parent});
100 } else
101 {
102 SetParent(obj, parent);
103 }
104
105 return obj;
106 }
107
108 /**
109 * @brief Move a GameObject in the hierarchy.
110 * @param obj The object to re-parent.
111 * @param parent New parent, or nullptr to promote to root.
112 * @return true if the operation succeeded.
113 */
114 bool SetParent(GameObject *obj, GameObject *parent);
115
116 /**
117 * @brief Designate the camera object whose CameraComponent drives the view.
118 * @param camera A GameObject that must have a CameraComponent attached.
119 */
120 void SetMainCamera(GameObject *camera);
121
122 /// Returns the current main camera object (set via SetMainCamera).
124
125 /**
126 * @brief Walk the entire object tree and collect all LightData.
127 *
128 * Called by Engine::Run() once per frame. Each GameObject that has a
129 * LightComponent contributes one LightData entry.
130 *
131 * @return Vector of LightData for all active lights in the scene.
132 */
133 std::vector<LightData> CollectLight();
134 static std::shared_ptr<Scene> Load(const str &path);
135
136 /// Editor access: iterate root GameObjects (non-owning).
137 const std::vector<std::unique_ptr<GameObject>> &GetRootObjects() const { return m_objects; }
138
139private:
140 /// Recursive DFS helper used by CollectLight().
141 void CollectLightsRecursive(GameObject *obj, std::vector<LightData> &out);
142 void LoadObject(const nlohmann::json &jsonObject, GameObject *parent = nullptr);
143
144private:
145 std::vector<std::unique_ptr<GameObject>> m_objects; ///< All root-level objects (own the tree).
146 std::vector<std::pair<GameObject *, GameObject *>> m_objectsToAdd;
147 GameObject *m_mainCamera = nullptr; ///< Non-owning pointer to the active camera object.
148 bool m_isUpdating;
149};
150
151} // namespace mnd
Engine-wide primitive type aliases and GLM math imports.
Node in the scene graph: a named transform that owns components and children.
Definition GameObject.h:91
Container for the entire game object graph.
Definition Scene.h:59
void Update(f32 deltaTime)
Definition Scene.cpp:44
static std::shared_ptr< Scene > Load(const str &path)
Definition Scene.cpp:266
GameObject * CreateObject(const std::string &name, GameObject *parent=nullptr)
Create a plain GameObject owned by this scene.
Definition Scene.cpp:132
static void RegisterTypes()
Update all root objects (which recursively update their children).
Definition Scene.cpp:29
std::vector< LightData > CollectLight()
Walk the entire object tree and collect all LightData.
Definition Scene.cpp:82
void SetMainCamera(GameObject *camera)
Designate the camera object whose CameraComponent drives the view.
Definition Scene.cpp:72
GameObject * GetMainCamera()
Returns the current main camera object (set via SetMainCamera).
Definition Scene.cpp:77
const std::vector< std::unique_ptr< GameObject > > & GetRootObjects() const
Editor access: iterate root GameObjects (non-owning).
Definition Scene.h:137
T * CreateObject(const str &name, GameObject *parent=nullptr)
Create a typed GameObject subclass owned by this scene.
Definition Scene.h:92
bool SetParent(GameObject *obj, GameObject *parent)
Move a GameObject in the hierarchy.
Definition Scene.cpp:339
void Clear()
Destroy all objects and reset the scene to an empty state.
Definition Scene.cpp:108
std::string str
Convenience alias for std::string.
Definition Types.h:45
float f32
32-bit IEEE float — the standard GL scalar type.
Definition Types.h:40