-registering same system multiple times only replace pointer for callback
-added export attributes (required by windows to make DLL library, not work at now due to DMD bugs) -interface D files to import
This commit is contained in:
parent
cb0f56b0fb
commit
d0fcdba6cd
20 changed files with 1322 additions and 105 deletions
|
|
@ -22,12 +22,12 @@ alias SerializeVector = ecs.vector.Vector!ubyte;
|
|||
class EntityManager
|
||||
{
|
||||
|
||||
static void initialize()
|
||||
export static void initialize()
|
||||
{
|
||||
instance = Mallocator.instance.make!EntityManager;
|
||||
}
|
||||
|
||||
static void destroy()
|
||||
export static void destroy()
|
||||
{
|
||||
|
||||
foreach (ref system; instance.systems)
|
||||
|
|
@ -204,16 +204,27 @@ class EntityManager
|
|||
//system.m_components = Mallocator.instance.makeArray!uint(types.length - 1);
|
||||
mixin(genCompList());
|
||||
|
||||
systems.add(system);
|
||||
|
||||
if (system.m_create)
|
||||
system.m_create(system.m_system_pointer);
|
||||
|
||||
systems[$ - 1].enable();
|
||||
|
||||
foreach (info; &entities_infos.byValue)
|
||||
ushort sys_id = systems_map.get(Sys.stringof, ushort.max);
|
||||
if(sys_id < systems.length)
|
||||
{
|
||||
addEntityCaller(*info, cast(uint) systems.length - 1);
|
||||
system.enable();
|
||||
systems[sys_id] = system;
|
||||
}
|
||||
else
|
||||
{
|
||||
systems_map.add(Sys.stringof,cast(ushort)systems.length);
|
||||
|
||||
systems.add(system);
|
||||
|
||||
if (system.m_create)
|
||||
system.m_create(system.m_system_pointer);
|
||||
|
||||
systems[$ - 1].enable();
|
||||
|
||||
foreach (info; &entities_infos.byValue)
|
||||
{
|
||||
addEntityCaller(*info, cast(uint) systems.length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
updateEntityCallers();
|
||||
|
|
@ -246,12 +257,20 @@ class EntityManager
|
|||
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
|
||||
*cast(Comp*) info.init_data.ptr = Comp.init; // = Comp();
|
||||
|
||||
components.add(info);
|
||||
Comp.component_id = cast(ushort)(components.length - 1);
|
||||
components_map.add(Comp.stringof, cast(ushort)(components.length - 1));
|
||||
ushort comp_id = components_map.get(Comp.stringof, ushort.max);
|
||||
if(comp_id < components.length)
|
||||
{
|
||||
Comp.component_id = comp_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
components.add(info);
|
||||
Comp.component_id = cast(ushort)(components.length - 1);
|
||||
components_map.add(Comp.stringof, cast(ushort)(components.length - 1));
|
||||
}
|
||||
}
|
||||
|
||||
void update()
|
||||
export void update()
|
||||
{
|
||||
foreach (info; &entities_infos.byValue)
|
||||
{
|
||||
|
|
@ -280,7 +299,7 @@ class EntityManager
|
|||
return 1;
|
||||
}
|
||||
|
||||
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
export EntityTemplate* allocateTemplate(ushort[] components_ids)
|
||||
{
|
||||
|
||||
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * components_ids.length))[0
|
||||
|
|
@ -318,7 +337,7 @@ class EntityManager
|
|||
return temp;
|
||||
}
|
||||
|
||||
EntityInfo* getEntityInfo(ushort[] ids)
|
||||
export EntityInfo* getEntityInfo(ushort[] ids)
|
||||
{
|
||||
EntityInfo* info = entities_infos.get(ids, null);
|
||||
if (info is null)
|
||||
|
|
@ -354,7 +373,7 @@ class EntityManager
|
|||
return info;
|
||||
}
|
||||
|
||||
void updateEntityCallers()
|
||||
export void updateEntityCallers()
|
||||
{
|
||||
foreach (entity; &entities_infos.byValue)
|
||||
{
|
||||
|
|
@ -365,7 +384,7 @@ class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
void addEntityCaller(ref EntityInfo entity, uint system_id)
|
||||
export void addEntityCaller(ref EntityInfo entity, uint system_id)
|
||||
{
|
||||
System* system = &systems[system_id];
|
||||
CallData call_data = CallData(system_id, system, &entity, null);
|
||||
|
|
@ -412,12 +431,12 @@ class EntityManager
|
|||
entity.callers.add(call_data, index);
|
||||
}
|
||||
|
||||
Entity* getEntity(EntityID id)
|
||||
export Entity* getEntity(EntityID id)
|
||||
{
|
||||
return cast(Entity*) id_manager.getEntityPointer(id);
|
||||
}
|
||||
|
||||
void removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
export void removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
{
|
||||
uint num = cast(uint) del_ids.length;
|
||||
change_entities_list.add(0);
|
||||
|
|
@ -426,7 +445,7 @@ class EntityManager
|
|||
change_entities_list.add(cast(ubyte[]) del_ids);
|
||||
}
|
||||
|
||||
void __removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
private void __removeComponents(EntityID entity_id, ushort[] del_ids)
|
||||
{
|
||||
Entity* entity = id_manager.getEntityPointer(entity_id);
|
||||
EntitiesBlock* block = getMetaData(entity);
|
||||
|
|
@ -493,7 +512,7 @@ class EntityManager
|
|||
removeComponents(entity_id, del_ids);
|
||||
}
|
||||
|
||||
void __addComponents(EntityID entity_id, ushort[] new_ids, void*[] data_pointers)
|
||||
private void __addComponents(EntityID entity_id, ushort[] new_ids, void*[] data_pointers)
|
||||
{
|
||||
uint num = cast(uint) new_ids.length;
|
||||
Entity* entity = id_manager.getEntityPointer(entity_id);
|
||||
|
|
@ -644,13 +663,13 @@ class EntityManager
|
|||
//__addComponents(entity_id, new_ids, pointers);
|
||||
}
|
||||
|
||||
void freeTemplate(EntityTemplate* template_)
|
||||
export void freeTemplate(EntityTemplate* template_)
|
||||
{
|
||||
Mallocator.instance.dispose(template_.entity_data);
|
||||
Mallocator.instance.dispose(template_);
|
||||
}
|
||||
|
||||
ref Entity addEntity(EntityTemplate* tmpl)
|
||||
export ref Entity addEntity(EntityTemplate* tmpl)
|
||||
{
|
||||
EntitiesBlock* block = findBlockWithFreeSpace(tmpl.info);
|
||||
|
||||
|
|
@ -669,7 +688,7 @@ class EntityManager
|
|||
return *entity;
|
||||
}
|
||||
|
||||
EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info)
|
||||
private EntitiesBlock* findBlockWithFreeSpace(EntityInfo* info)
|
||||
{
|
||||
EntitiesBlock* previous_block;
|
||||
EntitiesBlock* block = info.first_with_free_space;
|
||||
|
|
@ -709,12 +728,12 @@ class EntityManager
|
|||
return block;
|
||||
}
|
||||
|
||||
void removeEntity(EntityID id)
|
||||
export void removeEntity(EntityID id)
|
||||
{
|
||||
entities_to_remove.add(id);
|
||||
}
|
||||
|
||||
void __removeEntity(EntityID id)
|
||||
private void __removeEntity(EntityID id)
|
||||
{
|
||||
//get entity and block meta data pointers
|
||||
Entity* entity = id_manager.getEntityPointer(id);
|
||||
|
|
@ -771,12 +790,12 @@ class EntityManager
|
|||
*params:
|
||||
*pointer = pointer to any data of entity (i.e. component data pointer)
|
||||
*/
|
||||
EntitiesBlock* getMetaData(void* pointer)
|
||||
export EntitiesBlock* getMetaData(void* pointer)
|
||||
{
|
||||
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
|
||||
}
|
||||
|
||||
void changeEntites()
|
||||
private void changeEntites()
|
||||
{
|
||||
uint index = 0;
|
||||
uint len = cast(uint) change_entities_list.length;
|
||||
|
|
@ -814,7 +833,7 @@ class EntityManager
|
|||
change_entities_list.clear();
|
||||
}
|
||||
|
||||
void updateBlocks()
|
||||
private void updateBlocks()
|
||||
{
|
||||
foreach (block; blocks_to_update)
|
||||
{
|
||||
|
|
@ -824,7 +843,7 @@ class EntityManager
|
|||
blocks_to_update.clear();
|
||||
}
|
||||
|
||||
void removeEntities()
|
||||
private void removeEntities()
|
||||
{
|
||||
foreach (id; entities_to_remove)
|
||||
{
|
||||
|
|
@ -833,7 +852,7 @@ class EntityManager
|
|||
entities_to_remove.clear();
|
||||
}
|
||||
|
||||
void begin()
|
||||
export void begin()
|
||||
{
|
||||
updateBlocks();
|
||||
changeEntites();
|
||||
|
|
@ -845,7 +864,7 @@ class EntityManager
|
|||
}
|
||||
}
|
||||
|
||||
void end()
|
||||
export void end()
|
||||
{
|
||||
foreach (ref system; instance.systems)
|
||||
{
|
||||
|
|
@ -900,7 +919,7 @@ class EntityManager
|
|||
}
|
||||
|
||||
///return pointer to first element in block
|
||||
void* dataBegin()
|
||||
export void* dataBegin()
|
||||
{
|
||||
ushort dif = EntitiesBlock.sizeof;
|
||||
alignNum(dif, type_data.alignment);
|
||||
|
|
@ -950,12 +969,39 @@ class EntityManager
|
|||
Vector!ubyte change_entities_list;
|
||||
|
||||
HashMap!(ushort[], EntityInfo*) entities_infos;
|
||||
HashMap!(string, ushort) systems_map;
|
||||
HashMap!(string, ushort) components_map;
|
||||
Vector!System systems;
|
||||
Vector!ComponentInfo components;
|
||||
__gshared EntityManager instance;
|
||||
|
||||
}
|
||||
|
||||
version(Windows)
|
||||
extern(Windows) bool DllMain(void* hInstance, uint ulReason, void*) {
|
||||
import core.sys.windows.windows;
|
||||
import core.sys.windows.dll;
|
||||
switch (ulReason)
|
||||
{
|
||||
default: assert(0);
|
||||
case DLL_PROCESS_ATTACH:
|
||||
dll_process_attach( hInstance, true );
|
||||
break;
|
||||
|
||||
case DLL_PROCESS_DETACH:
|
||||
dll_process_detach( hInstance, true );
|
||||
break;
|
||||
|
||||
case DLL_THREAD_ATTACH:
|
||||
dll_thread_attach( true, true );
|
||||
break;
|
||||
|
||||
case DLL_THREAD_DETACH:
|
||||
dll_thread_detach( true, true );
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
static ulong defaultHashFunc(T)(auto ref T t)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue