bubel-ecs/source/ecs/entity.d
Mergul c915b1a8c7 -EntityTemplate.getComponent additional data check
-LinearLayout proggress:
 *added alignment to data
 *fixed add/remove component functions
2018-09-26 12:50:51 +02:00

52 lines
1.3 KiB
D

module ecs.entity;
import ecs.manager;
struct EntityID
{
uint id;
uint counter;
}
struct Entity
{
EntityID id;
void updateID()
{
EntityManager.instance.id_manager.update(this);
}
T* getComponent(T)()
{
EntityManager.EntitiesBlock* block = gEM.getMetaData(&this);
EntityManager.EntityInfo* info = block.type_info;
if (T.component_id >= info.deltas.length || info.deltas[T.component_id] == 0)
return null;
version (LinearLayout)
{
static if (EntityID.sizeof == 8)
uint ind = cast(uint)((cast(void*)&this - block.dataBegin()) >> 3);
else
uint ind = cast(uint)((cast(void*)&this - block.dataBegin()) / EntityID.sizeof());
return cast(T*)(cast(void*)block + info.deltas[T.component_id] + ind * T.sizeof);
}
else
{
return cast(T*)(cast(void*)&this + info.deltas[T.component_id]);
}
}
}
export struct EntityTemplate
{
ubyte[] entity_data;
EntityManager.EntityInfo* info;
T* getComponent(T)()
{
if(T.component_id >= info.tmpl_deltas.length)return null;
version(LinearLayout)return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]);
else return cast(T*)(entity_data.ptr + info.deltas[T.component_id]);
}
}