ECSTASY
All in the name
Loading...
Searching...
No Matches
Entities.cpp
Go to the documentation of this file.
1
11
12#include "Entities.hpp"
13
14namespace ecstasy
15{
16 Entities::Builder::Builder(Entities &parent, Entity entity) noexcept
17 : _parent(parent), _entity(entity), _built(false)
18 {
19 }
20
22 {
23 if (_built) [[unlikely]]
24 throw std::logic_error(
25 "Try to change entity using an Entities::Builder already consumed (build() has been called)");
26 }
27
29 {
30 assertNotBuilt();
31 _built = true;
32 _parent._alive[_entity.getIndex()] = true;
33 return _entity;
34 }
35
37 {
39 _alive.push(false);
40 _killed.push(true);
41 }
42
44 {
45 size_t firstDead = _alive.firstUnset();
46
48 if (firstDead == _generations.size()) {
50 _alive[firstDead] = alive;
52 _alive.push(false);
53
54 return Entity(firstDead, 1);
55 }
57 _generations[firstDead]++;
58 _alive[firstDead] = alive;
60 if (firstDead < _killed.size() - 1)
61 _killed[firstDead] = false;
62 return Entity(firstDead, _generations[firstDead]);
63 }
64
66 {
67 return Builder(*this, create(false));
68 }
69
71 {
72 if (id >= _generations.size())
73 return Entity(id, 0);
74 return Entity(id, _generations[id]);
75 }
76
78 {
79 Entity::Index id = entity.getIndex();
80
81 if (id >= _generations.size() || entity.getGeneration() != _generations[id] || !_alive[id])
82 return false;
83 _alive[id] = false;
84 return true;
85 }
86
88 {
89 size_t res = 0;
90
91 for (Entity entity : entities)
92 res += erase(entity);
93 return res;
94 }
95
97 {
98 Entity::Index id = entity.getIndex();
99
100 if (id >= _generations.size() || !_alive[id] || entity.getGeneration() != _generations[id])
101 return false;
102 if (id > _killed.size() - 1)
103 _killed.resizeSentinel(id + 1, true);
104 _killed[id] = true;
105 return true;
106 }
107
108 bool Entities::isAlive(Entity entity) const
109 {
110 return entity.getGeneration() != 0 && entity.getIndex() < _generations.size() && _alive[entity.getIndex()];
111 }
112
114 {
115 return get(index);
116 }
117
119 {
120 std::vector<Entity> deleted;
121 size_t firstKilled = _killed.firstSet();
122
123 while (firstKilled != _killed.size() - 1) {
124 _alive[firstKilled] = false;
125 _killed[firstKilled] = false;
127 deleted.push_back(Entity(firstKilled, _generations[firstKilled]));
128 firstKilled = _killed.firstSet(firstKilled);
129 }
130 return deleted;
131 }
132
133} // namespace ecstasy
Definition of the Entities class.
Entities builder to add multiple component to an entity on creation.
Definition Entities.hpp:38
void assertNotBuilt() const
Verify if the builder has not already been consumed.
Definition Entities.cpp:21
Builder(const Builder &other)=delete
Cannot copy a builder.
bool _built
Whether the builder has been consumed.
Definition Entities.hpp:135
Entity build()
Finalize the entity, making it alive.
Definition Entities.cpp:28
Resource holding all the Registry entities.
Definition Entities.hpp:30
Entity getQueryData(Entity::Index index) const
Get the entity from its index.
Definition Entities.cpp:113
Builder builder()
Create a new entity builder.
Definition Entities.cpp:65
bool kill(Entity entity)
Mark en entity for deletion.
Definition Entities.cpp:96
util::BitSet _alive
Mask of alive entities.
Definition Entities.hpp:303
Entities()
Construct a new Entities resource without any entity.
Definition Entities.cpp:36
bool erase(Entity entity)
Erase (delete) the entity when called.
Definition Entities.cpp:77
bool isAlive(Entity entity) const
Tests if an entity is alive.
Definition Entities.cpp:108
util::BitSet _killed
Mask of entities marked for deletion.
Definition Entities.hpp:305
Entity get(Entity::Index id) const noexcept
Retrieve an entity from its identifier.
Definition Entities.cpp:70
std::vector< Entity > maintain()
Effectively erase the entities marked for deletion (using kill()).
Definition Entities.cpp:118
std::vector< Entity::Generation > _generations
List of entity generations.
Definition Entities.hpp:301
Entity create(bool alive=true)
Create a new entity, alive or not.
Definition Entities.cpp:43
Encapsulate an index to an entity.
Definition Entity.hpp:35
constexpr Index getIndex() const noexcept
Get the entity identifier,.
Definition Entity.hpp:50
constexpr Generation getGeneration() const noexcept
Get the Generation of the entity.
Definition Entity.hpp:67
size_t Index
The entity identifier type.
Definition Entity.hpp:38
BIT_SET_CONSTEXPR std::size_t firstUnset(std::size_t start=0) const
Finds the first unset bit in the set, starting from (and including) start.
Definition BitSet.hpp:272
BitSet & push(bool value)
Adds the given bit to the end of the set.
Definition BitSet.cpp:175
constexpr std::size_t size() const noexcept
Definition BitSet.hpp:87
void resizeSentinel(std::size_t size, bool sentinelValue)
Changes the number of bits stored in this set, and sets the final bit past size to the value of senti...
Definition BitSet.cpp:167
BIT_SET_CONSTEXPR std::size_t firstSet(std::size_t start=0) const
Finds the first set bit in the set, starting from (and including) start.
Definition BitSet.hpp:229
Namespace containing all symbols specific to ecstasy.
Definition ecstasy.hpp:30
T push_back(T... args)
T size(T... args)