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.

master
Zed A. Shaw 10 months ago
parent e45de2a2cf
commit d2a5dfa713
  1. 17
      assets/styles.json
  2. 7
      assets/tiles.json
  3. 4
      config.cpp
  4. 1
      config.hpp
  5. 7
      textures.cpp
  6. 4
      textures.hpp
  7. 18
      worldbuilder.cpp

@ -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"
}
]

@ -50,5 +50,12 @@
"display": 35, "display": 35,
"light": 0, "light": 0,
"id": 7 "id": 7
},
"wood_wall": {
"texture": "assets/textures/wood_wall.png",
"collision": false,
"display": 35,
"light": 0,
"id": 8
} }
} }

@ -15,6 +15,10 @@ Config::Config(const std::string src_path) : $src_path(src_path) {
$config = json::parse(infile); $config = json::parse(infile);
} }
nlohmann::json &Config::operator[](size_t key) {
return $config[key];
}
json &Config::operator[](const std::string &key) { json &Config::operator[](const std::string &key) {
dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key)); dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key));
return $config[key]; return $config[key];

@ -14,6 +14,7 @@ struct Config {
Config(nlohmann::json config, std::string src_path) Config(nlohmann::json config, std::string src_path)
: $config(config), $src_path(src_path) {} : $config(config), $src_path(src_path) {}
nlohmann::json &operator[](size_t);
nlohmann::json &operator[](const std::string &key); nlohmann::json &operator[](const std::string &key);
nlohmann::json &json() { return $config; }; nlohmann::json &json() { return $config; };
std::wstring wstring(const std::string main_key, const std::string sub_key); std::wstring wstring(const std::string main_key, const std::string sub_key);

@ -46,6 +46,7 @@ namespace textures {
auto &config = el.value(); auto &config = el.value();
const std::string& texture_fname = config["texture"]; const std::string& texture_fname = config["texture"];
size_t surface_i = config["id"]; size_t surface_i = config["id"];
TMGR.name_to_id.insert_or_assign(el.key(), surface_i);
if(surface_i >= tiles.size()) { if(surface_i >= tiles.size()) {
resize_shit(surface_i + 1); resize_shit(surface_i + 1);
@ -113,4 +114,10 @@ namespace textures {
size_t ceiling_num = TMGR.ceilings[num]; size_t ceiling_num = TMGR.ceilings[num];
return (const uint32_t *)TMGR.surfaces[ceiling_num].getPixelsPtr(); 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);
}
}; };

@ -20,6 +20,7 @@ namespace textures {
std::vector<wchar_t> map_tile_set; std::vector<wchar_t> map_tile_set;
std::vector<int> ambient_light; std::vector<int> ambient_light;
std::unordered_map<std::string, SpriteTexture> sprite_textures; std::unordered_map<std::string, SpriteTexture> sprite_textures;
std::unordered_map<std::string, size_t> name_to_id;
}; };
void init(); void init();
@ -29,9 +30,12 @@ namespace textures {
sf::Image load_image(const std::string& filename); sf::Image load_image(const std::string& filename);
std::vector<int>& get_ambient_light(); std::vector<int>& get_ambient_light();
std::vector<wchar_t>& get_map_tile_set(); std::vector<wchar_t>& get_map_tile_set();
const uint32_t* get_surface(size_t num); const uint32_t* get_surface(size_t num);
const uint32_t* get_ceiling(size_t num); const uint32_t* get_ceiling(size_t num);
size_t get_id(const std::string& name);
} }

@ -12,13 +12,27 @@ using namespace components;
void WorldBuilder::stylize_rooms() { void WorldBuilder::stylize_rooms() {
auto& tiles = $map.tiles(); auto& tiles = $map.tiles();
Config style_config("assets/styles.json");
json& styles = style_config.json();
for(auto& room : $map.rooms()) { 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();) { for(matrix::box it{tiles, room.x, room.y, room.width+1, room.height+1}; it.next();) {
if(tiles[it.y][it.x] == 1) { 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) { } else if(tiles[it.y][it.x] == 0) {
tiles[it.y][it.x] = 6; tiles[it.y][it.x] = floor_id;
} }
} }
} }