Merge branch 'master' of https://Mergul@bitbucket.org/mmcomando/ecs.git
This commit is contained in:
commit
86d91bc11c
8 changed files with 444 additions and 65 deletions
|
|
@ -20,6 +20,7 @@ import ecs.block_allocator;
|
|||
import ecs.hash_map;
|
||||
import ecs.id_manager;
|
||||
import ecs.vector;
|
||||
import ecs.simple_vector;
|
||||
import ecs.events;
|
||||
import ecs.traits;
|
||||
|
||||
|
|
@ -36,7 +37,17 @@ export class EntityManager
|
|||
export static void initialize(uint threads_count)
|
||||
{
|
||||
if (instance is null)
|
||||
{
|
||||
instance = Mallocator.instance.make!EntityManager(threads_count);
|
||||
|
||||
with(instance)
|
||||
{
|
||||
UpdatePass* pass = Mallocator.instance.make!UpdatePass;
|
||||
pass.name = Mallocator.instance.makeArray("update");
|
||||
passes.add(pass);
|
||||
passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export static void destroy()
|
||||
|
|
@ -44,19 +55,58 @@ export class EntityManager
|
|||
if (instance is null)
|
||||
return;
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
with(instance)
|
||||
{
|
||||
system.disable();
|
||||
}
|
||||
foreach (ref system; systems)
|
||||
{
|
||||
system.disable();
|
||||
if (system.m_destroy)
|
||||
(cast(void function(void*)) system.m_destroy)(system.m_system_pointer);
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
{
|
||||
if (system.m_destroy)
|
||||
(cast(void function(void*)) system.m_destroy)(system.m_system_pointer);
|
||||
if(system.jobs)Mallocator.instance.dispose(system.jobs);
|
||||
if(system.m_read_only_components)Mallocator.instance.dispose(system.m_read_only_components);
|
||||
if(system.m_modified_components)Mallocator.instance.dispose(system.m_modified_components);
|
||||
if(system.m_components)Mallocator.instance.dispose(system.m_components);
|
||||
if(system.m_excluded_components)Mallocator.instance.dispose(system.m_excluded_components);
|
||||
if(system.m_optional_components)Mallocator.instance.dispose(system.m_optional_components);
|
||||
if(system.name)Mallocator.instance.dispose(system.name);
|
||||
if(system.m_event_callers)Mallocator.instance.dispose(system.m_event_callers);
|
||||
|
||||
if(system.m_system_pointer)Mallocator.instance.dispose(system.m_system_pointer);
|
||||
}
|
||||
|
||||
foreach(EntityInfo* info;&entities_infos.byValue)
|
||||
{
|
||||
//if(info.components)Mallocator.instance.dispose(info.components);
|
||||
|
||||
Mallocator.instance.dispose(info);
|
||||
}
|
||||
|
||||
foreach(UpdatePass* pass; passes)
|
||||
{
|
||||
Mallocator.instance.dispose(pass);
|
||||
}
|
||||
passes.clear();
|
||||
|
||||
foreach(ComponentInfo info; components)
|
||||
{
|
||||
if(info.init_data)Mallocator.instance.dispose(info.init_data);
|
||||
}
|
||||
|
||||
foreach(EventInfo info; events)
|
||||
{
|
||||
if(info.callers)Mallocator.instance.dispose(info.callers);
|
||||
}
|
||||
|
||||
foreach(name; &components_map.byKey)
|
||||
{
|
||||
if(name)Mallocator.instance.dispose(name);
|
||||
}
|
||||
}
|
||||
|
||||
Mallocator.instance.dispose(instance);
|
||||
instance = null;
|
||||
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
|
|
@ -198,12 +248,16 @@ export class EntityManager
|
|||
entity_block_alloc_mutex = Mallocator.instance.make!Mutex;
|
||||
//event_manager = EventManager(this);
|
||||
//event_manager.manager = this;
|
||||
}
|
||||
|
||||
UpdatePass* pass = Mallocator.instance.make!UpdatePass;
|
||||
pass.name = Mallocator.instance.makeArray("update");
|
||||
passes.add(pass);
|
||||
~this()
|
||||
{
|
||||
id_manager.deinitialize();
|
||||
|
||||
passes_map.add(cast(string) pass.name, cast(ushort)(passes.length - 1));
|
||||
if(threads)Mallocator.instance.dispose(threads);
|
||||
if(entity_block_alloc_mutex)Mallocator.instance.dispose(entity_block_alloc_mutex);
|
||||
|
||||
allocator.freeMemory();
|
||||
}
|
||||
|
||||
/************************************************************************************************************************
|
||||
|
|
@ -451,7 +505,7 @@ export class EntityManager
|
|||
|
||||
enum ComponentsIndices components_info = getComponentsInfo();
|
||||
|
||||
static void genCompList()(ref System system, ref HashMap!(const(char)[], ushort) components_map)
|
||||
static void genCompList()(ref System system, ref HashMap!(char[], ushort) components_map)
|
||||
{
|
||||
|
||||
foreach (member; __traits(allMembers, Sys.EntitiesData))
|
||||
|
|
@ -661,12 +715,13 @@ export class EntityManager
|
|||
|
||||
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
|
||||
system.m_priority = priority;
|
||||
(cast(Sys*) system.m_system_pointer).__ecsInitialize();
|
||||
system.jobs = (cast(Sys*) system.m_system_pointer)._ecs_jobs;
|
||||
//(cast(Sys*) system.m_system_pointer).__ecsInitialize();
|
||||
//system.jobs = (cast(Sys*) system.m_system_pointer)._ecs_jobs;
|
||||
system.jobs = Mallocator.instance.makeArray!(Job)((cast(Sys*) system.m_system_pointer).__ecs_jobs_count);
|
||||
|
||||
genCompList(system, components_map);
|
||||
|
||||
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
|
||||
ushort sys_id = systems_map.get(cast(char[])Sys.stringof, ushort.max);
|
||||
if (sys_id < systems.length)
|
||||
{
|
||||
system.enable();
|
||||
|
|
@ -761,7 +816,7 @@ export class EntityManager
|
|||
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
|
||||
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
|
||||
|
||||
ushort comp_id = components_map.get(Comp.stringof, ushort.max);
|
||||
ushort comp_id = components_map.get(cast(char[])Comp.stringof, ushort.max);
|
||||
if (comp_id < components.length)
|
||||
{
|
||||
Comp.component_id = comp_id;
|
||||
|
|
@ -771,7 +826,7 @@ export class EntityManager
|
|||
{
|
||||
components.add(info);
|
||||
Comp.component_id = cast(ushort)(components.length - 1);
|
||||
const(char)[] name = Mallocator.instance.makeArray(Comp.stringof);
|
||||
char[] name = Mallocator.instance.makeArray(Comp.stringof);
|
||||
components_map.add(name, cast(ushort)(components.length - 1));
|
||||
}
|
||||
}
|
||||
|
|
@ -1331,9 +1386,9 @@ export class EntityManager
|
|||
ThreadData* data = &threads[thread_id];
|
||||
uint num = cast(uint) del_ids.length;
|
||||
data.change_entities_list.add(0);
|
||||
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
|
||||
data.change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
|
||||
data.change_entities_list.add(cast(ubyte[]) del_ids);
|
||||
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. EntityID.sizeof]);
|
||||
data.change_entities_list.add((cast(ubyte*)&num)[0 .. uint.sizeof]);
|
||||
data.change_entities_list.add((cast(ubyte*)del_ids.ptr)[0 .. num * 2]);
|
||||
}
|
||||
|
||||
private void __removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
|
|
@ -1349,7 +1404,9 @@ export class EntityManager
|
|||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length)))[0
|
||||
.. info.components.length];
|
||||
|
||||
uint j = 0;
|
||||
EntityInfo* new_info = info;
|
||||
|
||||
/*uint j = 0;
|
||||
uint k = 0;
|
||||
foreach (id; info.components)
|
||||
{
|
||||
|
|
@ -1362,15 +1419,23 @@ export class EntityManager
|
|||
else if (del_ids[k] == info.components[j])
|
||||
{
|
||||
k++;
|
||||
new_info = new_info.getNewInfoAdd(del_ids[k]);
|
||||
}
|
||||
else
|
||||
ids[j++] = id;
|
||||
}
|
||||
|
||||
if (j == info.components.length)
|
||||
return;
|
||||
return;*/
|
||||
|
||||
EntityInfo* new_info = getEntityInfo(ids[0 .. j]);
|
||||
foreach(id;del_ids)
|
||||
{
|
||||
new_info = new_info.getNewInfoRemove(id);
|
||||
}
|
||||
|
||||
if(new_info == info)return;
|
||||
|
||||
//EntityInfo* new_info = getEntityInfo(ids[0 .. j]);
|
||||
|
||||
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
|
||||
|
||||
|
|
@ -1459,8 +1524,8 @@ export class EntityManager
|
|||
return;
|
||||
EntitiesBlock* block = getMetaData(entity);
|
||||
EntityInfo* info = block.type_info;
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
|
||||
.. info.components.length + num];
|
||||
/*ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (info.components.length + num)))[0
|
||||
.. info.components.length + num];*/
|
||||
/*ushort[num] new_ids;
|
||||
|
||||
static foreach (i, comp; Components)
|
||||
|
|
@ -1498,7 +1563,9 @@ export class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
uint j = 0;
|
||||
EntityInfo* new_info = info;
|
||||
|
||||
/* uint j = 0;
|
||||
uint k = 0;
|
||||
uint len = 0;
|
||||
//foreach (ref id; ids)
|
||||
|
|
@ -1515,6 +1582,7 @@ export class EntityManager
|
|||
else if (j >= info.components.length)
|
||||
{
|
||||
*id = new_ids[k++];
|
||||
new_info = new_info.getNewInfoAdd(*id);
|
||||
//continue;
|
||||
}
|
||||
else if (new_ids[k] == info.components[j])
|
||||
|
|
@ -1523,18 +1591,26 @@ export class EntityManager
|
|||
k++;
|
||||
}
|
||||
/*debug if (new_ids[k] == info.components[j])
|
||||
assert(0, "Trying to add already existing component!");*/
|
||||
assert(0, "Trying to add already existing component!");//*
|
||||
else if (new_ids[k] < info.components[j])
|
||||
{
|
||||
*id = new_ids[k++];
|
||||
new_info = new_info.getNewInfoAdd(*id);
|
||||
}
|
||||
else
|
||||
*id = info.components[j++];
|
||||
}
|
||||
if (len == info.components.length)
|
||||
return;
|
||||
return;*/
|
||||
|
||||
EntityInfo* new_info = getEntityInfo(ids[0 .. len]);
|
||||
foreach(id;new_ids)
|
||||
{
|
||||
new_info = new_info.getNewInfoAdd(id);
|
||||
}
|
||||
|
||||
if(new_info == info)return;
|
||||
|
||||
//EntityInfo* new_info = getEntityInfo(ids[0 .. len]);
|
||||
|
||||
EntitiesBlock* new_block = findBlockWithFreeSpace(new_info);
|
||||
|
||||
|
|
@ -1544,8 +1620,8 @@ export class EntityManager
|
|||
new_entity.id = entity.id;
|
||||
new_entity.updateID();
|
||||
|
||||
j = 0;
|
||||
k = 0;
|
||||
uint j = 0;
|
||||
uint k = 0;
|
||||
static if (EntityID.sizeof == 8)
|
||||
uint ind = cast(uint)((cast(void*) entity - block.dataBegin()) >> 3);
|
||||
else
|
||||
|
|
@ -1562,7 +1638,7 @@ export class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
foreach (ref id; ids[0 .. len])
|
||||
foreach (ref id; new_info.components)//ids[0 .. len])
|
||||
{
|
||||
void* dst = cast(void*) new_block + new_info.deltas[id] + (
|
||||
new_block.entities_count /*+ new_block.added_count*/ ) * components[id].size;
|
||||
|
|
@ -1646,9 +1722,9 @@ export class EntityManager
|
|||
pointers[i] = ∁
|
||||
}*/
|
||||
ThreadData* data = &threads[thread_id];
|
||||
data.change_entities_list.add(1);
|
||||
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. 8]);
|
||||
data.change_entities_list.add((cast(ubyte*)&num)[0 .. 4]);
|
||||
data.change_entities_list.add(cast(ubyte)1u);
|
||||
data.change_entities_list.add((cast(ubyte*)&entity_id)[0 .. EntityID.sizeof]);
|
||||
data.change_entities_list.add((cast(ubyte*)&num)[0 .. uint.sizeof]);
|
||||
data.change_entities_list.add(cast(ubyte[]) new_ids);
|
||||
static foreach (i, comp; comps)
|
||||
{
|
||||
|
|
@ -1903,6 +1979,7 @@ export class EntityManager
|
|||
{
|
||||
uint index = 0;
|
||||
uint len = cast(uint) thread.change_entities_list.length;
|
||||
void*[32] pointers;// = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
|
||||
while (index < len)
|
||||
{
|
||||
if (!thread.change_entities_list[index++])
|
||||
|
|
@ -1911,8 +1988,8 @@ export class EntityManager
|
|||
index += EntityID.sizeof;
|
||||
uint num = *cast(uint*)&thread.change_entities_list[index];
|
||||
index += uint.sizeof;
|
||||
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
||||
ids[0 .. $] = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
|
||||
ushort[] ids;// = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
||||
ids = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
|
||||
index += ushort.sizeof * num;
|
||||
__removeComponents(id, ids);
|
||||
}
|
||||
|
|
@ -1922,16 +1999,16 @@ export class EntityManager
|
|||
index += EntityID.sizeof;
|
||||
uint num = *cast(uint*)&thread.change_entities_list[index];
|
||||
index += uint.sizeof;
|
||||
ushort[] ids = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
||||
ids[0 .. $] = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
|
||||
ushort[] ids;// = (cast(ushort*) alloca(num * ushort.sizeof))[0 .. num];
|
||||
ids = (cast(ushort*)&thread.change_entities_list[index])[0 .. num];
|
||||
index += ushort.sizeof * num;
|
||||
void*[] pointers = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
|
||||
//void*[] pointers = (cast(void**) alloca(num * (void*).sizeof))[0 .. num];
|
||||
foreach (i; 0 .. num)
|
||||
{
|
||||
pointers[i] = &thread.change_entities_list[index];
|
||||
index += components[ids[i]].size;
|
||||
}
|
||||
__addComponents(id, ids, pointers);
|
||||
__addComponents(id, ids, pointers[0..num]);
|
||||
}
|
||||
}
|
||||
thread.change_entities_list.clear();
|
||||
|
|
@ -2361,6 +2438,93 @@ export class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
EntityInfo* getNewInfoAdd(ushort id)
|
||||
{
|
||||
if(comp_add_info.length < id)
|
||||
{
|
||||
EntityInfo*[] new_infos = Mallocator.instance.makeArray!(EntityInfo*)(instance.components.length);
|
||||
if(comp_add_info !is null)
|
||||
{
|
||||
new_infos[0 .. comp_add_info.length] = comp_add_info[0 .. $];
|
||||
Mallocator.instance.dispose(comp_add_info);
|
||||
}
|
||||
comp_add_info = new_infos;
|
||||
}
|
||||
if(comp_add_info[id])return comp_add_info[id];
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (components.length + 1)))[0
|
||||
.. components.length + 1];
|
||||
uint len = 0;
|
||||
|
||||
foreach(comp; components)
|
||||
{
|
||||
if(id > comp)
|
||||
{
|
||||
ids[len++] = comp;
|
||||
}
|
||||
else if(id == comp)return &this;
|
||||
else
|
||||
{
|
||||
ids[len++] = id;
|
||||
ids[len++] = comp;
|
||||
}
|
||||
}
|
||||
if(id > components[$ - 1])ids[len++] = id;
|
||||
|
||||
assert(len == components.length + 1);
|
||||
|
||||
EntityInfo* new_info = instance.getEntityInfo(ids[0 .. len]);
|
||||
|
||||
comp_add_info[id] = new_info;
|
||||
return new_info;
|
||||
}
|
||||
|
||||
EntityInfo* getNewInfoRemove(ushort id)
|
||||
{
|
||||
if(comp_rem_info.length < id)
|
||||
{
|
||||
EntityInfo*[] new_infos = Mallocator.instance.makeArray!(EntityInfo*)(instance.components.length);
|
||||
if(comp_rem_info !is null)
|
||||
{
|
||||
new_infos[0 .. comp_rem_info.length] = comp_rem_info[0 .. $];
|
||||
Mallocator.instance.dispose(comp_rem_info);
|
||||
}
|
||||
comp_rem_info = new_infos;
|
||||
}
|
||||
if(comp_rem_info[id])return comp_rem_info[id];
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * (components.length - 1)))[0
|
||||
.. components.length - 1];
|
||||
uint len = 0;
|
||||
|
||||
foreach(comp; components)
|
||||
{
|
||||
if(id != comp)
|
||||
{
|
||||
ids[len++] = comp;
|
||||
}
|
||||
}
|
||||
if(len == components.length)return &this;
|
||||
|
||||
assert(len == components.length - 1);
|
||||
|
||||
EntityInfo* new_info = instance.getEntityInfo(ids[0 .. len]);
|
||||
|
||||
comp_rem_info[id] = new_info;
|
||||
return new_info;
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
if(components)Mallocator.instance.dispose(components);
|
||||
if(deltas)Mallocator.instance.dispose(deltas);
|
||||
if(tmpl_deltas)Mallocator.instance.dispose(tmpl_deltas);
|
||||
if(systems)Mallocator.instance.dispose(systems);
|
||||
if(add_listeners)Mallocator.instance.dispose(add_listeners);
|
||||
if(remove_listeners)Mallocator.instance.dispose(remove_listeners);
|
||||
if(change_listeners)Mallocator.instance.dispose(change_listeners);
|
||||
}
|
||||
|
||||
///entity components
|
||||
ushort[] components;
|
||||
|
||||
|
|
@ -2369,6 +2533,11 @@ export class EntityManager
|
|||
///deltas in memory for components in EntityTemplate
|
||||
ushort[] tmpl_deltas;
|
||||
|
||||
///cached new infos after adding component
|
||||
EntityInfo*[] comp_add_info;
|
||||
///cached new infos after removing component
|
||||
EntityInfo*[] comp_rem_info;
|
||||
|
||||
///alignment of whole entity
|
||||
ushort alignment; //unused in linear-layout
|
||||
///size of entity (with alignment respect)
|
||||
|
|
@ -2497,6 +2666,9 @@ export class EntityManager
|
|||
if (dependencies)
|
||||
{
|
||||
Mallocator.instance.dispose(dependencies);
|
||||
}
|
||||
if(exclusion)
|
||||
{
|
||||
Mallocator.instance.dispose(exclusion);
|
||||
}
|
||||
if (job_group.dependencies)
|
||||
|
|
@ -2514,7 +2686,8 @@ export class EntityManager
|
|||
struct ThreadData
|
||||
{
|
||||
Vector!EntityID entities_to_remove;
|
||||
Vector!ubyte change_entities_list;
|
||||
//Vector!ubyte change_entities_list;
|
||||
SimpleVector change_entities_list;
|
||||
Vector!(EntitiesBlock*) blocks_to_update;
|
||||
}
|
||||
|
||||
|
|
@ -2525,6 +2698,11 @@ export class EntityManager
|
|||
assert(name);
|
||||
if (name)
|
||||
Mallocator.instance.dispose(name);
|
||||
foreach(caller; system_callers)
|
||||
{
|
||||
Mallocator.instance.dispose(caller);
|
||||
}
|
||||
system_callers.clear();
|
||||
}
|
||||
|
||||
char[] name;
|
||||
|
|
@ -2555,8 +2733,8 @@ export class EntityManager
|
|||
uint delegate() m_thread_id_func;
|
||||
|
||||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||
HashMap!(const(char)[], ushort) systems_map;
|
||||
HashMap!(const(char)[], ushort) components_map;
|
||||
HashMap!(char[], ushort) systems_map;
|
||||
HashMap!(char[], ushort) components_map;
|
||||
HashMap!(const(char)[], ushort) events_map;
|
||||
HashMap!(const(char)[], ushort) passes_map;
|
||||
Vector!System systems;
|
||||
|
|
@ -2575,6 +2753,15 @@ export class EntityManager
|
|||
uint allocated = 0;
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
foreach(block;blocks)
|
||||
{
|
||||
Mallocator.instance.dispose(block);
|
||||
}
|
||||
blocks.clear();
|
||||
}
|
||||
|
||||
Vector!(Block*) blocks;
|
||||
uint id;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue