From 9402e917f239fbb18fb01dfc9b6a4a7210f6707d Mon Sep 17 00:00:00 2001 From: Mergul Date: Sat, 10 Aug 2019 16:32:57 +0000 Subject: [PATCH] -added function to create EntityTemplate form Entity --- source/ecs/manager.d | 39 +++++++++++++++++++++++++++++++++++++++ tests/tests.d | 10 +++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 6d01d6d..4523e4e 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -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. * diff --git a/tests/tests.d b/tests/tests.d index 1b7e47b..4c41220 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -674,8 +674,14 @@ int main() //writeln((cast(uint*) pp)[0 .. 14], " ", pp); writeEntityComponents(gEM.getEntity(entity)); + writeln("Entity, its copy, and template, and default filled tempalte"); 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, TestComp3()); @@ -704,6 +710,8 @@ int main() //writeln((cast(uint*)tmpl.info.first_block)[0..48]); gEM.freeTemplate(tmpl); gEM.freeTemplate(tmpl2); + gEM.freeTemplate(copy_tempalte); + gEM.freeTemplate(copy_default_tempalte); EntityManager.destroy(); return 0;