From a82ca1e659a9f8a7ccab86a29384fcf9ca2c9a3d Mon Sep 17 00:00:00 2001 From: Mergul Date: Thu, 21 Mar 2019 13:19:03 +0100 Subject: [PATCH] -some documentation changes -added Component onCreate callback which is called after allocating entity from template --- .gitignore | 1 - source/ecs/entity.d | 3 ++ source/ecs/manager.d | 65 +++++++++++++++++++++++++++++++++----------- source/ecs/system.d | 10 +++++++ 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 34d64bd..3a4254f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ !*/ !source/** !tests/** - !README.md !dub.json !.gitignore \ No newline at end of file diff --git a/source/ecs/entity.d b/source/ecs/entity.d index 3757d80..931dfa0 100644 --- a/source/ecs/entity.d +++ b/source/ecs/entity.d @@ -1,3 +1,6 @@ +/************************************************************************************************************************ +*Entity module. +*/ module ecs.entity; import ecs.manager; diff --git a/source/ecs/manager.d b/source/ecs/manager.d index aa8c20a..87491ea 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -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 diff --git a/source/ecs/system.d b/source/ecs/system.d index d21e75a..ba2168e 100644 --- a/source/ecs/system.d +++ b/source/ecs/system.d @@ -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: +*
-void onEnable() +*
-void onDisable(); +*
-bool onBegin(); +*
-void onEnd(); +*
-void onCreate() +*
-void onDestroy(); */ struct System {