-some documentation changes

-added Component onCreate callback which is called after allocating entity from template
This commit is contained in:
Mergul 2019-03-21 13:19:03 +01:00
parent b18440e9bc
commit a82ca1e659
4 changed files with 62 additions and 17 deletions

View file

@ -1,3 +1,6 @@
/************************************************************************************************************************
*Entity module.
*/
module ecs.entity;
import ecs.manager;

View file

@ -1,3 +1,6 @@
/************************************************************************************************************************
*Most important module.
*/
module ecs.manager;
import std.algorithm : max;
@ -24,6 +27,9 @@ alias gEntityManager = EntityManager.instance;
alias gEventManager = EntityManager.instance;
alias SerializeVector = ecs.vector.Vector!ubyte;
/************************************************************************************************************************
*Entity manager is responsible for everything.
*/
class EntityManager
{
export static void initialize(uint threads_count)
@ -189,8 +195,9 @@ class EntityManager
*Systems can be registered from external dynamic library, and can be registered after adding entities too.
*System mustn't be registered before components which system want to use, in this case functions call assertion.
*
*params:
*priority = system priority. Priority determines order of execution of systems updates.
*Params:
*priority = system priority. Priority determines order of execution of systems updates
*pass = index of UpdatePass which sholud call system update
*/
void registerSystem(Sys)(int priority, ushort pass = 0)
{
@ -730,11 +737,17 @@ class EntityManager
Sys.system_id = system.id;
}
/************************************************************************************************************************
*Return system ECS api by id
*/
System* getSystem(ushort id) nothrow @nogc
{
return &systems[id];
}
/************************************************************************************************************************
*Return pointer to system registered in manager
*/
Sys* getSystem(Sys)() nothrow @nogc
{
return cast(Sys*) systems[Sys.system_id].m_system_pointer;
@ -766,7 +779,6 @@ class EntityManager
&& Parameters!(Comp.onDestroy).length == 0)
{
static void callDestroy(void* pointer) nothrow @nogc
{
(cast(void delegate() nothrow @nogc)&(cast(Comp*) pointer).onDestroy)();
}
@ -774,6 +786,18 @@ class EntityManager
info.destroy_callback = &callDestroy;
}
static if (hasMember!(Comp, "onCreate") && isFunction!(Comp.onCreate)
&& is(ReturnType!(Comp.onCreate) == void)
&& Parameters!(Comp.onCreate).length == 0)
{
static void callCreate(void* pointer) nothrow @nogc
{
(cast(void delegate() nothrow @nogc)&(cast(Comp*) pointer).onCreate)();
}
info.create_callback = &callCreate;
}
info.size = Comp.sizeof;
info.alignment = Comp.alignof; //8;
info.init_data = Mallocator.instance.makeArray!ubyte(Comp.sizeof);
@ -1025,7 +1049,7 @@ class EntityManager
/************************************************************************************************************************
*Allocate EntityTemplate with specifed components and returns pointer to it.
*
*params:
*Params:
*components_ids = array of components allocated with template
*/
export EntityTemplate* allocateTemplate(ushort[] components_ids)
@ -1069,7 +1093,7 @@ class EntityManager
/************************************************************************************************************************
*Returns entity type info.
*
*params:
*Params:
*ids = array of components
*/
export EntityInfo* getEntityInfo(ushort[] ids)
@ -1174,7 +1198,7 @@ class EntityManager
/************************************************************************************************************************
*Returns pointer to entity.
*
*params:
*Params:
*id = ID of entity
*/
export Entity* getEntity(EntityID id) nothrow @nogc
@ -1185,7 +1209,7 @@ class EntityManager
/************************************************************************************************************************
*Remove components from entity by IDs. Components will be removed on end of frame.
*
*params:
*Params:
*entity_id = ID of entity
*del_ids = array of components IDs
*/
@ -1262,8 +1286,8 @@ class EntityManager
/************************************************************************************************************************
*Remove coponents from entity.
*
*params:
*Compoenents = components types to remove
*Params:
*Components = components types to remove
*entity_id = ID of entity
*/
void removeComponents(Components...)(EntityID entity_id)
@ -1411,9 +1435,9 @@ class EntityManager
/************************************************************************************************************************
*Add components to entity. Components will be added on end of frame.
*
*params:
*Params:
*entity_id = ID of entity to remove
*tmpl = pointer entity template allocated by EntityManager.
*comps = components to add
*/
void addComponents(Components...)(const EntityID entity_id, Components comps) nothrow @nogc
{
@ -1452,8 +1476,8 @@ class EntityManager
/************************************************************************************************************************
*Free template memory.
*
*params:
*tmpl = pointer entity template allocated by EntityManager.
*Params:
*template_ = pointer entity template allocated by EntityManager.
*/
export void freeTemplate(EntityTemplate* template_)
{
@ -1464,7 +1488,7 @@ class EntityManager
/************************************************************************************************************************
*Add entity to system.
*
*params:
*Params:
*tmpl = pointer entity template allocated by EntityManager.
*/
export ref Entity addEntity(EntityTemplate* tmpl)
@ -1489,6 +1513,13 @@ class EntityManager
{
memcpy(cast(void*) block + info.deltas[comp] + components[comp].size * id,
tmpl.entity_data.ptr + info.tmpl_deltas[comp], components[comp].size);
if (components[comp].create_callback)
{
components[comp].create_callback(cast(
void*) block + info.deltas[comp] + id * components[comp].size);
}
}
if (index == 1)
@ -1578,7 +1609,7 @@ class EntityManager
/************************************************************************************************************************
*Remove entity by ID. Entity will be removed on frame end.
*
*params:
*Params:
*id = id of entity to remove
*/
export void removeEntity(EntityID id)
@ -1660,7 +1691,7 @@ class EntityManager
/************************************************************************************************************************
*functions return MetaData of page.
*
*params:
*Params:
*pointer = pointer to any data of entity (i.e. component data pointer)
*/
export EntitiesBlock* getMetaData(const void* pointer) nothrow @nogc
@ -1983,6 +2014,8 @@ class EntityManager
ubyte[] init_data;
///Pointer to component destroy callback
void function(void* pointer) nothrow @nogc destroy_callback;
///Pointer to component create callback
void function(void* pointer) nothrow @nogc create_callback;
}
struct EventCaller

View file

@ -1,3 +1,6 @@
/************************************************************************************************************************
*System module.
*/
module ecs.system;
import ecs.entity;
@ -5,6 +8,13 @@ import ecs.manager;
/************************************************************************************************************************
*System contain data required to proper glue EntityManager with Systems.
*System callbacks:
*<br/>-void onEnable()
*<br/>-void onDisable();
*<br/>-bool onBegin();
*<br/>-void onEnd();
*<br/>-void onCreate()
*<br/>-void onDestroy();
*/
struct System
{