-added EntityManager.removeEntity(EntityID) -EntitiesBlock now has ID usefull for updating first_free_block in EntityInfo
367 lines
10 KiB
D
367 lines
10 KiB
D
module ecs.manager;
|
|
|
|
import std.experimental.allocator.mallocator : Mallocator, AlignedMallocator;
|
|
import std.experimental.allocator;
|
|
import std.traits;
|
|
import std.algorithm : max;
|
|
import std.conv : to;
|
|
|
|
import core.stdc.string;
|
|
import core.stdc.stdlib;
|
|
|
|
import ecs.system;
|
|
import ecs.entity;
|
|
import ecs.vector;
|
|
import ecs.hash_map;
|
|
import ecs.id_manager;
|
|
|
|
alias gEM = EntityManager.instance;
|
|
|
|
class EntityManager
|
|
{
|
|
|
|
static void initialize()
|
|
{
|
|
instance = Mallocator.instance.make!EntityManager;
|
|
}
|
|
|
|
static void destory()
|
|
{
|
|
Mallocator.instance.dispose(instance);
|
|
instance = null;
|
|
}
|
|
|
|
void registerSystem(Sys)(int priority)
|
|
{
|
|
alias types = Parameters!(Sys.update);
|
|
|
|
System system;
|
|
|
|
static string genCall()()
|
|
{
|
|
string ret = "s.update(";
|
|
foreach (i, param; Parameters!(Sys.update))
|
|
{
|
|
ret ~= "*cast(types[" ~ i.to!string
|
|
~ "]*)(data_pointer + data.deltas[" ~ i.to!string ~ "]),";
|
|
}
|
|
ret ~= ");";
|
|
return ret;
|
|
}
|
|
|
|
static string genCompList()()
|
|
{
|
|
string ret;
|
|
foreach (i, param; Parameters!(Sys.update))
|
|
{
|
|
ret ~= "system.m_components[" ~ i.to!string
|
|
~ "] = components_map.get(types[" ~ i.to!string ~ "].stringof);\n";
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static if (hasMember!(Sys, "update"))
|
|
{
|
|
static void callUpdate(ref CallData data, void* entity)
|
|
{
|
|
static if (hasMember!(Sys, "update"))
|
|
{
|
|
Sys* s = cast(Sys*) data.system.m_system_pointer;
|
|
|
|
EntitiesBlock* block = data.info.first_block;
|
|
while (block !is null)
|
|
{
|
|
void* data_pointer = block.dataBegin();
|
|
foreach (i; 0 .. block.entities_count)
|
|
{
|
|
mixin(genCall());
|
|
data_pointer += data.info.size;
|
|
}
|
|
block = block.next_block;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
system.m_update = &callUpdate;
|
|
}
|
|
|
|
static if (hasMember!(Sys, "onEnable"))
|
|
{
|
|
static void callEnable(void* system_pointer)
|
|
{
|
|
|
|
Sys* s = cast(Sys*) system_pointer;
|
|
s.onEnable();
|
|
}
|
|
|
|
system.m_enable = &callEnable;
|
|
}
|
|
|
|
static if (hasMember!(Sys, "onDisable"))
|
|
{
|
|
static void callDisable(void* system_pointer)
|
|
{
|
|
|
|
Sys* s = cast(Sys*) system_pointer;
|
|
s.onDisable();
|
|
}
|
|
|
|
system.m_disable = &callDisable;
|
|
}
|
|
|
|
system.m_system_pointer = cast(void*) Mallocator.instance.make!Sys;
|
|
|
|
system.m_components = Mallocator.instance.makeArray!uint(types.length);
|
|
mixin(genCompList());
|
|
|
|
systems.add(system);
|
|
systems[$ - 1].enable();
|
|
|
|
foreach (info; &entities_infos.byValue)
|
|
{
|
|
addEntityCaller(*info, systems[$ - 1]);
|
|
}
|
|
}
|
|
|
|
void registerComponent(Comp)()
|
|
{
|
|
ushort size = Comp.sizeof;
|
|
ComponentInfo info;
|
|
info.size = size;
|
|
info.aligment = Comp.alignof; //8;
|
|
|
|
components.add(info);
|
|
components_map.add(Comp.stringof, cast(uint)(components.length - 1));
|
|
}
|
|
|
|
void update()
|
|
{
|
|
foreach (info; &entities_infos.byValue)
|
|
{
|
|
foreach (data; info.callers)
|
|
{
|
|
if (data.system.enabled)
|
|
(cast(SytemFuncType) data.system.m_update)(data, null); //caller(call_data,null);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void alignNum(ref ushort num, ushort aligment)
|
|
{
|
|
num += aligment - (num & (aligment - 1));
|
|
}
|
|
|
|
EntityTemplate* allocateTemplate(ushort[] components_ids)
|
|
{
|
|
EntityInfo* info = entities_infos.get(components_ids, null);
|
|
if (info is null)
|
|
{
|
|
info = Mallocator.instance.make!EntityInfo;
|
|
|
|
info.components = Mallocator.instance.makeArray(components_ids);
|
|
info.deltas = Mallocator.instance.makeArray!ushort(components_ids.length);
|
|
|
|
info.size = EntityID.sizeof;
|
|
info.alignment = EntityID.alignof;
|
|
|
|
foreach (i, id; components_ids)
|
|
{
|
|
info.alignment = max(info.alignment, components[id].aligment);
|
|
alignNum(info.size, components[id].aligment);
|
|
info.deltas[i] = info.size;
|
|
info.size += components[id].size;
|
|
}
|
|
alignNum(info.size, info.alignment);
|
|
|
|
foreach (ref system; systems)
|
|
{
|
|
if (system.m_update is null)
|
|
continue;
|
|
addEntityCaller(*info, system);
|
|
}
|
|
|
|
entities_infos.add(info.components, info);
|
|
}
|
|
|
|
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
|
|
temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size);
|
|
temp.info = info;
|
|
return temp;
|
|
}
|
|
|
|
void addEntityCaller(ref EntityInfo entity, ref System system)
|
|
{
|
|
CallData call_data = CallData(&system, &entity, null);
|
|
ushort[] deltas = (cast(ushort*) alloca(system.m_components.length * ushort.sizeof))[0
|
|
.. system.m_components.length];
|
|
foreach (i, id; system.m_components)
|
|
{
|
|
deltas[i] = ushort.max;
|
|
foreach (i2, id2; entity.components)
|
|
{
|
|
if (id2 == id)
|
|
{
|
|
deltas[i] = entity.deltas[i2];
|
|
break;
|
|
}
|
|
}
|
|
if (deltas[i] == ushort.max)
|
|
{
|
|
deltas = null;
|
|
break;
|
|
}
|
|
}
|
|
if (deltas)
|
|
{
|
|
call_data.deltas = Mallocator.instance.makeArray(deltas); //Mallocator.instance.makeArray!ushort(system.m_components.length);
|
|
entity.callers.add(call_data);
|
|
}
|
|
}
|
|
|
|
void freeTemplate(EntityTemplate* template_)
|
|
{
|
|
Mallocator.instance.dispose(template_.entity_data);
|
|
Mallocator.instance.dispose(template_);
|
|
}
|
|
|
|
ref Entity addEntity(EntityTemplate* tmpl)
|
|
{
|
|
EntitiesBlock* previous_block;
|
|
EntitiesBlock* block = tmpl.info.first_with_free_space; //tmpl.info.first_block;
|
|
|
|
// find block with enought space
|
|
while (1)
|
|
{
|
|
if (block is null)
|
|
{
|
|
block = cast(EntitiesBlock*) AlignedMallocator.instance.alignedAllocate(page_size,
|
|
page_size);
|
|
*block = EntitiesBlock(tmpl.info);
|
|
if (previous_block is null)
|
|
{
|
|
tmpl.info.first_block = block;
|
|
block.id = 0;
|
|
}
|
|
else
|
|
{
|
|
previous_block.next_block = block;
|
|
block.id = previous_block.id + 1;
|
|
}
|
|
tmpl.info.first_with_free_space = block;
|
|
break; // new block certainly has free space
|
|
}
|
|
// check if there is enought space
|
|
if (block.dataDelta() + (block.entities_count + 1) * tmpl.info.size > page_size)
|
|
{
|
|
previous_block = block;
|
|
block = block.next_block;
|
|
continue;
|
|
}
|
|
|
|
tmpl.info.first_with_free_space = block;
|
|
break; // block exists and bounds check passed
|
|
}
|
|
|
|
void* start = block.dataBegin() + block.entities_count * tmpl.info.size;
|
|
memcpy(start, tmpl.entity_data.ptr, tmpl.info.size);
|
|
Entity* entity = cast(Entity*) start;
|
|
entity.id = id_manager.getNewID();
|
|
entity.updateID();
|
|
block.entities_count++;
|
|
return *entity;
|
|
}
|
|
|
|
void removeEntity(EntityID id)
|
|
{
|
|
Entity* entity = id_manager.getEntityPointer(id);
|
|
if(entity is null)return;
|
|
EntitiesBlock* block = getMetaData(entity);
|
|
id_manager.releaseID(id);
|
|
|
|
void* data_begin = block.dataBegin();
|
|
uint pos = cast(int)(cast(void*)entity - data_begin) / block.type_data.size;
|
|
|
|
block.entities_count--;
|
|
|
|
if(block.type_data.first_with_free_space.id > block.id)block.type_data.first_with_free_space = block;
|
|
|
|
if(pos == block.entities_count)return;
|
|
|
|
void* src = data_begin + block.entities_count * block.type_data.size;
|
|
void* dst = data_begin + pos * block.type_data.size;
|
|
|
|
memcpy(dst,src,block.type_data.size);
|
|
|
|
entity = cast(Entity*) dst;
|
|
entity.updateID();
|
|
}
|
|
|
|
EntitiesBlock* getMetaData(void* pointer)
|
|
{
|
|
return cast(EntitiesBlock*)(cast(size_t) pointer & (~cast(size_t)(page_size - 1)));
|
|
}
|
|
|
|
struct ComponentInfo
|
|
{
|
|
ushort size;
|
|
ushort aligment;
|
|
}
|
|
|
|
struct EntityInfo
|
|
{
|
|
ushort[] components;
|
|
ushort[] deltas;
|
|
ushort alignment;
|
|
ushort size;
|
|
EntitiesBlock* first_block;
|
|
EntitiesBlock* first_with_free_space; // a hint for allocations, should have empty space in it but doesn't have to
|
|
Vector!(CallData) callers;
|
|
}
|
|
|
|
struct EntitiesBlock
|
|
{
|
|
uint dataDelta()
|
|
{
|
|
ushort dif = EntitiesBlock.sizeof;
|
|
alignNum(dif, type_data.alignment);
|
|
return dif;
|
|
}
|
|
|
|
void* dataBegin()
|
|
{
|
|
ushort dif = EntitiesBlock.sizeof;
|
|
alignNum(dif, type_data.alignment);
|
|
return cast(void*)&this + dif;
|
|
}
|
|
|
|
EntityInfo* type_data;
|
|
uint entities_count;
|
|
uint id;
|
|
EntitiesBlock* next_block;
|
|
///there is a loooot of data (4kB, pure magic)
|
|
}
|
|
|
|
struct CallData
|
|
{
|
|
System* system;
|
|
EntityManager.EntityInfo* info;
|
|
ushort[] deltas;
|
|
}
|
|
|
|
alias SytemFuncType = void function(ref EntityManager.CallData data, void* entity);
|
|
|
|
///Single page size. Must be power of two.
|
|
enum page_size = 4096;
|
|
///Number of pages in block.
|
|
enum pages_in_block = 128;
|
|
|
|
IDManager id_manager;
|
|
|
|
HashMap!(ushort[], EntityInfo*) entities_infos;
|
|
HashMap!(string, uint) components_map;
|
|
Vector!System systems;
|
|
Vector!ComponentInfo components;
|
|
__gshared EntityManager instance;
|
|
|
|
}
|