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
135 auto update(float dt) -> void override
136 {
138
139
140 constexpr auto pointer = 1; // the child pointer to the second object in the pair
141 using namespace std;
142 for (auto& [key, child] : children)
143 {
144 hlg::Debug(" {} is updating child {}"
146 , child->getFormattedName());
147
148 if (!child->getInternalParent())
149 {
150 hlg::Error("{} doesn't have a parent. Something is very wrong", child->getFormattedName());
151 }
152
153 if (key.getUUID() != child->getUUID())
154 {
155 hlg::Error("key uuid {} does not match {}", key.getUUID(), child->getFormattedName());
156 }
157
158 child->update(dt);
159 }
160
161 static auto to_erase = [](auto& pair)
162 {
163 if (pair.second->destroyed())
164 {
165 hlg::Trace("{} was found to be destroyed, it will be removed"
166 , pair.second->getFormattedName()
167 );
168 return pair.second->destroyed();
169 }
170 return false;
171 };
172 erase_if(children, to_erase);
173 }
174
175private:
176 auto onUpdate(float dt) -> void override
177 {
178
179 }
180
181public:
182 auto endWindow() -> void override
183 {
184 }; // for debug ImGUI
185
186
187 auto render() -> void override
188 {
190 for (auto&& [key, child] : children)
191 {
192 hlg::Debug(" {} is rendering child {}", getFormattedName(), child->getFormattedName());
193 child->render();
194 }
195 }
196
197 auto onRender() -> void override
198 {
199 }
200
201 auto onEnterEngine() -> void override
202 {
203 for (auto&& child : std::views::values(children))
204 {
205 child->checkAddToSceneHook();
206 }
207 }
208
209
210 auto load(Stream& stream) -> void override
211 {
212 // @todo implement load for ChildrenHandler
213 *this = stream.data();
214 }
215
216
217 auto save(Stream& stream) const -> void override
218 {
219 stream.data() = *this;
220 }
221
222
223 auto clone() const -> std::unique_ptr<GameObject> override
224 {
225 return std::make_unique<ChildrenHandeler>(*this);
226 }
227
228 auto setShouldUpdate(const bool _shouldUpdate) -> void override
229 {
230 GameObject::setShouldUpdate(_shouldUpdate);
231
232 for (auto&& [key, child] : children)
233 {
234 child->setShouldUpdate(_shouldUpdate);
235 }
236 }
237
238 auto setShouldRender(const bool _shouldRender) -> void override
239 {
240 GameObject::setShouldRender(_shouldRender);
241
242 for (auto&& [key, child] : children)
243 {
244 child->setShouldRender(_shouldRender);
245 }
246 }
247
248 auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void override
249 {
250 GameObject::setShouldReceiveMessages(_shouldReceiveMessages);
251
252 for (auto&& [key, child] : children)
253 {
254 child->setShouldReceiveMessages(_shouldReceiveMessages);
255 }
256 }
257
258
259 auto add(std::unique_ptr<C> newChild) -> void
260 {
261 assert(newChild && "ChildHandler add was passed null unique_ptr");
262 children[newChild->getKey()] = std::move(newChild);
263 }
264
269 auto for_each(std::function<void(C&)> func) -> void
270 {
271 for (auto& [key, child] : children)
272 {
273 func(*child);
274 }
275 }
276
281 auto for_each(std::function<void(const C&)> func) const -> void
282 {
283 for (auto& [key, child] : children)
284 {
285 func(*child);
286 }
287 }
288
293 auto for_each(std::function<void(GameObject&)> func) -> void
294 {
295 for (auto& [key, child] : children)
296 {
297 func(*child);
298 }
299 }
300
305 auto for_each(std::function<void(const GameObject&)> func) const -> void override
306 {
307 for (auto& [key, child] : children)
308 {
309 func(*child);
310 }
311 }
312
318 auto find(std::function<bool(C&)> predicate) -> C*
319 {
320 using namespace std;
321 constexpr auto obj_ptr = 1;
322 for (auto& child : children)
323 {
324 if (predicate(*get<obj_ptr>(child)))
325 {
326 // Get second part of pair, dereference that unique pointer,
327 // then get the address of the object it's pointing at
328 return &*get<obj_ptr>(child);
329 }
330
331 }
332 return nullptr;
333 }
334
335 auto count() -> size_t
336 {
337 return children.size();
338 }
339
340 auto getChildren() const -> std::shared_ptr<std::vector<GameObject*>> override
341 {
342 return getChildrenRange();
343 }
344
349 auto getChildrenRange() const -> std::shared_ptr<std::vector<GameObject*>>
350 {
352
353 auto result = std::make_shared<std::vector<GameObject*>>();
354 // copies every child from children to the vector
355 for (auto& [key, ptr] : children)
356 {
357 result->push_back(ptr.get());
358 }
359 return result;
360 }
361
367 auto pop(Key key) -> std::unique_ptr<C>
368 {
369 auto itrToPtr = children.find(key);
370 if (itrToPtr == children.end())
371 {
372 assert(false && "Tried to remove an object that doesn't exist");
373 hlg::Error("Tried to remove an object that doesn't exist");
374 }
375 auto& [mapKey, ptr] = *itrToPtr;
376 std::unique_ptr<C> pointer = std::move(ptr);
377 children.erase(itrToPtr);
378 return pointer;
379 }
380
385 auto setParents(GameObject& parent) -> void
386 {
387 using namespace std;
388 for (auto& child : children)
389 {
390 if (!(get<1>(child)->parentTo(&parent))) // second object at pos 1 is unique ptr to gameobject
391 {
392 assert(0 && "Setting parents in children handeler");
393 }
394 }
395 }
396
397 // these are the regular overload functions
398 template <typename C>
399 friend auto to_json(json& j, const ChildrenHandeler<C>& ch) -> void;
400 template <typename C>
401 friend auto from_json(const json& j, ChildrenHandeler<C>& ch) -> void;
402
403 // this is too access component and node ChildrenHandler from_json functions
404 friend auto from_json(const json& j, ChildrenHandeler<C>& ch) -> void;
405
406private:
410 std::map<Key, std::unique_ptr<C>> children;
411
412
413};
414
415
416template <typename C>
417inline auto to_json(json& j, const ChildrenHandeler<C>& ch) -> void
418{
419 j["GameObject"] = ch.getGameObjectJson();
420 for (auto& child : ch.children)
421 {
422 j["children"].push_back(*(get<1>(child)));
423 }
424}
425
430template <typename C>
431auto from_json(const json& j, ChildrenHandeler<C>& ch) -> void
432{
433 /*for (const auto& child : j.at("children"))
434 {
435 C c = child.get<C>();
436 ch.add(std::make_unique<C>(c));
437 }*/
438 assert(0 && "Children Handler with non-Node and non-Component");
439 ch.setGameObjectJson(j.at("GameObject"));
440}
441
442auto BuildComponent(unsigned int comp, const json& j) -> Component*;
static FMOD_RESULT result
Definition AudioObject.cpp:27
sys::Json Stream
Definition AudioObject.h:20
@ C
Definition Card.h:26
auto BuildComponent(unsigned int comp, const json &j) -> Component *
Definition ChildrenHandeler.cpp:59
auto from_json(const json &j, ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:431
auto to_json(json &j, const ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:417
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:187
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition ChildrenHandeler.h:201
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:182
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:410
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:349
auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void override
set whether the GameObject should Receive Messags, and whether all of it's children should receive me...
Definition ChildrenHandeler.h:248
~ChildrenHandeler() override=default
default dtor.
auto onUpdate(float dt) -> void override
called once every frame.
Definition ChildrenHandeler.h:176
auto add(std::unique_ptr< C > newChild) -> void
Definition ChildrenHandeler.h:259
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:238
auto showMenu() -> void override
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition ChildrenHandeler.h:127
auto find(std::function< bool(C &)> predicate) -> C *
Returns pointer to child object.
Definition ChildrenHandeler.h:318
auto for_each(std::function< void(const C &)> func) const -> void
applies a function to every child.
Definition ChildrenHandeler.h:281
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:385
friend auto from_json(const json &j, ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:431
auto for_each(std::function< void(C &)> func) -> void
applies a function to every child.
Definition ChildrenHandeler.h:269
friend auto to_json(json &j, const ChildrenHandeler< C > &ch) -> void
Definition ChildrenHandeler.h:417
ChildrenHandeler(const ChildrenHandeler &other)
deep copy of ChildrenHandeler
Definition ChildrenHandeler.h:58
auto for_each(std::function< void(GameObject &)> func) -> void
applies a function to every child.
Definition ChildrenHandeler.h:293
auto count() -> size_t
Definition ChildrenHandeler.h:335
auto onRender() -> void override
called every frame after update has been called for every object.
Definition ChildrenHandeler.h:197
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition ChildrenHandeler.h:217
auto load(Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition ChildrenHandeler.h:210
auto getChildren() const -> std::shared_ptr< std::vector< GameObject * > > override
gets a vector of a GameObjects public (i.e.
Definition ChildrenHandeler.h:340
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:228
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition ChildrenHandeler.h:223
auto for_each(std::function< void(const GameObject &)> func) const -> void override
applies a function to every child.
Definition ChildrenHandeler.h:305
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:367
auto update(float dt) -> void override
called once every frame.
Definition ChildrenHandeler.h:135
Definition Component.h:28
a Key class to be used as a key when holding GameObjects (or unique pointers to game objects) in <key...
Definition GameObject.h:398
virtual auto update(float dt) -> void
called once every frame.
Definition GameObject.cpp:125
virtual auto setShouldReceiveMessages(const bool _shouldReceiveMessages) -> void
set whether the GameObject should Receive Messags, and whether all of it's children should receive me...
Definition GameObject.cpp:302
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
virtual auto setShouldUpdate(const bool _shouldUpdate) -> void
set whether the GameObject should update every frame, and whether all of it's children should update ...
Definition GameObject.cpp:290
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:23
virtual auto setShouldRender(const bool _shouldRender) -> void
set whether the GameObject should render every frame, and whether all of it's children should render ...
Definition GameObject.cpp:296
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
namespace for things relating to GameObject, other than GameObject itself
Definition Entity.h:229
@ 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