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.
This commit is contained in:
parent
e45de2a2cf
commit
d2a5dfa713
7 changed files with 56 additions and 2 deletions
17
assets/styles.json
Normal file
17
assets/styles.json
Normal file
|
@ -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,
|
||||
"light": 0,
|
||||
"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);
|
||||
}
|
||||
|
||||
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];
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -20,6 +20,7 @@ namespace textures {
|
|||
std::vector<wchar_t> map_tile_set;
|
||||
std::vector<int> ambient_light;
|
||||
std::unordered_map<std::string, SpriteTexture> sprite_textures;
|
||||
std::unordered_map<std::string, size_t> name_to_id;
|
||||
};
|
||||
|
||||
void init();
|
||||
|
@ -29,9 +30,12 @@ namespace textures {
|
|||
sf::Image load_image(const std::string& filename);
|
||||
|
||||
std::vector<int>& get_ambient_light();
|
||||
|
||||
std::vector<wchar_t>& 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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue