-LinearLayout proggress:
*EntityTemplate.getComponent *Adding/removing components for Entitites -better code for adding components
This commit is contained in:
parent
cb9bac5dde
commit
c18ac54265
4 changed files with 95 additions and 40 deletions
|
|
@ -45,6 +45,7 @@ export struct EntityTemplate
|
||||||
|
|
||||||
T* getComponent(T)()
|
T* getComponent(T)()
|
||||||
{
|
{
|
||||||
return cast(T*)(entity_data.ptr + info.deltas[T.component_id]);
|
version(LinearLayout)return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]);
|
||||||
|
else return cast(T*)(entity_data.ptr + info.deltas[T.component_id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ struct IDManager
|
||||||
|
|
||||||
void update(ref Entity entity)
|
void update(ref Entity entity)
|
||||||
{
|
{
|
||||||
m_ids_array[entity.id.id].entity = &entity;
|
if(entity.id.counter == m_ids_array[entity.id.id].counter)m_ids_array[entity.id.id].entity = &entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
export Entity* getEntityPointer(EntityID id)
|
export Entity* getEntityPointer(EntityID id)
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,9 @@ class EntityManager
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ret ~= "types[" ~ i.to!string ~ "][] array" ~ req.to!string ~ " = (cast(types[" ~ i.to!string ~ "]*)(cast(void*)block + info.deltas[types[" ~ i.to!string ~ "].component_id]))[0..block.entities_count];";
|
ret ~= "types[" ~ i.to!string ~ "][] array" ~ req.to!string ~ " = (cast(types["
|
||||||
|
~ i.to!string ~ "]*)(cast(void*)block + info.deltas[types["
|
||||||
|
~ i.to!string ~ "].component_id]))[0..block.entities_count];";
|
||||||
req++;
|
req++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -235,7 +237,8 @@ class EntityManager
|
||||||
pointer = null;
|
pointer = null;
|
||||||
}*/
|
}*/
|
||||||
EntityInfo* info = block.type_info;
|
EntityInfo* info = block.type_info;
|
||||||
Entity[] id_array = (cast(Entity*)block.dataBegin())[0..block.entities_count];
|
Entity[] id_array = (cast(Entity*) block.dataBegin())[0
|
||||||
|
.. block.entities_count];
|
||||||
mixin(genArrays());
|
mixin(genArrays());
|
||||||
foreach (i; 0 .. block.entities_count)
|
foreach (i; 0 .. block.entities_count)
|
||||||
{
|
{
|
||||||
|
|
@ -710,11 +713,27 @@ class EntityManager
|
||||||
new_entity.id = entity.id;
|
new_entity.id = entity.id;
|
||||||
new_entity.updateID();
|
new_entity.updateID();
|
||||||
|
|
||||||
|
version (LinearLayout)
|
||||||
|
{
|
||||||
|
static if (EntityID.sizeof == 8)
|
||||||
|
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
|
||||||
|
else
|
||||||
|
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
|
||||||
|
foreach (comp; new_info.components)
|
||||||
|
{
|
||||||
|
uint comp_size = components[comp].size;
|
||||||
|
memcpy(cast(void*) new_block + new_info.deltas[comp] + new_block.entities_count * comp_size,
|
||||||
|
cast(void*) block + info.deltas[comp] + ind * comp_size, comp_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
foreach (comp; new_info.components)
|
foreach (comp; new_info.components)
|
||||||
{
|
{
|
||||||
memcpy(cast(void*) new_entity + new_info.deltas[comp],
|
memcpy(cast(void*) new_entity + new_info.deltas[comp],
|
||||||
cast(void*) entity + info.deltas[comp], components[comp].size);
|
cast(void*) entity + info.deltas[comp], components[comp].size);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
new_block.entities_count++;
|
new_block.entities_count++;
|
||||||
|
|
||||||
|
|
@ -817,42 +836,74 @@ class EntityManager
|
||||||
Entity* new_entity = cast(Entity*) start;
|
Entity* new_entity = cast(Entity*) start;
|
||||||
new_entity.id = entity.id;
|
new_entity.id = entity.id;
|
||||||
new_entity.updateID();
|
new_entity.updateID();
|
||||||
new_block.entities_count++;
|
|
||||||
|
|
||||||
//removeEntityNoID(entity, block);
|
//removeEntityNoID(entity, block);
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
k = 0;
|
k = 0;
|
||||||
|
version (LinearLayout)
|
||||||
|
{
|
||||||
|
static if (EntityID.sizeof == 8)
|
||||||
|
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
|
||||||
|
else
|
||||||
|
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) / EntityID.sizeof());
|
||||||
foreach (ref id; ids)
|
foreach (ref id; ids)
|
||||||
{
|
{
|
||||||
|
void* dst = cast(void*) new_block + new_info.deltas[id] + (
|
||||||
|
new_block.entities_count + new_block.added_count) * components[id].size;
|
||||||
|
uint size = components[id].size;
|
||||||
if (k >= new_ids.length)
|
if (k >= new_ids.length)
|
||||||
{
|
{
|
||||||
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
|
||||||
cast(void*) entity + info.deltas[info.components[j]],
|
|
||||||
components[info.components[j]].size); //id = info.components[j++];
|
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
else if (j >= info.components.length)
|
else if (j >= info.components.length)
|
||||||
{
|
{
|
||||||
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
memcpy(dst, data_pointers[k], size);
|
||||||
data_pointers[k], components[new_ids[k]].size); //id = new_ids[k++];
|
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
else if (id == new_ids[k])
|
else if (id == new_ids[k])
|
||||||
{
|
{
|
||||||
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
memcpy(dst, data_pointers[k], size);
|
||||||
data_pointers[k], components[new_ids[k]].size); //id = new_ids[k++];
|
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memcpy(cast(void*) new_entity + new_info.deltas[id],
|
memcpy(dst, cast(void*) block + info.deltas[id] + ind * size, size);
|
||||||
cast(void*) entity + info.deltas[info.components[j]],
|
|
||||||
components[info.components[j]].size); //id = info.components[j++];
|
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (ref id; ids)
|
||||||
|
{
|
||||||
|
void* dst = cast(void*) new_entity + new_info.deltas[id];
|
||||||
|
uint size = components[id].size;
|
||||||
|
if (k >= new_ids.length)
|
||||||
|
{
|
||||||
|
memcpy(dst, cast(void*) entity + info.deltas[id], size);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
else if (j >= info.components.length)
|
||||||
|
{
|
||||||
|
memcpy(dst, data_pointers[k], size);
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
else if (id == new_ids[k])
|
||||||
|
{
|
||||||
|
memcpy(dst, data_pointers[k], size);
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memcpy(dst, cast(void*) entity + info.deltas[id], size);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
new_block.entities_count++;
|
||||||
removeEntityNoID(entity, block);
|
removeEntityNoID(entity, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,10 +79,10 @@ int main()
|
||||||
__gshared ushort component_id;
|
__gshared ushort component_id;
|
||||||
uint gg = 7; //good game
|
uint gg = 7; //good game
|
||||||
uint bg = 8; //bad game
|
uint bg = 8; //bad game
|
||||||
ulong a;
|
ulong a = 9;
|
||||||
ulong b;
|
ulong b = 10;
|
||||||
ulong c;
|
ulong c = 11;
|
||||||
ulong g;
|
ulong g = 12;
|
||||||
|
|
||||||
static void serializeComponent(ref TestComp comp, SerializeVector output)
|
static void serializeComponent(ref TestComp comp, SerializeVector output)
|
||||||
{
|
{
|
||||||
|
|
@ -260,10 +260,12 @@ int main()
|
||||||
|
|
||||||
time = MonoTime.currTime;
|
time = MonoTime.currTime;
|
||||||
|
|
||||||
ushort[3] ids = [TestComp2.component_id, TestComp.component_id, TestComp4.component_id];
|
//ushort[3] ids = [TestComp2.component_id, TestComp.component_id, TestComp4.component_id];
|
||||||
|
ushort[2] ids = [TestComp2.component_id, TestComp.component_id];
|
||||||
EntityTemplate* tmpl = gEM.allocateTemplate(ids);
|
EntityTemplate* tmpl = gEM.allocateTemplate(ids);
|
||||||
|
|
||||||
ushort[3] ids2 = [TestComp3.component_id, TestComp.component_id, TestComp4.component_id];
|
//ushort[3] ids2 = [TestComp3.component_id, TestComp.component_id, TestComp4.component_id];
|
||||||
|
ushort[2] ids2 = [TestComp3.component_id, TestComp.component_id];
|
||||||
EntityTemplate* tmpl2 = gEM.allocateTemplate(ids2);
|
EntityTemplate* tmpl2 = gEM.allocateTemplate(ids2);
|
||||||
//writeln(tmpl.info.components[]);
|
//writeln(tmpl.info.components[]);
|
||||||
//*cast(EntityID*) tmpl.entity_data.ptr = EntityID(1, 1);
|
//*cast(EntityID*) tmpl.entity_data.ptr = EntityID(1, 1);
|
||||||
|
|
@ -286,7 +288,7 @@ int main()
|
||||||
|
|
||||||
EntityID[1000] idss;
|
EntityID[1000] idss;
|
||||||
|
|
||||||
/*foreach (i; 0 .. 1_000)
|
foreach (i; 0 .. 1_000)
|
||||||
{
|
{
|
||||||
gEM.begin();
|
gEM.begin();
|
||||||
foreach (j; 0 .. 1_000)
|
foreach (j; 0 .. 1_000)
|
||||||
|
|
@ -294,7 +296,7 @@ int main()
|
||||||
foreach (j; 0 .. 1_000)
|
foreach (j; 0 .. 1_000)
|
||||||
gEM.removeEntity(idss[j]);
|
gEM.removeEntity(idss[j]);
|
||||||
gEM.end();
|
gEM.end();
|
||||||
}*/
|
}
|
||||||
|
|
||||||
dur = (MonoTime.currTime - time).total!"usecs";
|
dur = (MonoTime.currTime - time).total!"usecs";
|
||||||
writeln("Entities adding: ", dur, " usecs");
|
writeln("Entities adding: ", dur, " usecs");
|
||||||
|
|
@ -375,6 +377,7 @@ int main()
|
||||||
writeEntityComponents(gEM.getEntity(entity.id));
|
writeEntityComponents(gEM.getEntity(entity.id));
|
||||||
|
|
||||||
gEM.removeComponents!(TestComp)(entity.id);
|
gEM.removeComponents!(TestComp)(entity.id);
|
||||||
|
gEM.addComponents(entity.id, TestComp());
|
||||||
|
|
||||||
gEM.begin();
|
gEM.begin();
|
||||||
gEM.update();
|
gEM.update();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue