-added removeComponents() function (removing by ids array, or components types)
-sorting IDs in addComponents() and removeComponents()
This commit is contained in:
parent
5b2a6e9890
commit
a066a97f01
3 changed files with 118 additions and 21 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,6 +2,7 @@ dub.userprefs
|
|||
dub.selections.json
|
||||
.dub
|
||||
/.vscode
|
||||
LOC
|
||||
perf.data
|
||||
perf.data.old
|
||||
*.o
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ class EntityManager
|
|||
|
||||
foreach (info; &entities_infos.byValue)
|
||||
{
|
||||
addEntityCaller(*info, cast(uint)systems.length - 1);
|
||||
addEntityCaller(*info, cast(uint) systems.length - 1);
|
||||
}
|
||||
|
||||
updateEntityCallers();
|
||||
|
|
@ -181,19 +181,20 @@ class EntityManager
|
|||
num = cast(ushort)((num + aligment - 1) & (-cast(int) aligment)); //num += aligment - (num & (aligment - 1));
|
||||
}
|
||||
|
||||
extern (C) static int compareUShorts(const void* a, const void* b)
|
||||
{
|
||||
ushort _a = *cast(ushort*) a;
|
||||
ushort _b = *cast(ushort*) b;
|
||||
if (_a < _b)
|
||||
return -1;
|
||||
else if (_a == _b)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
{
|
||||
extern (C) static int compareUShorts(const void* a, const void* b)
|
||||
{
|
||||
ushort _a = *cast(ushort*) a;
|
||||
ushort _b = *cast(ushort*) b;
|
||||
if (_a < _b)
|
||||
return -1;
|
||||
else if (_a == _b)
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * components_ids.length))[0
|
||||
.. components_ids.length];
|
||||
|
|
@ -268,9 +269,9 @@ class EntityManager
|
|||
|
||||
void updateEntityCallers()
|
||||
{
|
||||
foreach(entity;&entities_infos.byValue)
|
||||
foreach (entity; &entities_infos.byValue)
|
||||
{
|
||||
foreach(ref caller;entity.callers)
|
||||
foreach (ref caller; entity.callers)
|
||||
{
|
||||
caller.system = &systems[caller.system_id];
|
||||
}
|
||||
|
|
@ -305,12 +306,13 @@ class EntityManager
|
|||
call_data.deltas = Mallocator.instance.makeArray(deltas); //Mallocator.instance.makeArray!ushort(system.m_components.length);
|
||||
|
||||
uint index = 0;
|
||||
for(;index<entity.callers.length;index++)
|
||||
for (; index < entity.callers.length; index++)
|
||||
{
|
||||
CallData* caller = &entity.callers[index];
|
||||
if(caller.system.priority >= call_data.system.priority)break;
|
||||
if (caller.system.priority >= call_data.system.priority)
|
||||
break;
|
||||
}
|
||||
entity.callers.add(call_data,index);
|
||||
entity.callers.add(call_data, index);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -319,6 +321,69 @@ class EntityManager
|
|||
return cast(Entity*) id_manager.getEntityPointer(id);
|
||||
}
|
||||
|
||||
void removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
{
|
||||
Entity* entity = id_manager.getEntityPointer(entity_id);
|
||||
EntitiesBlock* block = getMetaData(entity);
|
||||
EntityInfo* info = block.type_data;
|
||||
|
||||
qsort(del_ids.ptr, del_ids.length, ushort.sizeof, &compareUShorts);
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length)))[0
|
||||
.. info.components.length];
|
||||
|
||||
uint j = 0;
|
||||
uint k = 0;
|
||||
foreach (id;info.components)
|
||||
{
|
||||
while (k < del_ids.length && del_ids[k] < id)
|
||||
k++;
|
||||
if (k >= del_ids.length)
|
||||
{
|
||||
ids[j++] = id;
|
||||
}
|
||||
else if (del_ids[k] == info.components[j])
|
||||
{
|
||||
k++;
|
||||
}
|
||||
else
|
||||
ids[j++] = id;
|
||||
}
|
||||
|
||||
if (j == info.components.length)
|
||||
return;
|
||||
|
||||
EntityInfo* new_info = getEntityInfo(ids[0 .. j]);
|
||||
|
||||
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
|
||||
|
||||
void* start = new_block.dataBegin() + new_block.entities_count * new_info.size;
|
||||
|
||||
Entity* new_entity = cast(Entity*) start;
|
||||
new_entity.id = entity.id;
|
||||
new_entity.updateID();
|
||||
|
||||
foreach (comp; new_info.components)
|
||||
{
|
||||
memcpy(cast(void*) new_entity + new_info.deltas[comp],
|
||||
cast(void*) entity + info.deltas[comp], components[comp].size);
|
||||
}
|
||||
|
||||
removeEntityNoID(entity, block);
|
||||
}
|
||||
|
||||
void removeComponents(Components...)(EntityID entity_id)
|
||||
{
|
||||
const uint num = Components.length;
|
||||
ushort[num] del_ids;
|
||||
static foreach (i, comp; Components)
|
||||
{
|
||||
del_ids[i] = comp.component_id;
|
||||
}
|
||||
|
||||
removeComponents(entity_id,del_ids);
|
||||
}
|
||||
|
||||
void addComponents(Components...)(EntityID entity_id, Components comps)
|
||||
{
|
||||
const uint num = Components.length;
|
||||
|
|
@ -341,6 +406,30 @@ class EntityManager
|
|||
pointers[i] = ∁
|
||||
}
|
||||
|
||||
foreach(int i;0..new_ids.length)
|
||||
{
|
||||
ushort min = new_ids[i];
|
||||
int pos = i;
|
||||
foreach(int j;0..new_ids.length)
|
||||
{
|
||||
if(new_ids[j] < min)
|
||||
{
|
||||
min = new_ids[j];
|
||||
pos = j;
|
||||
}
|
||||
}
|
||||
if(pos != i)
|
||||
{
|
||||
ushort id = new_ids[i];
|
||||
new_ids[i] = new_ids[pos];
|
||||
new_ids[pos] = id;
|
||||
void* ptr = pointers[i];
|
||||
pointers[i] = pointers[pos];
|
||||
pointers[pos] = ptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint j = 0;
|
||||
uint k = 0;
|
||||
foreach (ref id; ids)
|
||||
|
|
@ -442,7 +531,7 @@ class EntityManager
|
|||
{
|
||||
if (block is null)
|
||||
{
|
||||
block = cast(EntitiesBlock*) allocator.getBlock();//AlignedMallocator.instance.alignedAllocate(page_size, page_size);
|
||||
block = cast(EntitiesBlock*) allocator.getBlock(); //AlignedMallocator.instance.alignedAllocate(page_size, page_size);
|
||||
*block = EntitiesBlock(info);
|
||||
if (previous_block is null)
|
||||
{
|
||||
|
|
@ -498,12 +587,12 @@ class EntityManager
|
|||
if (block.type_data.first_with_free_space.id > block.id)
|
||||
block.type_data.first_with_free_space = block;
|
||||
|
||||
if(call_destructors)
|
||||
if (call_destructors)
|
||||
{
|
||||
void* data = data_begin + pos * block.type_data.size;
|
||||
foreach(comp;block.type_data.components)
|
||||
foreach (comp; block.type_data.components)
|
||||
{
|
||||
if(components[comp].destroy_callback)
|
||||
if (components[comp].destroy_callback)
|
||||
{
|
||||
components[comp].destroy_callback(data + block.type_data.deltas[comp]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -263,6 +263,13 @@ int main()
|
|||
|
||||
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
|
||||
|
||||
gEM.removeComponents!(TestComp)(entity.id);
|
||||
pp = gEM.getEntity(entity.id);
|
||||
|
||||
gEM.update();
|
||||
|
||||
writeln((cast(uint*) pp)[0 .. 14], " ", pp);
|
||||
|
||||
//import std.stdio;
|
||||
//writeln((cast(uint*)tmpl.info.first_block)[0..48]);
|
||||
gEM.freeTemplate(tmpl);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue