-getComponent functions for Template and Entity

-error information about adding system with non existing component (prints name of system and component)
-fill components in template with default values
This commit is contained in:
Mergul 2018-09-13 20:53:34 +02:00
parent 4b19907c03
commit 624c899c8c
2 changed files with 30 additions and 5 deletions

View file

@ -16,10 +16,22 @@ struct Entity
{ {
EntityManager.instance.id_manager.update(this); EntityManager.instance.id_manager.update(this);
} }
T* getComponent(T)()
{
EntityManager.EntitiesBlock* block = gEM.getMetaData(&this);
EntityManager.EntityInfo* info = block.type_data;
return cast(T*)(cast(void*)&this + info.deltas[T.component_id]);
}
} }
struct EntityTemplate struct EntityTemplate
{ {
ubyte[] entity_data; ubyte[] entity_data;
EntityManager.EntityInfo* info; EntityManager.EntityInfo* info;
T* getComponent(T)()
{
return cast(T*)(entity_data.ptr + info.deltas[T.component_id]);
}
} }

View file

@ -51,11 +51,15 @@ class EntityManager
static string genCompList()() static string genCompList()()
{ {
string ret; string ret = "uint comp;";
foreach (i; 1 .. (Parameters!(Sys.update)).length) foreach (i; 1 .. (Parameters!(Sys.update)).length)
{ {
ret ~= "system.m_components[" ~ (i - 1) ret ~= "comp = components_map.get(types[" ~ i.to!string ~ "].stringof, ushort.max);\n
.to!string ~ "] = components_map.get(types[" ~ i.to!string ~ "].stringof);\n"; if(comp == ushort.max)assert(0,\"Can't register system \\\"" ~ Sys.stringof
~ "\\\" due to non existing component \\\"\"~types[" ~ i.to!string ~ "].stringof~\"\\\".\");
system.m_components[" ~ (i - 1)
.to!string ~ "] = comp;";
} }
return ret; return ret;
} }
@ -131,10 +135,11 @@ class EntityManager
static assert(0, "Component should have \"__gshared ushort component_id"); static assert(0, "Component should have \"__gshared ushort component_id");
} }
ushort size = Comp.sizeof;
ComponentInfo info; ComponentInfo info;
info.size = size; info.size = Comp.sizeof;
info.aligment = Comp.alignof; //8; info.aligment = Comp.alignof; //8;
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
*cast(Comp*)info.init_data.ptr = Comp();
components.add(info); components.add(info);
Comp.component_id = cast(ushort)(components.length - 1); Comp.component_id = cast(ushort)(components.length - 1);
@ -196,6 +201,13 @@ class EntityManager
EntityTemplate* temp = Mallocator.instance.make!EntityTemplate; EntityTemplate* temp = Mallocator.instance.make!EntityTemplate;
temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size); temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size);
temp.info = info; temp.info = info;
//fill components with default data
foreach(comp;info.components)
{
temp.entity_data[info.deltas[comp]..info.deltas[comp]+components[comp].size] = components[comp].init_data;
}
return temp; return temp;
} }
@ -475,6 +487,7 @@ class EntityManager
{ {
ushort size; ushort size;
ushort aligment; ushort aligment;
ubyte[] init_data;
} }
/************************************************************************************************************************ /************************************************************************************************************************