ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::Entities Class Reference

Resource holding all the Registry entities. More...

#include <Entities.hpp>

Inheritance diagram for ecstasy::Entities:
Collaboration diagram for ecstasy::Entities:

Classes

class  Builder
 Entities builder to add multiple component to an entity on creation. More...
 

Public Types

using QueryData = Entity
 QueryableObject constraint
 
using ConstQueryData = QueryData
 ConstQueryableObject constraint
 

Public Member Functions

 Entities ()
 Construct a new Entities resource without any entity.
 
Entity create (bool alive=true)
 Create a new entity, alive or not.
 
Builder builder ()
 Create a new entity builder.
 
Entity get (Entity::Index id) const noexcept
 Retrieve an entity from its identifier.
 
bool erase (Entity entity)
 Erase (delete) the entity when called.
 
size_t erase (std::span< Entity > entities)
 Erase multiple entities when called.
 
bool kill (Entity entity)
 Mark en entity for deletion.
 
bool isAlive (Entity entity) const
 Tests if an entity is alive.
 
constexpr const util::BitSetgetMask () const noexcept
 Get the Alive entities mask.
 
Entity getQueryData (Entity::Index index) const
 Get the entity from its index.
 
std::vector< Entitymaintain ()
 Effectively erase the entities marked for deletion (using kill()).
 
- Public Member Functions inherited from ecstasy::IResource
virtual ~IResource ()=default
 Default destructor.
 

Private Attributes

std::vector< Entity::Generation_generations
 List of entity generations.
 
util::BitSet _alive
 Mask of alive entities.
 
util::BitSet _killed
 Mask of entities marked for deletion.
 

Detailed Description

Resource holding all the Registry entities.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 30 of file Entities.hpp.

Member Typedef Documentation

◆ ConstQueryData

ConstQueryableObject constraint

Definition at line 164 of file Entities.hpp.

◆ QueryData

QueryableObject constraint

Definition at line 162 of file Entities.hpp.

Constructor & Destructor Documentation

◆ Entities()

ecstasy::Entities::Entities ( )

Construct a new Entities resource without any entity.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Add sentinel to allow use of firstUnset/firstSet

Definition at line 36 of file Entities.cpp.

37 {
39 _alive.push(false);
40 _killed.push(true);
41 }
util::BitSet _alive
Mask of alive entities.
Definition Entities.hpp:303
util::BitSet _killed
Mask of entities marked for deletion.
Definition Entities.hpp:305
BitSet & push(bool value)
Adds the given bit to the end of the set.
Definition BitSet.cpp:175

Member Function Documentation

◆ builder()

Entities::Builder ecstasy::Entities::builder ( )

Create a new entity builder.

Returns
Builder Newly created builder.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-19)

Definition at line 65 of file Entities.cpp.

66 {
67 return Builder(*this, create(false));
68 }
Entity create(bool alive=true)
Create a new entity, alive or not.
Definition Entities.cpp:43

◆ create()

Entity ecstasy::Entities::create ( bool  alive = true)

Create a new entity, alive or not.

Parameters
[in]aliveWhether the entity must be created alive or not.
Returns
Entity Newly created entity.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

No index available, need to add one at the end

Always add a sentinel bit

One index available, use it

Remove deletion marker

Definition at line 43 of file Entities.cpp.

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 }
std::vector< Entity::Generation > _generations
List of entity generations.
Definition Entities.hpp:301
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
constexpr std::size_t size() const noexcept
Definition BitSet.hpp:87
T push_back(T... args)
T size(T... args)

◆ erase() [1/2]

bool ecstasy::Entities::erase ( Entity  entity)

Erase (delete) the entity when called.

Note
Entity is considered erased if it was alive.
Parameters
[in]entityEntity to erase.
Returns
bool Whether the entity was erased or not.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 77 of file Entities.cpp.

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 }
size_t Index
The entity identifier type.
Definition Entity.hpp:38

◆ erase() [2/2]

size_t ecstasy::Entities::erase ( std::span< Entity entities)

Erase multiple entities when called.

Note
An entity is considered erased if it was alive before.
Parameters
[in]entitiesentities to erase.
Returns
size_t Number of entities erased.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-21)

Definition at line 87 of file Entities.cpp.

88 {
89 size_t res = 0;
90
91 for (Entity entity : entities)
92 res += erase(entity);
93 return res;
94 }
bool erase(Entity entity)
Erase (delete) the entity when called.
Definition Entities.cpp:77

◆ get()

Entity ecstasy::Entities::get ( Entity::Index  id) const
noexcept

Retrieve an entity from its identifier.

Parameters
[in]idIndex of the entity.
Returns
Entity
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 70 of file Entities.cpp.

71 {
72 if (id >= _generations.size())
73 return Entity(id, 0);
74 return Entity(id, _generations[id]);
75 }

◆ getMask()

constexpr const util::BitSet & ecstasy::Entities::getMask ( ) const
inlineconstexprnoexcept

Get the Alive entities mask.

Returns
const util::BitSet& alive entities mask.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-20)

Definition at line 269 of file Entities.hpp.

270 {
271 return _alive;
272 }

◆ getQueryData()

Entity ecstasy::Entities::getQueryData ( Entity::Index  index) const

Get the entity from its index.

Note
QueryableObject constraints.
Returns
const Entity Entity at index index.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-20)

Definition at line 113 of file Entities.cpp.

114 {
115 return get(index);
116 }
Entity get(Entity::Index id) const noexcept
Retrieve an entity from its identifier.
Definition Entities.cpp:70

◆ isAlive()

bool ecstasy::Entities::isAlive ( Entity  entity) const

Tests if an entity is alive.

Parameters
[in]entityentity to test.
Returns
bool If the entity is alive.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-18)

Definition at line 108 of file Entities.cpp.

109 {
110 return entity.getGeneration() != 0 && entity.getIndex() < _generations.size() && _alive[entity.getIndex()];
111 }

◆ kill()

bool ecstasy::Entities::kill ( Entity  entity)

Mark en entity for deletion.

The entity is alive until a call to ecstasy::Entities::maintain() is made.

Parameters
[in]entityEntity to kill.
Returns
bool Whether or not the entity is marked for deletion.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-21)

Definition at line 96 of file Entities.cpp.

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 }
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

◆ maintain()

std::vector< Entity > ecstasy::Entities::maintain ( )

Effectively erase the entities marked for deletion (using kill()).

Note
Invalidate iterators.
Warning
This function doesn't delete the entities components, see Registry::maintain() instead.
Returns
std::vector<Entity> List of deleted entities.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-21)

Cannot emplace_back because constructor is private in the vector context :/

Definition at line 118 of file Entities.cpp.

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 }
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

Member Data Documentation

◆ _alive

util::BitSet ecstasy::Entities::_alive
private

Mask of alive entities.

Definition at line 303 of file Entities.hpp.

◆ _generations

std::vector<Entity::Generation> ecstasy::Entities::_generations
private

List of entity generations.

Definition at line 301 of file Entities.hpp.

◆ _killed

util::BitSet ecstasy::Entities::_killed
private

Mask of entities marked for deletion.

Definition at line 305 of file Entities.hpp.


The documentation for this class was generated from the following files: