From f6c5fa10fd0326ae4ef102c13b14af6108fa5240 Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 24 Jul 2023 23:59:19 +0800 Subject: [PATCH] Add simple water flowing visual --- scenes/editor_scene.c | 36 +++++++++++++++++++- scenes/engine/collisions.h | 2 +- scenes/water_flow.c | 1 + water_test.c | 69 +++++++++++++++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index e824521..cccdf3b 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -99,6 +99,36 @@ static void level_scene_render_func(Scene_t* scene) tilemap.tiles[i].size.x, tilemap.tiles[i].size.y, RED ); } + if (tilemap.tiles[i].wet) + { +#define SURFACE_THICKNESS 4 + int up = i - tilemap.width; + int bot = i + tilemap.width; + int right = i + 1; + int left = i - 1; + int bot_line = y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP - SURFACE_THICKNESS / 2; + if (up >= 0 && tilemap.tiles[up].wet) + { + DrawLineEx((Vector2){x + TILE_SIZE / 2, y}, (Vector2){x + TILE_SIZE / 2, y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + + + if ( + bot <= tilemap.n_tiles + && tilemap.tiles[bot].water_level < MAX_WATER_LEVEL + && tilemap.tiles[i].water_level == 0 + ) + { + if (i % tilemap.width != 0 && tilemap.tiles[left].wet) + { + DrawLineEx((Vector2){x, bot_line}, (Vector2){x + TILE_SIZE / 2, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + if (right % tilemap.width != 0 && tilemap.tiles[right].wet) + { + DrawLineEx((Vector2){x + TILE_SIZE / 2, bot_line}, (Vector2){x + TILE_SIZE, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + } + } // Draw water tile uint32_t water_height = tilemap.tiles[i].water_level * WATER_BBOX_STEP; @@ -513,7 +543,11 @@ static void toggle_block_system(Scene_t* scene) { case TOGGLE_TILE: new_type = (tilemap.tiles[tile_idx].tile_type == SOLID_TILE)? EMPTY_TILE : SOLID_TILE; - if (new_type == SOLID_TILE) tilemap.tiles[tile_idx].water_level = 0; + if (new_type == SOLID_TILE) + { + tilemap.tiles[tile_idx].water_level = 0; + tilemap.tiles[tile_idx].wet = false; + } break; case TOGGLE_ONEWAY: new_type = (tilemap.tiles[tile_idx].tile_type == ONEWAY_TILE)? EMPTY_TILE : ONEWAY_TILE; diff --git a/scenes/engine/collisions.h b/scenes/engine/collisions.h index 7a1dc72..8bc4465 100644 --- a/scenes/engine/collisions.h +++ b/scenes/engine/collisions.h @@ -20,6 +20,7 @@ typedef struct Tile { Vector2 offset; Vector2 size; bool moveable; + bool wet; }Tile_t; typedef struct TileGrid @@ -28,7 +29,6 @@ typedef struct TileGrid unsigned int height; unsigned int n_tiles; unsigned int tile_size; - //unsigned int max_water_level; Tile_t* tiles; }TileGrid_t; diff --git a/scenes/water_flow.c b/scenes/water_flow.c index e8b435f..25aa5a7 100644 --- a/scenes/water_flow.c +++ b/scenes/water_flow.c @@ -219,6 +219,7 @@ void update_water_runner_system(Scene_t* scene) CTransform_t* p_ct = get_component(ent, CTRANSFORM_COMP_T); p_ct->position.x = (p_crunner->current_tile % tilemap.width) * tilemap.tile_size; p_ct->position.y = (p_crunner->current_tile / tilemap.width) * tilemap.tile_size; + tilemap.tiles[p_crunner->current_tile].wet = true; if (p_crunner->current_tile == p_crunner->target_tile) { p_crunner->state = REACHABILITY_SEARCH; diff --git a/water_test.c b/water_test.c index 62481d9..d7319e6 100644 --- a/water_test.c +++ b/water_test.c @@ -54,6 +54,37 @@ static void level_scene_render_func(Scene_t* scene) { DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK); } + + if (tilemap.tiles[i].wet) + { +#define SURFACE_THICKNESS 4 + int up = i - tilemap.width; + int bot = i + tilemap.width; + int right = i + 1; + int left = i - 1; + int bot_line = y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP - SURFACE_THICKNESS / 2; + if (up >= 0 && tilemap.tiles[up].wet) + { + DrawLineEx((Vector2){x + TILE_SIZE / 2, y}, (Vector2){x + TILE_SIZE / 2, y + TILE_SIZE - tilemap.tiles[i].water_level * WATER_BBOX_STEP}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + + + if ( + bot <= tilemap.n_tiles + && tilemap.tiles[bot].water_level < MAX_WATER_LEVEL + && tilemap.tiles[i].water_level == 0 + ) + { + if (i % tilemap.width != 0 && tilemap.tiles[left].wet) + { + DrawLineEx((Vector2){x, bot_line}, (Vector2){x + TILE_SIZE / 2, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + if (right % tilemap.width != 0 && tilemap.tiles[right].wet) + { + DrawLineEx((Vector2){x + TILE_SIZE / 2, bot_line}, (Vector2){x + TILE_SIZE, bot_line}, SURFACE_THICKNESS, ColorAlpha(BLUE, 0.7)); + } + } + } uint32_t water_height = tilemap.tiles[i].water_level * WATER_BBOX_STEP; // Draw water tile @@ -221,7 +252,11 @@ static void toggle_block_system(Scene_t* scene) { TileType_t new_type = EMPTY_TILE; new_type = (tilemap.tiles[tile_idx].tile_type == SOLID_TILE)? EMPTY_TILE : SOLID_TILE; - if (new_type == SOLID_TILE) tilemap.tiles[tile_idx].water_level = 0; + if (new_type == SOLID_TILE) + { + tilemap.tiles[tile_idx].water_level = 0; + tilemap.tiles[tile_idx].wet = false; + } change_a_tile(&tilemap, tile_idx, new_type); } else @@ -243,7 +278,39 @@ static void toggle_block_system(Scene_t* scene) CWaterRunner_t* p_crunner; sc_map_foreach_value(&scene->ent_manager.component_map[CWATERRUNNER_T], p_crunner) { + //if (tilemap.tiles[tile_idx].solid) + //{ + // int curr_idx = p_crunner->current_tile; + // //Tile_t* curr_tile = tilemap.tiles + p_crunner->current_tile; + // bool blocked = false; + // puts("Retracing"); + // while(curr_idx >= 0) + // { + // printf("%u\n", curr_idx); + // if (curr_idx == tile_idx) + // { + // blocked = true; + // break; + // } + // curr_idx = p_crunner->bfs_tilemap.tilemap[curr_idx].from; + // } + // if (blocked) + // { + // int return_idx = p_crunner->current_tile; + // if (p_crunner->bfs_tilemap.tilemap[curr_idx].from >= 0) + // { + // p_crunner->current_tile = p_crunner->bfs_tilemap.tilemap[curr_idx].from; + // } + // puts("Blocking..."); + // while(curr_idx != return_idx) + // { + // tilemap.tiles[curr_idx].wet = false; + // curr_idx = p_crunner->bfs_tilemap.tilemap[curr_idx].to; + // } + // } + //} p_crunner->state = BFS_RESET; + } } else if (IsMouseButtonReleased(MOUSE_RIGHT_BUTTON))