From d2a5dfa713377457e38d9c6fbe9ef2e215ddded9 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 30 May 2025 20:35:17 -0400 Subject: [PATCH] Rooms are now styled randomly based on assets/styles.json which will evolve into specifications for themes of levels and rooms in them plus other configs. --- assets/styles.json | 17 +++++++++++++++++ assets/tiles.json | 7 +++++++ config.cpp | 4 ++++ config.hpp | 1 + textures.cpp | 7 +++++++ textures.hpp | 4 ++++ worldbuilder.cpp | 18 ++++++++++++++++-- 7 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 assets/styles.json diff --git a/assets/styles.json b/assets/styles.json new file mode 100644 index 0000000..b1822d4 --- /dev/null +++ b/assets/styles.json @@ -0,0 +1,17 @@ +[ + { + "name": "Mossy Blue Ceiling", + "floor": "gray_stone_floor_light", + "walls": "wall_moss" + }, + { + "name": "Plain", + "floor": "floor_tile", + "walls": "wall_plain" + }, + { + "name": "Wood Walls", + "floor": "floor_tile", + "walls": "wood_wall" + } +] diff --git a/assets/tiles.json b/assets/tiles.json index e1d3d53..0728c7c 100644 --- a/assets/tiles.json +++ b/assets/tiles.json @@ -50,5 +50,12 @@ "display": 35, "light": 0, "id": 7 + }, + "wood_wall": { + "texture": "assets/textures/wood_wall.png", + "collision": false, + "display": 35, + "light": 0, + "id": 8 } } diff --git a/config.cpp b/config.cpp index d71fe5f..d549f3a 100644 --- a/config.cpp +++ b/config.cpp @@ -15,6 +15,10 @@ Config::Config(const std::string src_path) : $src_path(src_path) { $config = json::parse(infile); } +nlohmann::json &Config::operator[](size_t key) { + return $config[key]; +} + json &Config::operator[](const std::string &key) { dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key)); return $config[key]; diff --git a/config.hpp b/config.hpp index 2ca8d52..d26f5a2 100644 --- a/config.hpp +++ b/config.hpp @@ -14,6 +14,7 @@ struct Config { Config(nlohmann::json config, std::string src_path) : $config(config), $src_path(src_path) {} + nlohmann::json &operator[](size_t); nlohmann::json &operator[](const std::string &key); nlohmann::json &json() { return $config; }; std::wstring wstring(const std::string main_key, const std::string sub_key); diff --git a/textures.cpp b/textures.cpp index a91d886..aa297d0 100644 --- a/textures.cpp +++ b/textures.cpp @@ -46,6 +46,7 @@ namespace textures { auto &config = el.value(); const std::string& texture_fname = config["texture"]; size_t surface_i = config["id"]; + TMGR.name_to_id.insert_or_assign(el.key(), surface_i); if(surface_i >= tiles.size()) { resize_shit(surface_i + 1); @@ -113,4 +114,10 @@ namespace textures { size_t ceiling_num = TMGR.ceilings[num]; return (const uint32_t *)TMGR.surfaces[ceiling_num].getPixelsPtr(); } + + size_t get_id(const std::string& name) { + dbc::check(TMGR.name_to_id.contains(name), + fmt::format("there is no texture named {} in tiles.json", name)); + return TMGR.name_to_id.at(name); + } }; diff --git a/textures.hpp b/textures.hpp index 56fa46f..39cc456 100644 --- a/textures.hpp +++ b/textures.hpp @@ -20,6 +20,7 @@ namespace textures { std::vector map_tile_set; std::vector ambient_light; std::unordered_map sprite_textures; + std::unordered_map name_to_id; }; void init(); @@ -29,9 +30,12 @@ namespace textures { sf::Image load_image(const std::string& filename); std::vector& get_ambient_light(); + std::vector& get_map_tile_set(); const uint32_t* get_surface(size_t num); const uint32_t* get_ceiling(size_t num); + + size_t get_id(const std::string& name); } diff --git a/worldbuilder.cpp b/worldbuilder.cpp index cdcf5c2..cd300d4 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -12,13 +12,27 @@ using namespace components; void WorldBuilder::stylize_rooms() { auto& tiles = $map.tiles(); + Config style_config("assets/styles.json"); + json& styles = style_config.json(); for(auto& room : $map.rooms()) { + auto& style = styles[Random::uniform(size_t(0), styles.size() - 1)]; + + dbc::check(style.contains("floor"), + fmt::format("no floor spec in style {}", (std::string)style["name"])); + dbc::check(style.contains("walls"), + fmt::format("no walls spec in style {}", (std::string)style["name"])); + + auto& floor_name = style["floor"]; + auto& wall_name = style["walls"]; + size_t floor_id = textures::get_id(floor_name); + size_t wall_id = textures::get_id(wall_name); + for(matrix::box it{tiles, room.x, room.y, room.width+1, room.height+1}; it.next();) { if(tiles[it.y][it.x] == 1) { - tiles[it.y][it.x] = 2; + tiles[it.y][it.x] = wall_id; } else if(tiles[it.y][it.x] == 0) { - tiles[it.y][it.x] = 6; + tiles[it.y][it.x] = floor_id; } } }