23#include "guillaume/ecs/entity.hpp"
28#include "guillaume/components/hierarchy.hpp"
29#include "guillaume/ecs/component_registry.hpp"
46 registry.getComponent<components::Hierarchy>(identifier);
48 hierarchy.setLayer(layer);
49 for (
const auto child: hierarchy.getChildIdentifiers()) {
50 if (registry.hasComponent<components::Hierarchy>(child)) {
51 propagateLayer(registry, child, layer + 1);
68 if (identifier == ancestor) {
71 if (!registry.hasComponent<components::Hierarchy>(identifier)) {
75 registry.getComponent<components::Hierarchy>(identifier)
76 .getParentIdentifier();
85 return currentId.fetch_add(1, std::memory_order_relaxed) + 1;
89 : utility::logging::Loggable<
Entity, utility::logging::DefaultLogger>()
90 , _identifier(getNextIdentifier())
106 _signature = signature;
111 _hierarchyRegistry = &componentRegistry;
121 if (_hierarchyRegistry !=
nullptr
135 const std::int32_t layer =
136 parent !=
nullptr ? parent->getLayer() + 1 : 0;
138 if (parent.get() ==
this) {
139 throw std::invalid_argument(
"An entity cannot be its own parent");
141 if (_hierarchyRegistry ==
nullptr) {
147 auto ®istry = *_hierarchyRegistry;
148 if (isSelfOrAncestor(registry, _identifier, parentIdentifier)) {
149 throw std::invalid_argument(
150 "An entity cannot be parented to one of its descendants");
155 const Identifier previous = hierarchy.getParentIdentifier();
156 if (previous != parentIdentifier) {
159 .removeChild(_identifier);
164 .addChild(_identifier);
166 hierarchy.setParentIdentifier(parentIdentifier);
170 propagateLayer(registry, _identifier, layer);
176 std::vector<Identifier> children;
179 if (_hierarchyRegistry ==
nullptr) {
183 auto ®istry = *_hierarchyRegistry;
185 const auto &hierarchy =
187 const Identifier parent = hierarchy.getParentIdentifier();
189 children = hierarchy.getChildIdentifiers();
192 .removeChild(_identifier);
195 for (
const auto child: children) {
202 registry.removeEntity(_identifier);
203 _hierarchyRegistry =
nullptr;
209 return _parent.lock();
Place of an entity in the entity tree: its parent, its direct children and its depth.
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
bool hasComponent(const Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component of a given type.
std::bitset< 128 > Signature
A set of component types, one bit per type (bit position given by ComponentTypeId,...
std::vector< Identifier > releaseComponents(void)
Drop the entity's place in the tree and all its components.
Entity & setParent(std::shared_ptr< Entity > parent)
Attach the entity under another one, or detach it.
Entity::Signature getSignature(void) const noexcept
Get the set of component types the entity declares.
Entity(void)
Create an entity with a fresh identifier, an empty signature, no parent and no component.
static Signature getSignatureFromTypes(void)
Build the signature that has exactly the bits of the given component types.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
std::int32_t getLayer(void) const noexcept
Get the depth of the entity in the tree.
std::shared_ptr< Entity > getParent(void) const noexcept
Get the parent entity.
void setSignature(void)
Replace the signature by the one made of the given component types.
void attachHierarchy(ComponentRegistry &componentRegistry)
Give the entity a components::Hierarchy in a registry and add that component to its signature.
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
The entity-component-system core, independent of any widget.