23#include "guillaume/ecs/entity_registry.hpp"
27#include "guillaume/ecs/level_order_traveler.hpp"
44 std::size_t &getTreeVersion(
void)
46 static std::size_t version = 0;
58 std::vector<Entity::Identifier> &orphans)
67 [identifier](
const std::shared_ptr<Entity> &entity) {
68 return entity->getIdentifier() == identifier;
74 && nested->removeOne(identifier, orphans)) {
82 const std::shared_ptr<Entity> entity = *iterator;
84 nested->clearOwned(orphans);
86 const auto children = entity->releaseComponents();
87 orphans.insert(orphans.end(), children.begin(), children.end());
88 entities.erase(iterator);
93 void EntityRegistry::clearOwned(std::vector<Entity::Identifier> &orphans)
97 for (
auto &entity: entities) {
99 nested->clearOwned(orphans);
101 const auto children = entity->releaseComponents();
102 orphans.insert(orphans.end(), children.begin(), children.end());
110 std::vector<Entity::Identifier> orphans;
112 if (!removeOne(identifier, orphans)) {
117 while (!orphans.empty()) {
120 removeOne(orphan, orphans);
127 std::vector<Entity::Identifier> orphans;
132 std::vector<std::shared_ptr<Entity>>
135 if (_cachedVersion == getTreeVersion()) {
140 std::queue<EntityRegistry *> registries;
142 registries.push(
this);
143 while (!registries.empty()) {
148 _bfsCache.push_back(entity);
150 auto *childRegistry =
152 if (childRegistry !=
nullptr) {
153 registries.push(childRegistry);
158 _cachedVersion = getTreeVersion();
162 std::vector<std::shared_ptr<Entity>>
165 if (_cachedVersion == getTreeVersion()) {
170 std::queue<const EntityRegistry *> registries;
172 registries.push(
this);
173 while (!registries.empty()) {
178 _bfsCache.push_back(entity);
180 auto *childRegistry =
182 if (childRegistry !=
nullptr) {
183 registries.push(childRegistry);
188 _cachedVersion = getTreeVersion();
203 std::vector<Entity::Identifier> matchingIdentifiers;
204 for (
const auto &entity: traveler.
travel(*
this)) {
205 if ((entity->getSignature() & systemSignature) == systemSignature) {
206 matchingIdentifiers.push_back(entity->getIdentifier());
210 return matchingIdentifiers;
virtual std::vector< std::shared_ptr< Entity > > & accessDirectEntities(void)=0
Give access to the list of entities this registry owns directly. Implemented by the class that provid...
std::vector< Entity::Identifier > getEntityWithSignature(Entity::Signature systemSignature) const
List the entities that have every component type of a signature, from the root down.
bool removeEntity(Entity::Identifier identifier)
Remove an entity, with its children and all their components.
std::vector< std::shared_ptr< Entity > > getEntitiesBreadthFirst(void)
List every entity of this registry and of the registries nested in it, level by level.
EntityRegistry(void)=default
Create a registry.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
void clearEntities(void)
Remove every entity owned by this registry, with everything they own and all their components.
Decides in which order the entities of a scene are visited.
virtual std::vector< std::shared_ptr< Entity > > travel(EntityRegistry &entityRegistry) const =0
List every entity below a registry, in this traveler's order.
std::bitset< 128 > Signature
A set of component types, one bit per type (bit position given by ComponentTypeId,...
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Visits owners before what they own: the entities owned by the scene first, then the entities those ow...
The entity-component-system core, independent of any widget.
Widgets: the classes used to create entities with the right components and children.