bubel-ecs/source/ecs/entity.d
2018-10-01 19:40:24 +02:00

68 lines
2.3 KiB
D

module ecs.entity;
import ecs.manager;
/************************************************************************************************************************
*Entity ID structure.
*/
struct EntityID
{
///Index to entity in IDManager.
uint id;
///Counter required for reusing ID.
uint counter;
}
/************************************************************************************************************************
*Structure of Entity. It's have only ID, but have full konwlege to get to components memory (If pointer to it is proper).
*/
struct Entity
{
///Entity ID.
EntityID id;
/************************************************************************************************************************
*Update pointer to it in IDManager
*/
void updateID()
{
EntityManager.instance.id_manager.update(this);
}
/************************************************************************************************************************
*Get specified component. If component doesn't exist function retun null.
*/
T* getComponent(T)() const
{
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;
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);
}
}
/************************************************************************************************************************
*Entity template structure.
*/
export struct EntityTemplate
{
///Entity components data
ubyte[] entity_data;
///Pointer to entity type info.
EntityManager.EntityInfo* info;
/************************************************************************************************************************
*Get specified component. If component doesn't exist function return null.
*/
T* getComponent(T)()
{
if(T.component_id >= info.tmpl_deltas.length)return null;
return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]);
}
}