21 entt::registry* registry = subject.
GetRawHandle().registry();
23 return std::unexpected(
Core::CoriError(
"Linking 2 entities is only allowed when both of them have a name."));
31 const auto& name = subject.
GetName();
32 if (cache.m_Children.contains(name)) {
33 return std::unexpected(
Core::CoriError(
"A parent entity can't have 2 children with the same name."));
35 cache.m_Children.emplace(name, subject.
GetRawHandle().entity());
38 const entt::entity firstChild = parentHierarchy.m_FirstChild;
40 if (registry->valid(firstChild)) {
42 hierarchy.m_NextSibling = firstChild;
45 parentHierarchy.m_FirstChild = subject.
GetRawHandle().entity();
51 entt::registry* registry = subject.
GetRawHandle().registry();
56 const entt::entity parent = hierarchy.m_Parent;
57 if (!registry->valid(parent)) {
66 cache.m_Children.erase(std::string(subject.
GetName()));
69 const entt::entity previousSibling = parentHierarchy.m_PreviousSibling;
70 const entt::entity nextSibling = parentHierarchy.m_NextSibling;
72 if (parentHierarchy.m_FirstChild == subject.
GetRawHandle().entity()) {
73 parentHierarchy.m_FirstChild = nextSibling;
75 if (registry->valid(previousSibling)) {
80 if (registry->valid(nextSibling)) {
84 hierarchy.m_Parent = entt::null;
85 hierarchy.m_NextSibling = entt::null;
86 hierarchy.m_PreviousSibling = entt::null;
92 return std::unexpected(
Core::CoriError(
"Entity doesn't have hierarchy component, and thus doesn't have any siblings."));
95 entt::registry* registry = subject.
GetRawHandle().registry();
96 if (!registry->valid(hierarchy.m_Parent)) {
97 return std::unexpected(
Core::CoriError(
"Entity doesn't have a parent, and thus doesn't have any siblings."));
100 const Entity parent = entt::handle{ *registry, hierarchy.m_Parent };
103 return siblings.value();
106 return std::unexpected(siblings.error());
110 std::vector<Entity> children;
113 return std::unexpected(
Core::CoriError(
"Entity doesn't have any children."));
116 entt::registry* registry = subject.
GetRawHandle().registry();
119 for (
const auto& child : childCache.m_Children | std::views::values) {
120 children.emplace_back(entt::handle{*registry, child});
128 return std::unexpected(
Core::CoriError(
"Entity doesn't have HierarchyComponent, and thus doesn't have a parent."));
132 if (hierarchy.m_Parent == entt::null) {
133 return std::unexpected(
Core::CoriError(
"Entity doesn't have a parent."));
136 return Entity{ entt::handle{ *subject.
GetRawHandle().registry(), hierarchy.m_Parent } };
141 return std::unexpected(
Core::CoriError(
"Entity doesn't have any children."));
146 if (cache.m_Children.contains(name)) {
147 entt::entity child = cache.m_Children.find(name)->second;
150 return std::unexpected(
Core::CoriError(
"No children found with the specified name."));
155 return std::unexpected(
Core::CoriError(
"Entity doesn't have any children."));
160 if (cache.m_Children.contains(name)) {
161 entt::entity child = cache.m_Children.find(name)->second;
164 return std::unexpected(
Core::CoriError(
"No children found with the specified name."));
169 return std::unexpected(
Core::CoriError(
"Entity doesn't have any children."));
174 if (cache.m_Children.contains(name)) {
175 entt::entity child = cache.m_Children.find(name)->second;
178 return std::unexpected(
Core::CoriError(
"No children found with the specified name."));
182 entt::registry* registry = subject.
GetRawHandle().registry();
186 cache.m_Children.clear();
188 entt::entity currentChild = hierarchy.m_FirstChild;
189 while (registry->valid(currentChild)) {
191 registry->destroy(currentChild);
192 currentChild = nextChild;
205 while (subject.
GetRawHandle().registry()->valid(currentParent)) {
206 root = currentParent;
212 const std::string_view rootName = rootHandle.
GetName();
218 for (
size_t i = 0; i < children.value().size(); ++i) {
219 const auto& child = children.value()[i];
220 const bool isLastChild = i == children.value().size() - 1;
236 const std::string childPrefix = prefix + (isLast ?
" " :
"│ ");
240 for (
size_t i = 0; i < children.value().size(); ++i) {
241 const auto& child = children.value()[i];
242 const bool isLastChild = i == children.value().size() - 1;
248 void Hierarchy::OnHierarchyComponentDestroyed(entt::registry& registry, entt::entity entity) {
249 Entity e = entt::handle{ registry, entity };
#define CORI_CORE_DEBUG_TAGGED(...)
#define CORI_CORE_TRACE_TAGGED(...)
Custom error class mainly used in std::expected.
Entities are the essential part of WorldSystem.
bool IsValid() const
Checks if the actual entity behind this handle is valid.
bool IsActiveGlobally() const
Checks if the entity is active locally (doesn't have InactiveGloballyFlag), just a convenience functi...
std::string_view GetName() const
Retrieves the name of the entity.
entt::handle GetRawHandle() const
Gets a raw entt::handle if you need to interact with entt directly.
std::expected< std::vector< Entity >, Core::CoriError<> > GetChildren() const
Creates and returns a vector containing all entity children (does not include grandchildren and so on...
T & GetOrAddComponent(Args &&... args)
Retries or adds a component to the entity.
void DestroyChildren()
Destroys all children (and they grandchildren) that the entity has.
bool HasComponents() const
Checks if the entity has all the specified components.
decltype(auto) GetComponents()
Retries the references to the requested components of the entity.
entt::entity GetRawEntity() const
Gets a raw entt::entity if you need to interact with entt directly.
void UpdateInactivityFlagsRecursive(entt::entity parent, bool parentIsActive)
static std::expected< std::vector< Entity >, Core::CoriError<> > GetSiblings(Entity subject)
static void DrawHierarchyRecursive(const Entity &entity, const std::string &prefix, const bool isLast)
static std::expected< void, Core::CoriError<> > LinkToParent(Entity subject, Entity parent)
static std::expected< Entity, Core::CoriError<> > GetParent(Entity subject)
static std::expected< Entity, Core::CoriError<> > FindChildByName(Entity subject, const char *name)
static std::expected< void, Core::CoriError<> > SetParent(Entity subject, Entity parent)
static void UnlinkFromParent(Entity subject)
static void DestroyChildren(Entity subject)
static std::expected< std::vector< Entity >, Core::CoriError<> > GetChildren(Entity subject)
static void PrintHierarchy(Entity subject)
Anything connected to WorldSystem (ECS) is in this namespace.
Every Entity has a ChildCache component by default, it's essential for entity ChildCache system.
Every Entity has a hierarchy component by default, it's essential for entity hierarchy system.
Every Entity by default has a name component, it holds a non-unique entity name. Just holds the data,...