From 4478a512a3a5b8d5eb49fc186ac9d97c2b4788fd Mon Sep 17 00:00:00 2001 From: En Yi Date: Tue, 23 May 2023 20:37:55 +0800 Subject: [PATCH] Combine Component, Entity + Manager headers It has reached a point where it is much easier to combine these three than to separate them. --- entManager_test.c | 1 - main.c | 1 + scenes/engine/EC/{components.h => EC.h} | 56 ++++++++++++++++++++++--- scenes/engine/EC/entManager.c | 2 +- scenes/engine/EC/entManager.h | 29 ------------- scenes/engine/EC/entity.h | 21 ---------- scenes/engine/EC/mempool.h | 4 +- scenes/engine/assets.h | 2 +- scenes/engine/engine.h | 1 - scenes/ent_impl.h | 1 - scenes/game_systems.c | 1 + 11 files changed, 55 insertions(+), 64 deletions(-) rename scenes/engine/EC/{components.h => EC.h} (64%) delete mode 100644 scenes/engine/EC/entManager.h delete mode 100644 scenes/engine/EC/entity.h diff --git a/entManager_test.c b/entManager_test.c index 7027e85..8f1dcd7 100644 --- a/entManager_test.c +++ b/entManager_test.c @@ -1,5 +1,4 @@ #include "mempool.h" -#include "entManager.h" #include int main(void) diff --git a/main.c b/main.c index 83e4e0b..117f04c 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include "raylib.h" #include "scene_impl.h" +#include "mempool.h" #include #define N_SCENES 3 diff --git a/scenes/engine/EC/components.h b/scenes/engine/EC/EC.h similarity index 64% rename from scenes/engine/EC/components.h rename to scenes/engine/EC/EC.h index 6ea9b7c..f2e36c7 100644 --- a/scenes/engine/EC/components.h +++ b/scenes/engine/EC/EC.h @@ -1,9 +1,16 @@ -#ifndef __COMPONENTS_H -#define __COMPONENTS_H -#include "raylib.h" +#ifndef __ENTITY_H +#define __ENTITY_H #include -#include "entity.h" -// TODO: Look at sc to use macros to auto generate functions +#include +#include "raylib.h" +#include "sc/map/sc_map.h" +#include "sc/queue/sc_queue.h" + +#define N_TAGS 4 +#define N_COMPONENTS 10 +#define MAX_COMP_POOL_SIZE 1024 +typedef struct EntityManager EntityManager_t; +typedef struct Entity Entity_t; typedef enum ComponentEnum { CBBOX_COMP_T, @@ -134,4 +141,41 @@ static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) p_bbox->half_size.x = (unsigned int)(x / 2); p_bbox->half_size.y = (unsigned int)(y / 2); } -#endif // __COMPONENTS_H + +typedef enum EntityTag { + NO_ENT_TAG, + PLAYER_ENT_TAG, + ENEMY_ENT_TAG, + CRATES_ENT_TAG, +} EntityTag_t; + +struct Entity { + unsigned long m_id; + EntityTag_t m_tag; + bool m_alive; + unsigned long components[N_COMPONENTS]; +}; + +struct EntityManager { + // All fields are Read-Only + struct sc_map_64v entities; // ent id : entity + struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}] + struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...] + struct sc_queue_uint to_add; + struct sc_queue_uint to_remove; +}; + +void init_entity_manager(EntityManager_t* p_manager); +void update_entity_manager(EntityManager_t* p_manager); +void clear_entity_manager(EntityManager_t* p_manager); +void free_entity_manager(EntityManager_t* p_manager); + +Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag); +void remove_entity(EntityManager_t* p_manager, unsigned long id); +Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id); + +void* add_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type); +void* get_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type); +void remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type); + +#endif // __ENTITY_H diff --git a/scenes/engine/EC/entManager.c b/scenes/engine/EC/entManager.c index 72a3036..0e30833 100644 --- a/scenes/engine/EC/entManager.c +++ b/scenes/engine/EC/entManager.c @@ -1,4 +1,4 @@ -#include "entManager.h" +#include "mempool.h" void init_entity_manager(EntityManager_t* p_manager) { diff --git a/scenes/engine/EC/entManager.h b/scenes/engine/EC/entManager.h deleted file mode 100644 index c3bdf87..0000000 --- a/scenes/engine/EC/entManager.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __ENTITY_MANAGER_H -#define __ENTITY_MANAGER_H -#include "sc/queue/sc_queue.h" -#include "sc/map/sc_map.h" -#include "mempool.h" // includes entity and components - -typedef struct EntityManager { - // All fields are Read-Only - struct sc_map_64v entities; // ent id : entity - struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}] - struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...] - struct sc_queue_uint to_add; - struct sc_queue_uint to_remove; -} EntityManager_t; - -void init_entity_manager(EntityManager_t* p_manager); -void update_entity_manager(EntityManager_t* p_manager); -void clear_entity_manager(EntityManager_t* p_manager); -void free_entity_manager(EntityManager_t* p_manager); - -Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag); -void remove_entity(EntityManager_t* p_manager, unsigned long id); -Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id); - -void* add_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type); -void* get_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type); -void remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type); - -#endif // __ENTITY_MANAGER_H diff --git a/scenes/engine/EC/entity.h b/scenes/engine/EC/entity.h deleted file mode 100644 index e8356c2..0000000 --- a/scenes/engine/EC/entity.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __ENTITY_H -#define __ENTITY_H -#include - -#define N_TAGS 4 -#define N_COMPONENTS 10 -typedef enum EntityTag { - NO_ENT_TAG, - PLAYER_ENT_TAG, - ENEMY_ENT_TAG, - CRATES_ENT_TAG, -} EntityTag_t; - -typedef struct Entity { - unsigned long m_id; - EntityTag_t m_tag; - bool m_alive; - unsigned long components[N_COMPONENTS]; - -} Entity_t; -#endif // __ENTITY_H diff --git a/scenes/engine/EC/mempool.h b/scenes/engine/EC/mempool.h index 2457e48..4da3e0c 100644 --- a/scenes/engine/EC/mempool.h +++ b/scenes/engine/EC/mempool.h @@ -1,8 +1,6 @@ #ifndef __MEMPOOL_H #define __MEMPOOL_H -#include "entity.h" -#include "components.h" -#define MAX_COMP_POOL_SIZE 1024 +#include "EC.h" void init_memory_pools(void); void free_memory_pools(void); diff --git a/scenes/engine/assets.h b/scenes/engine/assets.h index cab9919..031732f 100644 --- a/scenes/engine/assets.h +++ b/scenes/engine/assets.h @@ -1,7 +1,7 @@ #ifndef __ASSETS_H #define __ASSETS_H #include "sc/map/sc_map.h" -#include "components.h" +#include "EC.h" #include "raylib.h" typedef struct Assets diff --git a/scenes/engine/engine.h b/scenes/engine/engine.h index d9eb952..00b2f60 100644 --- a/scenes/engine/engine.h +++ b/scenes/engine/engine.h @@ -1,6 +1,5 @@ #ifndef __ENGINE_H #define __ENGINE_H -#include "entManager.h" #include "actions.h" #include "sc/array/sc_array.h" #include "assets.h" diff --git a/scenes/ent_impl.h b/scenes/ent_impl.h index aad635a..40674e7 100644 --- a/scenes/ent_impl.h +++ b/scenes/ent_impl.h @@ -1,6 +1,5 @@ #ifndef __ENT_IMPL_H #define __ENT_IMPL_H -#include "entManager.h" #include "assets.h" bool init_player_creation(const char* info_file, Assets_t* assets); diff --git a/scenes/game_systems.c b/scenes/game_systems.c index 8479ce1..493db25 100644 --- a/scenes/game_systems.c +++ b/scenes/game_systems.c @@ -1,5 +1,6 @@ #include "game_systems.h" #include "AABB.h" +#include "EC.h" #include "constants.h" #include