bubel-ecs/source/ecs/entity.d
Mergul c18ac54265 -LinearLayout proggress:
*EntityTemplate.getComponent
 *Adding/removing components for Entitites
-better code for adding components
2018-09-26 10:57:42 +02:00

51 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)()
{
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]);
}
}