-IDManager
-times counting for tests
This commit is contained in:
parent
86e4e57f74
commit
17551b08a5
4 changed files with 118 additions and 10 deletions
65
source/ecs/id_manager.d
Normal file
65
source/ecs/id_manager.d
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
module ecs.id_manager;
|
||||
|
||||
import ecs.entity;
|
||||
import ecs.vector;
|
||||
|
||||
struct IDManager
|
||||
{
|
||||
EntityID getNewID()
|
||||
{
|
||||
while(m_next_id >= m_ids_array.length)m_ids_array.add(Data());
|
||||
EntityID id;
|
||||
id.id = m_next_id;
|
||||
id.counter = m_ids_array[m_next_id].counter++;
|
||||
m_next_id = m_ids_array[m_next_id].next_id;
|
||||
if(m_next_id == uint.max)m_next_id = cast(uint)m_ids_array.length;
|
||||
return id;
|
||||
}
|
||||
|
||||
void releaseID(EntityID id)
|
||||
{
|
||||
Data* data = &m_ids_array[id.id];
|
||||
if(data.counter != id.counter)return;
|
||||
data.next_id = m_next_id;
|
||||
data.entity = null;
|
||||
m_next_id = id.id;
|
||||
}
|
||||
|
||||
void update(ref Entity entity)
|
||||
{
|
||||
m_ids_array[entity.id.id].entity = &entity;
|
||||
}
|
||||
|
||||
struct Data
|
||||
{
|
||||
uint counter = 0;
|
||||
uint next_id = uint.max;
|
||||
Entity* entity = null;
|
||||
}
|
||||
|
||||
private uint m_next_id = 0;
|
||||
Vector!Data m_ids_array;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
IDManager manager;
|
||||
EntityID id1 = manager.getNewID();
|
||||
EntityID id2 = manager.getNewID();
|
||||
EntityID id3 = manager.getNewID();
|
||||
|
||||
assert(id1 == EntityID(0,1));
|
||||
assert(id2 == EntityID(1,1));
|
||||
assert(id3 == EntityID(2,1));
|
||||
|
||||
manager.releaseID(id2);
|
||||
manager.releaseID(id1);
|
||||
|
||||
id2 = manager.getNewID();
|
||||
id1 = manager.getNewID();
|
||||
|
||||
assert(id1 == EntityID(1,2));
|
||||
assert(id2 == EntityID(0,2));
|
||||
assert(id3 == EntityID(2,1));
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue