-added function to create EntityTemplate form Entity

This commit is contained in:
Mergul 2019-08-10 16:32:57 +00:00
parent 1be08eb534
commit 9402e917f2
2 changed files with 48 additions and 1 deletions

View file

@ -1073,6 +1073,45 @@ export class EntityManager
return 1; return 1;
} }
/************************************************************************************************************************
*Allocate EntityTemplate with all components from entity witch it's data and returns pointer to it.
*
*Params:
*id = ID of entity from which should be created template
*fill_default = if true, components will be filled with default data, instead entity data will be taken
*/
export EntityTemplate* allocateTemplate(EntityID entity_id, bool fill_default = false)
{
Entity* entity = getEntity(entity_id);
EntitiesBlock* block = getMetaData(entity);
EntityInfo* info = block.type_info;
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size);
temp.info = info;
if(fill_default)
{
//fill components with default data
foreach (comp; info.components)
{
temp.entity_data[info.tmpl_deltas[comp] .. info.tmpl_deltas[comp] + components[comp].size]
= components[comp].init_data;
}
}
else
{
ushort index = block.entityIndex(entity);
foreach (comp; info.components)
{
memcpy(cast(void*) temp.entity_data + info.tmpl_deltas[comp],
cast(void*) block + info.deltas[comp] + components[comp].size * index, components[comp].size);
}
}
return temp;
}
/************************************************************************************************************************ /************************************************************************************************************************
*Allocate EntityTemplate with specifed components and returns pointer to it. *Allocate EntityTemplate with specifed components and returns pointer to it.
* *

View file

@ -674,8 +674,14 @@ int main()
//writeln((cast(uint*) pp)[0 .. 14], " ", pp); //writeln((cast(uint*) pp)[0 .. 14], " ", pp);
writeEntityComponents(gEM.getEntity(entity)); writeEntityComponents(gEM.getEntity(entity));
writeln("Entity, its copy, and template, and default filled tempalte");
gEM.addEntity(tmpl); gEM.addEntity(tmpl);
gEM.addEntityCopy(entity); writeEntityComponents(gEM.getEntity(entity));
writeEntityComponents(gEM.addEntityCopy(entity));
EntityTemplate* copy_tempalte = gEM.allocateTemplate(entity);
writeEntityComponents(gEM.addEntity(copy_tempalte));
EntityTemplate* copy_default_tempalte = gEM.allocateTemplate(entity,true);
writeEntityComponents(gEM.addEntity(copy_default_tempalte));
gEM.addComponents(entity, TestComp4()); gEM.addComponents(entity, TestComp4());
gEM.addComponents(entity, TestComp3()); gEM.addComponents(entity, TestComp3());
@ -704,6 +710,8 @@ int main()
//writeln((cast(uint*)tmpl.info.first_block)[0..48]); //writeln((cast(uint*)tmpl.info.first_block)[0..48]);
gEM.freeTemplate(tmpl); gEM.freeTemplate(tmpl);
gEM.freeTemplate(tmpl2); gEM.freeTemplate(tmpl2);
gEM.freeTemplate(copy_tempalte);
gEM.freeTemplate(copy_default_tempalte);
EntityManager.destroy(); EntityManager.destroy();
return 0; return 0;