bubel-ecs/source/ecs/entity.d
Mergul a82ca1e659 -some documentation changes
-added Component onCreate callback which is called after allocating entity from template
2019-03-21 13:19:03 +01:00

71 lines
2.5 KiB
D

/************************************************************************************************************************
*Entity module.
*/
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() nothrow @nogc
{
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)() nothrow @nogc
{
if(T.component_id >= info.tmpl_deltas.length)return null;
return cast(T*)(entity_data.ptr + info.tmpl_deltas[T.component_id]);
}
}