Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1/**************************************************************************
2 * Relationship v0.0.0
3 *
4 * Relationship is a software package, part of the Engine².
5 *
6 * This file is part of the EngineSquared project that is under GPL-3.0 License.
7 * Copyright © 2024 by @EngineSquared, All rights reserved.
8 *
9 * EngineSquared is a free software: you can redistribute it and/or modify
10 * it under the terms of the GPL-3.0 License as published by the
11 * Free Software Foundation. See the GPL-3.0 License for more details.
12 *
13 * @file Utils.hpp
14 * @brief utility functions for the Relationship component
15 *
16 * This file contains the declaration of the utility functions for the Relationship component.
17 *
18 * @author @miou-zora
19 * @version 0.0.0
20 * @date 18-02-2024
21 **************************************************************************/
22
23#pragma once
24
26
34auto SetChildOf(Engine::Entity child, Engine::Entity parent) -> void;
35
43auto IsChildOf(Engine::Entity child, Engine::Entity parent) -> bool;
44
50auto RemoveParent(Engine::Entity child) -> void;
51
58auto GetParent(Engine::Entity child) -> std::optional<Engine::Entity>;
59
66template <typename TFunc> auto ForEachChild(Engine::Entity parent, TFunc func) -> void
67{
70 if (!parentRS)
71 {
72 Log::Warning(fmt::format("Entity({}) has no Relationship component in ForEachChild", parent));
73 return;
74 }
75
76 auto currentChildOpt = parentRS->first;
77 while (currentChildOpt.has_value())
78 {
79 Engine::Entity currentChild = currentChildOpt.value();
80 func(currentChild);
81 const Relationship::Component::Relationship *currentChildRS =
83 if (!currentChildRS)
84 {
85 break;
86 }
87 currentChildOpt = currentChildRS->next;
88 }
89}
90
97
98template <typename TComponent>
99auto GetChildComponents(Engine::Entity parent) -> std::vector<std::reference_wrapper<TComponent>>
100{
101 std::vector<std::reference_wrapper<TComponent>> childComponents;
102 ForEachChild(parent, [&childComponents](Engine::Entity child) {
103 TComponent *childComponent = child.TryGetComponent<TComponent>();
104 if (childComponent)
105 {
106 childComponents.emplace_back(*childComponent);
107 }
108 });
109 return childComponents;
110}
111
119template <typename TComponent> auto TryGetParentComponent(Engine::Entity child) -> TComponent *
120{
121 std::optional<Engine::Entity> parentOpt = GetParent(child);
122 if (!parentOpt.has_value())
123 {
124 Log::Warning(fmt::format("Entity({}) has no parent in TryGetParentComponent", child));
125 return nullptr;
126 }
127 Engine::Entity parent = parentOpt.value();
128 return parent.TryGetComponent<TComponent>();
129}
130} // namespace Relationship::Utils
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
decltype(auto) TryGetComponent()
Try to get a component of type TComponent from the entity. It returns a pointer to the component if i...
Definition Entity.ipp:41
void Warning(const T &msg) noexcept(false)
Definition Logger.hpp:32
Definition Utils.hpp:27
auto TryGetParentComponent(Engine::Entity child) -> TComponent *
Definition Utils.hpp:119
auto IsChildOf(Engine::Entity child, Engine::Entity parent) -> bool
Definition Utils.cpp:36
auto ForEachChild(Engine::Entity parent, TFunc func) -> void
Definition Utils.hpp:66
auto GetParent(Engine::Entity child) -> std::optional< Engine::Entity >
Definition Utils.cpp:81
auto SetChildOf(Engine::Entity child, Engine::Entity parent) -> void
Definition Utils.cpp:5
auto GetChildComponents(Engine::Entity parent) -> std::vector< std::reference_wrapper< TComponent > >
Definition Utils.hpp:99
auto RemoveParent(Engine::Entity child) -> void
Definition Utils.cpp:43
Definition Relationship.hpp:31
std::optional< Engine::Entity > next
Definition Relationship.hpp:39
std::optional< Engine::Entity > first
Definition Relationship.hpp:35