-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
|
||||
|
|
|
|||
|
|
@ -181,8 +181,6 @@ class EntityManager
|
|||
num = cast(ushort)((num + aligment - 1) & (-cast(int) aligment)); //num += aligment - (num & (aligment - 1));
|
||||
}
|
||||
|
||||
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
{
|
||||
extern (C) static int compareUShorts(const void* a, const void* b)
|
||||
{
|
||||
ushort _a = *cast(ushort*) a;
|
||||
|
|
@ -195,6 +193,9 @@ class EntityManager
|
|||
return 1;
|
||||
}
|
||||
|
||||
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
{
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * components_ids.length))[0
|
||||
.. components_ids.length];
|
||||
ids[0 .. $] = components_ids[];
|
||||
|
|
@ -308,7 +309,8 @@ class EntityManager
|
|||
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);
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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