-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;
}
/************************************************************************************************************************
*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.
*