From ebffd489588cc5ead162740ae4c4be21d88396ad Mon Sep 17 00:00:00 2001 From: En Yi Date: Tue, 18 Jul 2023 21:49:43 +0800 Subject: [PATCH] Ready a water filler/runner entity Changelog: - Add new component: water runner component - specific for such an entity. Not general, but i dont really care - Implement adding and freeing the water filler entity - Component involves dynamic memory allocation, so need custom function to add and free - May look into custom allocation for this part in the future. --- scenes/CMakeLists.txt | 1 + scenes/engine/EC/EC.h | 21 +++++++++++++- scenes/engine/EC/mempool.c | 2 ++ scenes/water_flow.c | 58 ++++++++++++++++++++++++++++++++++++++ scenes/water_flow.h | 10 +++++++ water_test.c | 6 +++- 6 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 scenes/water_flow.c create mode 100644 scenes/water_flow.h diff --git a/scenes/CMakeLists.txt b/scenes/CMakeLists.txt index f5e7660..8f6e177 100644 --- a/scenes/CMakeLists.txt +++ b/scenes/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(lib_scenes STATIC assets_loader.c player_ent.c items_ent.c + water_flow.c editor_scene.c menu_scene.c game_systems.c diff --git a/scenes/engine/EC/EC.h b/scenes/engine/EC/EC.h index 9af0d95..67b6372 100644 --- a/scenes/engine/EC/EC.h +++ b/scenes/engine/EC/EC.h @@ -7,7 +7,7 @@ #include "sc/queue/sc_queue.h" #define N_TAGS 8 -#define N_COMPONENTS 12 +#define N_COMPONENTS 13 #define MAX_COMP_POOL_SIZE 1024 typedef struct EntityManager EntityManager_t; typedef struct Entity Entity_t; @@ -25,6 +25,7 @@ typedef enum ComponentEnum { CSPRITE_T, CMOVEABLE_T, CLIFETIMER_T, + CWATERRUNNER_T, } ComponentEnum_t; typedef enum MovementMode { @@ -127,6 +128,24 @@ typedef struct _CLifeTimer_t { uint8_t life_time; } CLifeTimer_t; +typedef struct _BFSTile { + int32_t to; + int32_t from; + bool reachable; +}BFSTile_t; + +typedef struct _BFSTileMap { + BFSTile_t* tilemap; + uint32_t width; + uint32_t height; + uint32_t len; +}BFSTileMap_t; + +typedef struct _CWaterRunner { + int32_t current_tile; + BFSTileMap_t bfs_tilemap; +}CWaterRunner_t; + // Credits to bedroomcoders.co.uk for this typedef struct Sprite { Texture2D* texture; diff --git a/scenes/engine/EC/mempool.c b/scenes/engine/EC/mempool.c index d41c74a..085cd41 100644 --- a/scenes/engine/EC/mempool.c +++ b/scenes/engine/EC/mempool.c @@ -19,6 +19,7 @@ static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE]; static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE]; static CMoveable_t cmoveable_buffer[MAX_COMP_POOL_SIZE]; static CLifeTimer_t clifetimer_buffer[MAX_COMP_POOL_SIZE]; +static CWaterRunner_t cwaterrunner_buffer[4]; typedef struct ULongCircBuffer { unsigned long* buffer; // data buffer @@ -90,6 +91,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] = { {csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}}, {cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}}, {clifetimer_buffer, MAX_COMP_POOL_SIZE, sizeof(CLifeTimer_t), NULL, {0}}, + {cwaterrunner_buffer, 4, sizeof(CWaterRunner_t), NULL, {0}}, }; static MemPool_t ent_mempool = { .buffer = entity_buffer, diff --git a/scenes/water_flow.c b/scenes/water_flow.c new file mode 100644 index 0000000..e6df135 --- /dev/null +++ b/scenes/water_flow.c @@ -0,0 +1,58 @@ +#include "water_flow.h" + + +Entity_t* create_water_runner(EntityManager_t* ent_manager, int32_t width, int32_t height, int32_t start_tile) +{ + Entity_t* p_filler = add_entity(ent_manager, NO_ENT_TAG); + if (p_filler == NULL) return NULL; + CWaterRunner_t* p_crunner = add_component(p_filler, CWATERRUNNER_T); + if (p_crunner == NULL) + { + remove_entity(ent_manager, p_filler->m_id); + return NULL; + } + int32_t total = width * height; + p_crunner->bfs_tilemap.tilemap = calloc(total, sizeof(BFSTile_t)); + if (p_crunner->bfs_tilemap.tilemap == NULL) + { + remove_entity(ent_manager, p_filler->m_id); + return NULL; + } + p_crunner->bfs_tilemap.width = width; + p_crunner->bfs_tilemap.height = height; + p_crunner->bfs_tilemap.len = total; + + p_crunner->current_tile = start_tile; + return p_filler; +} + +void free_water_runner(Entity_t** ent, EntityManager_t* ent_manager) +{ + CWaterRunner_t* p_crunner = get_component(*ent, CWATERRUNNER_T); + free(p_crunner->bfs_tilemap.tilemap); + remove_entity(ent_manager, (*ent)->m_id); + *ent = NULL; +} + + +void update_water_runner_system(Scene_t* scene) +{ + // The core of the water runner is to: + // - Reach the lowest possible point in the tilemap + // - Scanline fill + // A runner is given an amount of movement cost + // Within the movement cost, do the following logic + // Perform a modified DFS to find the lowest point: + // - Solid tiles are not reachable + // - If bottom tile is non-solid, that is the only reachable tile, + // - If bottom tile is filled with water fully, down+left+right are reachable + // - If bottom tile is solid, left+right are reachable + // - If bottom tile is OOB, terminate + // Use a LIFO to deal with this. + // On DFS completion, find the path to the lowest point. Keep track of this + // The DFS should have figured out all reachable tiles, start scanline filling at the lowest point. + // On completion, move up update tile reachability by DFS on the current level. (repeat first step) + // - No need to recheck already reachable tiles + // - If current tile is solid, scan left and right for reachable tile and start from there + // Repeat scanline fill +} diff --git a/scenes/water_flow.h b/scenes/water_flow.h new file mode 100644 index 0000000..39616c4 --- /dev/null +++ b/scenes/water_flow.h @@ -0,0 +1,10 @@ +#ifndef __WATER_FLOW_H +#define __WATER_FLOW_H +#include "scene_impl.h" +#include "ent_impl.h" +Entity_t* create_water_runner(EntityManager_t* ent_manager, int32_t width, int32_t height, int32_t start_tile); +void free_water_runner(Entity_t** ent, EntityManager_t* ent_manager); + +void update_water_runner_system(Scene_t* scene); +#endif // __WATER_FLOW_H + diff --git a/water_test.c b/water_test.c index f80470d..798b8d8 100644 --- a/water_test.c +++ b/water_test.c @@ -3,6 +3,7 @@ #include "raymath.h" #include "scene_impl.h" #include "ent_impl.h" +#include "water_flow.h" #include "game_systems.h" #include "assets_loader.h" #include @@ -306,6 +307,8 @@ int main(void) sc_map_put_64(&scene.scene.action_map, KEY_LEFT, ACTION_LEFT); sc_map_put_64(&scene.scene.action_map, KEY_RIGHT, ACTION_RIGHT); + Entity_t* p_runner = create_water_runner(&scene.scene.ent_manager, DEFAULT_MAP_WIDTH, DEFAULT_MAP_HEIGHT, 0); + while(true) { @@ -345,7 +348,8 @@ int main(void) // This is needed to advance time delta render_scene(&scene.scene); if (WindowShouldClose()) break; - } + } + free_water_runner(&p_runner, &scene.scene.ent_manager); free_scene(&scene.scene); for (size_t i = 0; i < scene.data.tilemap.n_tiles;i++) {