From 7fe6ad174dd63f6bfca1bd4ba34962b9dee59e01 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 24 Dec 2024 01:36:25 -0500 Subject: [PATCH] Now have a configurable displayable tilemap to do better tiles. --- .vimrc_proj | 2 +- assets/tiles.json | 8 ++++++++ constants.hpp | 1 + map.cpp | 11 ++++++++--- map.hpp | 7 +++++-- systems.cpp | 2 +- tests/tilemap.cpp | 3 +-- tilemap.cpp | 13 +++++++++---- tilemap.hpp | 5 +++-- worldbuilder.cpp | 4 +++- 10 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 assets/tiles.json diff --git a/.vimrc_proj b/.vimrc_proj index d58d328..2b745b4 100644 --- a/.vimrc_proj +++ b/.vimrc_proj @@ -1 +1 @@ -set makeprg=meson\ compile\ -j\ 4\ -C\ . +set makeprg=meson\ compile\ -C\ . diff --git a/assets/tiles.json b/assets/tiles.json new file mode 100644 index 0000000..125a3a8 --- /dev/null +++ b/assets/tiles.json @@ -0,0 +1,8 @@ +{ + "WALL_TILE": "\ua5b8", + "FLOOR_TILE": "\u2849", + "PLAYER_TILE": "\ua66b", + "ENEMY_TILE": "\u1d5c", + "BG_TILE": "█", + "WATER_TILE": "\u224b" +} diff --git a/constants.hpp b/constants.hpp index c99dce1..268e46a 100644 --- a/constants.hpp +++ b/constants.hpp @@ -18,3 +18,4 @@ const int MIN_FONT_SIZE = 20; const int SCREEN_WIDTH = 40; const int SCREEN_HEIGHT = 30; #define FONT_FILE_NAME "./assets/text.otf" +#define TILE_MAP_CONFIG "./assets/tiles.json" diff --git a/map.cpp b/map.cpp index 008fafb..7c3289d 100644 --- a/map.cpp +++ b/map.cpp @@ -13,17 +13,18 @@ using namespace fmt; Map::Map(size_t width, size_t height) : $width(width), $height(height), - $tiles(height, matrix::Row(width, SPACE_VALUE)), + $tiles(width, height), $walls(height, matrix::Row(width, SPACE_VALUE)), $paths(width, height) {} Map::Map(Matrix &walls, Pathing &paths) : + $tiles(matrix::width(walls), matrix::height(walls)), $walls(walls), $paths(paths) { - $width = walls[0].size(); - $height = walls.size(); + $width = matrix::width(walls); + $height = matrix::height(walls); } void Map::make_paths() { @@ -175,3 +176,7 @@ bool Map::INVARIANT() { return true; } + +void Map::load_tiles() { + $tiles.load($walls); +} diff --git a/map.hpp b/map.hpp index c0a1d68..72f560b 100644 --- a/map.hpp +++ b/map.hpp @@ -11,6 +11,7 @@ #include "pathing.hpp" #include "matrix.hpp" #include "constants.hpp" +#include "tilemap.hpp" using lighting::LightSource; @@ -30,7 +31,7 @@ class Map { public: size_t $width; size_t $height; - Matrix $tiles; + TileMap $tiles; Matrix $walls; Pathing $paths; std::vector $rooms; @@ -43,7 +44,7 @@ public: Map(Map &map) = delete; Matrix& paths() { return $paths.paths(); } - Matrix& tiles() { return $tiles; } + TileMap& tiles() { return $tiles; } Matrix& input_map() { return $paths.input(); } Matrix& walls() { return $walls; } size_t width() { return $width; } @@ -68,4 +69,6 @@ public: void dump(int show_x=-1, int show_y=-1); bool INVARIANT(); + + void load_tiles(); }; diff --git a/systems.cpp b/systems.cpp index df177e0..deaa2ad 100644 --- a/systems.cpp +++ b/systems.cpp @@ -188,7 +188,7 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, const Matrix &light for(size_t y = 0; y < end_y; ++y) { for(size_t x = 0; x < end_x; ++x) { - string tile = tiles[start.y+y][start.x+x] == L'#' ? config.WALL_TILE : config.FLOOR_TILE; + const string& tile = tiles.at(start.x+x, start.y+y); int light_value = debug.LIGHT ? 160 : lighting[start.y+y][start.x+x]; if(tile == config.WALL_TILE) { diff --git a/tests/tilemap.cpp b/tests/tilemap.cpp index dfe6be5..7b5018f 100644 --- a/tests/tilemap.cpp +++ b/tests/tilemap.cpp @@ -17,8 +17,7 @@ TEST_CASE("tilemap can load tiles and make a map", "[tilemap]") { WorldBuilder builder(map); builder.generate(); - Config config("./assets/tiles.json"); - TileMap tiles(config, width, height); + TileMap tiles(width, height); auto& walls = map.walls(); tiles.load(walls); tiles.dump(); diff --git a/tilemap.cpp b/tilemap.cpp index 2fc4ca7..5128c74 100644 --- a/tilemap.cpp +++ b/tilemap.cpp @@ -3,16 +3,16 @@ #include "constants.hpp" #include "render.hpp" -TileMap::TileMap(Config& config, size_t width, size_t height) -: $config(config), +TileMap::TileMap(size_t width, size_t height) : + $config("./assets/tiles.json"), $width(width), $height(height), $tile_ids(height, matrix::Row(width, SPACE_VALUE)), $display(height, TileRow(width, "")) { - } + void TileMap::dump(int show_x, int show_y) { SFMLRender::init_terminal(); for(matrix::each_row it{$tile_ids}; it.next();) { @@ -33,13 +33,18 @@ void TileMap::load(matrix::Matrix &walls) { string tile_name = walls[it.y][it.x] == SPACE_VALUE ? "FLOOR_TILE" : "WALL_TILE"; std::wstring tile = $config.wstring(tile_name); - std::string tile_s = $config[tile_name]; + string tile_s = $config[tile_name]; $tile_ids[it.y][it.x] = tile[0]; $display[it.y][it.x] = tile_s; } } + +const string &TileMap::at(size_t x, size_t y) { + return $display[y][x]; +} + bool TileMap::INVARIANT() { dbc::check(matrix::height($tile_ids) == $height, "$tile_ids has wrong height"); dbc::check(matrix::width($tile_ids) == $width, "$tile_ids has wrong width"); diff --git a/tilemap.hpp b/tilemap.hpp index 571f98e..407648e 100644 --- a/tilemap.hpp +++ b/tilemap.hpp @@ -13,13 +13,13 @@ typedef std::vector TileDisplay; class TileMap { public: - Config &$config; + Config $config; size_t $width; size_t $height; matrix::Matrix $tile_ids; TileDisplay $display; - TileMap(Config &config, size_t width, size_t height); + TileMap(size_t width, size_t height); // disable copying TileMap(TileMap &map) = delete; @@ -27,6 +27,7 @@ public: size_t width() { return $width; } size_t height() { return $height; } void load(matrix::Matrix &walls); + const std::string &at(size_t x, size_t y); void dump(int show_x=-1, int show_y=-1); bool INVARIANT(); diff --git a/worldbuilder.cpp b/worldbuilder.cpp index 87ba9cc..518dda8 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -137,8 +137,10 @@ void WorldBuilder::generate() { for(matrix::each_cell it{$map.$walls}; it.next();) { int is_wall = !$map.$walls[it.y][it.x]; $map.$walls[it.y][it.x] = is_wall; - $map.$tiles[it.y][it.x] = is_wall ? L'#' : L'.'; } + + // BUG: this is so weird + $map.load_tiles(); } void WorldBuilder::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {