From d7113d42fe7398b30800d5998ea507b1fd478c41 Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 6 May 2024 22:04:53 +0800 Subject: [PATCH] Add neighbour solid counts This is preparing for the tile sprites rework --- engine/collisions.h | 3 ++ scenes/editor_scene.c | 5 ++++ scenes/scene_systems.c | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/engine/collisions.h b/engine/collisions.h index 189c7a0..4fc3b73 100644 --- a/engine/collisions.h +++ b/engine/collisions.h @@ -17,8 +17,11 @@ typedef enum TileRotation TILE_180ROT }TileRotation_t; +// Some of the fields are game-dependent, may have to think about doing OOP +// to separate out standard fields vs context-dependent fields typedef struct Tile { unsigned int tile_type; + uint8_t connectivity; SolidType_t solid; TileRotation_t rotation; uint8_t def; diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 3440013..34d943d 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -479,6 +479,11 @@ static void render_editor_game_scene(Scene_t* scene) // Draw water tile DrawText(buffer, x, y, 10, BLACK); } + if (tilemap.tiles[i].solid == SOLID) + { + sprintf(buffer, "%u", tilemap.tiles[i].connectivity); + DrawText(buffer, x + tilemap.tile_size / 2, y + tilemap.tile_size / 2, 12, WHITE); + } } } diff --git a/scenes/scene_systems.c b/scenes/scene_systems.c index 3ea4934..b98b8e8 100644 --- a/scenes/scene_systems.c +++ b/scenes/scene_systems.c @@ -223,6 +223,7 @@ void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_ty } tilemap->tiles[tile_idx].rotation = TILE_NOROTATE; + if (new_type == SPIKES) { // Priority: Down, Up, Left, Right @@ -272,4 +273,65 @@ void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_ty ); tilemap->tiles[tile_idx].def = (tilemap->tiles[tile_idx].tile_type == SOLID_TILE) ? 5: 2; + tilemap->tiles[tile_idx].connectivity = 0; + + const uint8_t LEFT_BIT = 0; + const uint8_t UP_BIT = 1; + const uint8_t RIGHT_BIT = 2; + const uint8_t DOWN_BIT = 3; + { + // Priority: Left, Up, Right, Down + if (tile_idx % tilemap->width != 0) + { + tilemap->tiles[tile_idx].connectivity |= (tilemap->tiles[tile_idx - 1].tile_type == SOLID_TILE) ? (1 << LEFT_BIT) : 0; + if (new_type == SOLID_TILE) + { + tilemap->tiles[tile_idx - 1].connectivity |= (1 << RIGHT_BIT); + } + else + { + tilemap->tiles[tile_idx - 1].connectivity &= ~(1 << RIGHT_BIT); + } + } + + if (tile_idx - tilemap->width >= 0) + { + tilemap->tiles[tile_idx].connectivity |= (tilemap->tiles[tile_idx - tilemap->width].tile_type == SOLID_TILE) ? (1 << UP_BIT) : 0; + if (new_type == SOLID_TILE) + { + tilemap->tiles[tile_idx - tilemap->width].connectivity |= (1 << DOWN_BIT); + } + else + { + tilemap->tiles[tile_idx - tilemap->width].connectivity &= ~(1 << DOWN_BIT); + } + } + + if (tile_idx + tilemap->width < tilemap->n_tiles) + { + tilemap->tiles[tile_idx].connectivity |= (tilemap->tiles[tile_idx + tilemap->width].tile_type == SOLID_TILE) ? (1 << DOWN_BIT) : 0; + if (new_type == SOLID_TILE) + { + tilemap->tiles[tile_idx + tilemap->width].connectivity |= (1 << UP_BIT); + } + else + { + tilemap->tiles[tile_idx + tilemap->width].connectivity &= ~(1 << UP_BIT); + } + } + + if ((tile_idx + 1) % tilemap->width != 0) + { + tilemap->tiles[tile_idx].connectivity |= (tilemap->tiles[tile_idx + 1].tile_type == SOLID_TILE) ? (1 << RIGHT_BIT) : 0; + if (new_type == SOLID_TILE) + { + tilemap->tiles[tile_idx + 1].connectivity |= (1 << LEFT_BIT); + } + else + { + tilemap->tiles[tile_idx + 1].connectivity &= ~(1 << LEFT_BIT); + } + } + } + }