Compare commits

...

3 Commits

Author SHA1 Message Date
En Yi 62c74fe545 Add water levels proper 2023-07-22 21:09:12 +08:00
En Yi eed785162e Update scanline fill function
Changelog:
- Fill only a range of reachable tiles instead
- Tweak fill complete check
    - Count tiles filled, if zero: complete
- Allow multiple runner to fill without breaking the game
2023-07-22 20:29:20 +08:00
En Yi cf807be7a2 Remove unnecessary state 2023-07-22 20:07:05 +08:00
3 changed files with 43 additions and 22 deletions

View File

@ -160,6 +160,7 @@ typedef struct _CWaterRunner {
int32_t current_tile;
int32_t target_tile;
int32_t fill_idx;
int16_t fill_range[2];
uint8_t movement_delay;
int16_t counter;
}CWaterRunner_t;

View File

@ -182,9 +182,6 @@ void update_water_runner_system(Scene_t* scene)
p_crunner->bfs_tilemap.tilemap[i].from = -1;
p_crunner->bfs_tilemap.tilemap[i].reachable = false;
}
p_crunner->state = BFS_START;
// Want the fallthough
case BFS_START:
p_crunner->state = LOWEST_POINT_SEARCH;
// Want the fallthough
case LOWEST_POINT_SEARCH:
@ -232,7 +229,7 @@ void update_water_runner_system(Scene_t* scene)
{
if (tilemap.tiles[p_crunner->current_tile].water_level == tilemap.max_water_level)
{
p_crunner->state = FILL_COMPLETE;
p_crunner->state = BFS_RESET;
break;
}
int start_tile =
@ -246,19 +243,29 @@ void update_water_runner_system(Scene_t* scene)
int32_t lowest_tile = p_crunner->current_tile;
runner_BFS(&tilemap, p_crunner, &lowest_tile, p_crunner->current_tile);
p_crunner->counter = 0;
for (int i = 0; i < p_crunner->bfs_tilemap.width; ++i)
{
Tile_t* curr_tile = tilemap.tiles + start_tile + i;
if (
p_crunner->bfs_tilemap.tilemap[start_tile + i].reachable
&& curr_tile->water_level < tilemap.max_water_level
)
{
p_crunner->counter++;
p_crunner->fill_range[0] = i;
break;
}
}
p_crunner->fill_idx = 0;
for (int i = p_crunner->bfs_tilemap.width - 1; i >= 0; --i)
{
if (
p_crunner->bfs_tilemap.tilemap[start_tile + i].reachable
)
{
p_crunner->fill_range[1] = i;
break;
}
}
p_crunner->fill_idx = p_crunner->fill_range[0];
p_crunner->counter = 0;
p_crunner->state = SCANLINE_FILL;
}
break;
@ -272,24 +279,30 @@ void update_water_runner_system(Scene_t* scene)
Tile_t* curr_tile = tilemap.tiles + curr_idx;
if (
p_crunner->bfs_tilemap.tilemap[curr_idx].reachable
&& curr_tile->water_level < tilemap.max_water_level
)
{
curr_tile->water_level++;
if (curr_tile->water_level == tilemap.max_water_level)
if (curr_tile->water_level < tilemap.max_water_level)
{
p_crunner->counter--;
curr_tile->water_level++;
}
if (curr_tile->water_level != tilemap.max_water_level)
{
p_crunner->counter++;
}
}
p_crunner->fill_idx++;
if (p_crunner->fill_idx > p_crunner->fill_range[1])
{
if (p_crunner->counter == 0)
{
p_crunner->state = BFS_RESET;
break;
}
p_crunner->fill_idx++;
p_crunner->fill_idx %= p_crunner->bfs_tilemap.width;
p_crunner->counter = 0;
p_crunner->fill_idx = p_crunner->fill_range[0];
}
}
}
break;

View File

@ -55,11 +55,18 @@ static void level_scene_render_func(Scene_t* scene)
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK);
}
if (tilemap.tiles[i].water_level > 0)
//if (tilemap.tiles[i].water_level > 0)
{
float water_height = tilemap.tiles[i].water_level * 1.0f / tilemap.max_water_level;
// Draw water tile
Color water_colour = ColorAlpha(BLUE, 0.5);
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour);
DrawRectangle(
x,
y + (1.0f - water_height) * TILE_SIZE,
TILE_SIZE,
water_height * TILE_SIZE,
water_colour
);
}
}
@ -355,7 +362,7 @@ int main(void)
scene.data.tilemap.width = DEFAULT_MAP_WIDTH;
scene.data.tilemap.height = DEFAULT_MAP_HEIGHT;
scene.data.tilemap.tile_size = TILE_SIZE;
scene.data.tilemap.max_water_level = 1;
scene.data.tilemap.max_water_level = 4;
scene.data.tilemap.n_tiles = scene.data.tilemap.width * scene.data.tilemap.height;
assert(scene.data.tilemap.n_tiles <= MAX_N_TILES);
scene.data.tilemap.tiles = all_tiles;