Brunot
Loading...
Searching...
No Matches
ChildrenHandeler.h
Go to the documentation of this file.
1
10// ____ __ __ __
11// /\__ _\/\ \ /\ \/\ \
12// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
13// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
14// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
15// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
16// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
17
18#pragma once
19#include <cassert>
20
22#include <vector>
23#include <functional>
24#include <ranges>
25#include <set>
26#include "Component.h"
27
31#define LOG_CHILDREN_HANDELER 0
32
33
40template <class C = Component>
42{
43public:
49 , children()
50 {
51 }
52
59 : GameObject("ChildrenHandler", gobj::Type::Node, gobj::Type::ChildrenHandler)
60 {
61 *this = other;
62 }
63
70 {
71 if (this == &other)
72 {
73 return *this;
74 }
76
77 for (auto& [key, pointer] : other.children)
78 {
79 using namespace std;
80 // deep copy everything in the other's vector
81 auto copy = pointer->clone();
82 // static cast from GameObject* to the correct object type
83 auto copyCast = static_cast<C*>(copy.release());
84 // We have to do weird catch and release thing because of unique pointers
85 children[copyCast->getKey()] = (std::unique_ptr<C>(copyCast));
86 }
87
88 return *this;
89 }
90
91
97 : GameObject(std::move(other))
98 , children(std::move(other.children))
99 {
100 }
101
102
109 auto operator=(ChildrenHandeler&& other) noexcept -> ChildrenHandeler&
110 {
111 if (this == &other)
112 {
113 return *this;
114 }
115 GameObject::operator =(std::move(other));
116 children = std::move(other.children);
117 return *this;
118 }
119
120
124 ~ChildrenHandeler() override = default;
125
126
127 auto showMenu() -> void override
128 {
129 for (auto& [key, child] : children)
130 {
131 child->showMenu();
132 }
133 }
134
139 {
140 static auto to_erase = [](auto& pair)
141 {
142 if (pair.second->destroyed())
143 {
144 hlg::Trace("{} was found to be destroyed, it will be removed"
145 , pair.second->getFormattedName()
146 );
147 return pair.second->destroyed();
148 }
149 return false;
150 };
151 erase_if(children, to_erase);
152 }
153
154 auto update(float dt) -> void override
155 {
157
158
159 constexpr auto pointer = 1; // the child pointer to the second object in the pair
160 using namespace std;
161 for (auto& [key, child] : children)
162 {
163 hlg::Debug(" {} is updating child {}"
165 , child->getFormattedName());
166
167 if (!child->getInternalParent())
168 {
169 hlg::Error("{} doesn't have a parent. Something is very wrong", child->getFormattedName());
170 }
171
172 if (key.getUUID() != child->getUUID())
173 {
174 hlg::Error("key uuid {} does not match {}", key.getUUID(), child->getFormattedName());
175 }
176
177 child->update(dt);
178 }
179
181 }
182
183private:
184 auto onUpdate(float dt) -> void override
185 {
186
187 }
188
189public:
190 auto endWindow() -> void override
191 {
192 }; // for debug ImGUI
193
194
195 auto render() -> void override
196 {
198 for (auto&& [key, child] : children)
199 {
200 hlg::Debug(" {} is rendering child {}", getFormattedName(), child->getFormattedName());
201 child->render();
202 }
203 }
204
205 auto onRender() -> void override
206 {
207 }
208
209 auto onEnterEngine() -> void override
210 {
211 for (auto&& child : std::views::values(children))
212 {
213 child->checkAddToSceneHook();
214 }
215 }
216
217 auto onSceneStart(const std::string& sceneName) -> void override
218 {
219 for (auto&& child : std::views::values(children))
220 {
221 child->onSceneStart(sceneName);
222 }
223 }
224
225 auto onSceneExit(const std::string& sceneName) -> void override
226 {
227 for (auto&& child : std::views::values(children))
228 {
229 child->onSceneExit(sceneName);
230 }
231 }
232
233
234 auto load(const Stream& stream) -> void override
235 {
236 // @todo implement load for ChildrenHandler
237 *this = stream.data();
238 }
239
240
241 auto save(Stream& stream) const -> void override
242 {
243 stream.data() = *this;
244 }
245
246
247 auto clone() const -> std::unique_ptr<GameObject> override
248 {
249 return std::make_unique<ChildrenHandeler>(*this);
250 }
251
252 auto setShouldUpdate(const bool _shouldUpdate) -> void override
253 {
254 GameObject::setShouldUpdate(_shouldUpdate);
255
256 for (auto&& [key, child] : children)
257 {
258 child->setShouldUpdate(_shouldUpdate);
259 }
260 }
261
262 auto setShouldRender(const bool _shouldRender) -> void override
263 {
264 GameObject::setShouldRender(_shouldRender);
265
266 for (auto&& [key, child] : children)
267 {
268 child->setShouldRender(_shouldRender);
269 }
270 }
271
272 auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void
273 {
274 GameObject::setShouldReceiveMessages(_shouldReceiveMessages);
275
276 for (auto&& [key, child] : children)
277 {
278 child->setShouldReceiveMessages(_shouldReceiveMessages);
279 }
280 }
281
282
283 auto add(std::unique_ptr<C> newChild) -> void
284 {
285 assert(newChild && "ChildHandler add was passed null unique_ptr");
286 children[newChild->getKey()] = std::move(newChild);
287 }
288
293 auto for_each(std::function<void(C&)> func) -> void
294 {
295 for (auto& [key, child] : children)
296 {
297 func(*child);
298 }
299 }
300
305 auto for_each(std::function<void(const C&)> func) const -> void
306 {
307 for (auto& [key, child] : children)
308 {
309 func(*child);
310 }
311 }
312
317 auto for_each(std::function<void(GameObject&)> func) -> void override
318 {
319 for (auto& [key, child] : children)
320 {
321 func(*child);
322 }
323 }
324
329 auto for_each(std::function<void(const GameObject&)> func) const -> void override
330 {
331 for (auto& [key, child] : children)
332 {
333 func(*child);
334 }
335 }
336
342 auto find(std::function<bool(C&)> predicate) -> C*
343 {
344 using namespace std;
345 constexpr auto obj_ptr = 1;
346 for (auto& child : children)
347 {
348 if (predicate(*get<obj_ptr>(child)))
349 {
350 // Get second part of pair, dereference that unique pointer,
351 // then get the address of the object it's pointing at
352 return &*get<obj_ptr>(child);
353 }
354
355 }
356 return nullptr;
357 }
358
359 auto count() -> size_t
360 {
361 return children.size();
362 }
363
364 auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>> override
365 {
366 return getChildrenRange();
367 }
368
373 auto getChildrenRange() const -> std::shared_ptr<std::vector<GameObject*>>
374 {
376
377 auto result = std::make_shared<std::vector<GameObject*>>();
378 // copies every child from children to the vector
379 for (auto& [key, ptr] : children)
380 {
381 result->push_back(ptr.get());
382 }
383 return result;
384 }
385
391 auto pop(Key key) -> std::unique_ptr<C>
392 {
393 auto itrToPtr = children.find(key);
394 if (itrToPtr == children.end())
395 {
396 assert(false && "Tried to remove an object that doesn't exist");
397 hlg::Error("Tried to remove an object that doesn't exist");
398 }
399 auto& [mapKey, ptr] = *itrToPtr;
400 std::unique_ptr<C> pointer = std::move(ptr);
401 children.erase(itrToPtr);
402 return pointer;
403 }
404
405
410 auto setParents(GameObject& parent) -> void
411 {
412 using namespace std;
413 for (auto& child : children)
414 {
415 if (!(get<1>(child)->parentTo(&parent))) // second object at pos 1 is unique ptr to gameobject
416 {
417 assert(0 && "Setting parents in children handeler");
418 }
419 }
420 }
421
422 // these are the regular overload functions
423 template <typename C>
424 friend auto to_json(json& j, const ChildrenHandeler<C>& ch) -> void;
425 template <typename C>
426 friend auto from_json(const json& j, ChildrenHandeler<C>& ch) -> void;
427
428 // this is too access component and node ChildrenHandler from_json functions
429 friend auto from_json(const json& j, ChildrenHandeler<C>& ch) -> void;
430
431private:
435 std::map<Key, std::unique_ptr<C>> children;
436
437
438};
439
440
441template <typename C>
442auto to_json(json& j, const ChildrenHandeler<C>& ch) -> void
443{
444 j["GameObject"] = ch.getGameObjectJson();
445 for (auto& child : ch.children)
446 {
447 j["children"].push_back(*(get<1>(child)));
448 }
449}
450
455template <typename C>
456auto from_json(const json& j, ChildrenHandeler<C>& ch) -> void
457{
458 /*for (const auto& child : j.at("children"))
459 {
460 C c = child.get<C>();
461 ch.add(std::make_unique<C>(c));
462 }*/
463 assert(0 && "Children Handler with non-Node and non-Component");
464 ch.setGameObjectJson(j.at("GameObject"));
465}
static FMOD_RESULT result
Definition AudioObject.cpp:27
sys::Json Stream
Definition AudioObject.h:20
@ C
Definition Card.h:26
auto from_json(const json &j, ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:456
auto to_json(json &j, const ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:442
The base class for components, holding all of their shared All components should inherit from this.
the base class for the engine, most things inherit from this.
nlohmann::json json
Definition Json.cpp:19
auto render() -> void override
called every frame after update has been called for every object.
Definition ChildrenHandeler.h:195
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition ChildrenHandeler.h:209
auto operator=(ChildrenHandeler &&other) noexcept -> ChildrenHandeler &
move assignment operator
Definition ChildrenHandeler.h:109
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition ChildrenHandeler.h:190
std::map< Key, std::unique_ptr< C > > children
A map that olds unique pointers to child objects, and a key that organizes them.
Definition ChildrenHandeler.h:435
auto getChildrenRange() const -> std::shared_ptr< std::vector< GameObject * > >
A function designed for Paths to use, for getting a list of all children, in a manageable format.
Definition ChildrenHandeler.h:373
~ChildrenHandeler() override=default
default dtor.
auto onUpdate(float dt) -> void override
called once every frame.
Definition ChildrenHandeler.h:184
auto add(std::unique_ptr< C > newChild) -> void
Definition ChildrenHandeler.h:283
auto setShouldRender(const bool _shouldRender) -> void override
set whether the GameObject should render every frame, and whether all of it's children should render ...
Definition ChildrenHandeler.h:262
auto showMenu() -> void override
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition ChildrenHandeler.h:127
auto deleteDestroyedChildren() -> void
check direct children, and see if they need to be destroyed.
Definition ChildrenHandeler.h:138
auto find(std::function< bool(C &)> predicate) -> C *
Returns pointer to child object.
Definition ChildrenHandeler.h:342
auto for_each(std::function< void(const C &)> func) const -> void
applies a function to every child.
Definition ChildrenHandeler.h:305
ChildrenHandeler(ChildrenHandeler &&other) noexcept
Move Constructor.
Definition ChildrenHandeler.h:96
auto setParents(GameObject &parent) -> void
Set's all managed objects parent to one passed in.
Definition ChildrenHandeler.h:410
friend auto from_json(const json &j, ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:456
auto for_each(std::function< void(C &)> func) -> void
applies a function to every child.
Definition ChildrenHandeler.h:293
friend auto to_json(json &j, const ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:442
ChildrenHandeler(const ChildrenHandeler &other)
deep copy of ChildrenHandeler
Definition ChildrenHandeler.h:58
auto onSceneStart(const std::string &sceneName) -> void override
Hook that is called before the first frame a scene is run, but after the entire scene is loaded into ...
Definition ChildrenHandeler.h:217
auto count() -> size_t
Definition ChildrenHandeler.h:359
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition ChildrenHandeler.h:234
auto onRender() -> void override
called every frame after update has been called for every object.
Definition ChildrenHandeler.h:205
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition ChildrenHandeler.h:241
auto onSceneExit(const std::string &sceneName) -> void override
Hook that is called right before a scene is unloaded To receive the hook, simply override the functio...
Definition ChildrenHandeler.h:225
auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void
set whether the GameObject should Receive Messags, and whether all of it's children should receive me...
Definition ChildrenHandeler.h:272
auto for_each(std::function< void(GameObject &)> func) -> void override
applies a function to every child.
Definition ChildrenHandeler.h:317
auto getChildren() const -> std::shared_ptr< std::vector< GameObject * > > override
gets a vector of a GameObjects public (i.e.
Definition ChildrenHandeler.h:364
auto setShouldUpdate(const bool _shouldUpdate) -> void override
set whether the GameObject should update every frame, and whether all of it's children should update ...
Definition ChildrenHandeler.h:252
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition ChildrenHandeler.h:247
auto for_each(std::function< void(const GameObject &)> func) const -> void override
applies a function to every child.
Definition ChildrenHandeler.h:329
auto operator=(const ChildrenHandeler &other) -> ChildrenHandeler &
Copy assignment operator.
Definition ChildrenHandeler.h:69
ChildrenHandeler()
default constructor for ChildrenHandeler
Definition ChildrenHandeler.h:47
auto pop(Key key) -> std::unique_ptr< C >
Removes a Child from the list, transferring ownership to the Caller.
Definition ChildrenHandeler.h:391
auto update(float dt) -> void override
called once every frame.
Definition ChildrenHandeler.h:154
a Key class to be used as a key when holding GameObjects (or unique pointers to game objects) in <key...
Definition GameObject.h:415
virtual auto setShouldReceiveMessages(bool _shouldReceiveMessages) -> void
set whether the GameObject should Receive Messags, and whether all of it's children should receive me...
Definition GameObject.cpp:302
virtual auto update(float dt) -> void
called once every frame.
Definition GameObject.cpp:125
auto getFormattedName() const -> const std::string &
combines a GameObjects Type, Nickname, and UUID into one string, for use with logging.
Definition GameObject.cpp:240
friend Node
Definition GameObject.h:86
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:23
virtual auto parentTo(GameObject *newParent) -> bool
Sets the GameObject as a child of another GameObject.
Definition GameObject.cpp:184
virtual auto render() -> void
called every frame after update has been called for every object.
Definition GameObject.cpp:139
auto operator=(const GameObject &other) -> GameObject &
copy assignment operator
Definition GameObject.cpp:56
virtual auto setShouldUpdate(bool _shouldUpdate) -> void
set whether the GameObject should update every frame, and whether all of it's children should update ...
Definition GameObject.cpp:290
virtual auto setShouldRender(bool _shouldRender) -> void
set whether the GameObject should render every frame, and whether all of it's children should render ...
Definition GameObject.cpp:296
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:309
@ Node
Definition GameObject.h:50
@ ChildrenHandler
Definition GameObject.h:51
auto Error(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:110
auto Debug(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:62
auto Trace(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:46