diff --git a/source/ecs/entity.d b/source/ecs/entity.d index a079857..ad0ef9b 100644 --- a/source/ecs/entity.d +++ b/source/ecs/entity.d @@ -16,10 +16,22 @@ struct Entity { 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 { ubyte[] entity_data; EntityManager.EntityInfo* info; + + T* getComponent(T)() + { + return cast(T*)(entity_data.ptr + info.deltas[T.component_id]); + } } diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 1089e50..b6071be 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -51,11 +51,15 @@ class EntityManager static string genCompList()() { - string ret; + string ret = "uint comp;"; foreach (i; 1 .. (Parameters!(Sys.update)).length) { - ret ~= "system.m_components[" ~ (i - 1) - .to!string ~ "] = components_map.get(types[" ~ i.to!string ~ "].stringof);\n"; + ret ~= "comp = components_map.get(types[" ~ i.to!string ~ "].stringof, ushort.max);\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; } @@ -131,10 +135,11 @@ class EntityManager static assert(0, "Component should have \"__gshared ushort component_id"); } - ushort size = Comp.sizeof; ComponentInfo info; - info.size = size; + info.size = Comp.sizeof; info.aligment = Comp.alignof; //8; + info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof); + *cast(Comp*)info.init_data.ptr = Comp(); components.add(info); Comp.component_id = cast(ushort)(components.length - 1); @@ -196,6 +201,13 @@ class EntityManager EntityTemplate* temp = Mallocator.instance.make!EntityTemplate; temp.entity_data = Mallocator.instance.makeArray!ubyte(info.size); 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; } @@ -475,6 +487,7 @@ class EntityManager { ushort size; ushort aligment; + ubyte[] init_data; } /************************************************************************************************************************