31#define LOG_CHILDREN_HANDELER 0
40template <
class C = Component>
77 for (
auto& [key, pointer] : other.children)
81 auto copy = pointer->clone();
83 auto copyCast =
static_cast<C*
>(copy.release());
85 children[copyCast->getKey()] = (std::unique_ptr<C>(copyCast));
98 ,
children(std::move(other.children))
116 children = std::move(other.children);
140 static auto to_erase = [](
auto& pair)
142 if (pair.second->destroyed())
144 hlg::Trace(
"{} was found to be destroyed, it will be removed"
145 , pair.second->getFormattedName()
147 return pair.second->destroyed();
159 constexpr auto pointer = 1;
165 , child->getFormattedName());
167 if (!child->getInternalParent())
169 hlg::Error(
"{} doesn't have a parent. Something is very wrong", child->getFormattedName());
172 if (key.getUUID() != child->getUUID())
174 hlg::Error(
"key uuid {} does not match {}", key.getUUID(), child->getFormattedName());
198 for (
auto&& [key, child] :
children)
211 for (
auto&& child : std::views::values(
children))
213 child->checkAddToSceneHook();
219 for (
auto&& child : std::views::values(
children))
221 child->onSceneStart(sceneName);
227 for (
auto&& child : std::views::values(
children))
229 child->onSceneExit(sceneName);
237 *
this = stream.data();
243 stream.data() = *
this;
249 return std::make_unique<ChildrenHandeler>(*
this);
256 for (
auto&& [key, child] :
children)
258 child->setShouldUpdate(_shouldUpdate);
266 for (
auto&& [key, child] :
children)
268 child->setShouldRender(_shouldRender);
276 for (
auto&& [key, child] :
children)
278 child->setShouldReceiveMessages(_shouldReceiveMessages);
283 auto add(std::unique_ptr<C> newChild) ->
void
285 assert(newChild &&
"ChildHandler add was passed null unique_ptr");
286 children[newChild->getKey()] = std::move(newChild);
305 auto for_each(std::function<
void(
const C&)> func)
const ->
void
342 auto find(std::function<
bool(
C&)> predicate) ->
C*
345 constexpr auto obj_ptr = 1;
348 if (predicate(*get<obj_ptr>(child)))
352 return &*get<obj_ptr>(child);
377 auto result = std::make_shared<std::vector<GameObject*>>();
381 result->push_back(ptr.get());
396 assert(
false &&
"Tried to remove an object that doesn't exist");
397 hlg::Error(
"Tried to remove an object that doesn't exist");
399 auto& [mapKey, ptr] = *itrToPtr;
400 std::unique_ptr<C> pointer = std::move(ptr);
415 if (!(get<1>(child)->
parentTo(&parent)))
417 assert(0 &&
"Setting parents in children handeler");
423 template <
typename C>
425 template <
typename C>
444 j[
"GameObject"] = ch.getGameObjectJson();
445 for (
auto& child : ch.children)
447 j[
"children"].push_back(*(get<1>(child)));
463 assert(0 &&
"Children Handler with non-Node and non-Component");
464 ch.setGameObjectJson(j.at(
"GameObject"));
static FMOD_RESULT result
Definition AudioObject.cpp:27
sys::Json Stream
Definition AudioObject.h:20
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