-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

1
.gitignore vendored
View file

@ -2,7 +2,6 @@
!*/ !*/
!source/** !source/**
!tests/** !tests/**
!README.md !README.md
!dub.json !dub.json
!.gitignore !.gitignore

View file

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

View file

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

View file

@ -1,3 +1,6 @@
/************************************************************************************************************************
*System module.
*/
module ecs.system; module ecs.system;
import ecs.entity; import ecs.entity;
@ -5,6 +8,13 @@ import ecs.manager;
/************************************************************************************************************************ /************************************************************************************************************************
*System contain data required to proper glue EntityManager with Systems. *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 struct System
{ {