There's a bug where the last item in tiles.json draws a black square, which is why I named ceiling_blue to zceiling_blue to temporarily solve it.

master
Zed A. Shaw 12 hours ago
parent b2a6262964
commit 5e01eb29a9
  1. 16
      assets/enemies.json
  2. 4
      assets/items.json
  3. 76
      assets/map_tiles.json
  4. BIN
      assets/map_tiles.png
  5. 37
      assets/tiles.json
  6. 4
      tests/map.cpp
  7. 57
      tools/icongen.cpp

@ -2,9 +2,9 @@
"PLAYER_TILE": { "PLAYER_TILE": {
"placement": "fixed", "placement": "fixed",
"components": [ "components": [
{"_type": "Tile", "display": 42603, {"_type": "Tile", "display": 41981,
"foreground": [255, 200, 125], "foreground": [255, 200, 125],
"background": [30, 20, 75] "background": [0,0,0]
}, },
{"_type": "Combat", "hp": 200, "max_hp": 200, "damage": 10, "dead": false}, {"_type": "Combat", "hp": 200, "max_hp": 200, "damage": 10, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false},
@ -13,9 +13,9 @@
}, },
"GOLD_SAVIOR": { "GOLD_SAVIOR": {
"components": [ "components": [
{"_type": "Tile", "display": 2189, {"_type": "Tile", "display": 42586,
"foreground": [131, 213, 238], "foreground": [131, 213, 238],
"background": [30, 20, 75] "background": [0,0,0]
}, },
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false}, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false},
@ -30,7 +30,7 @@
"components": [ "components": [
{"_type": "Tile", "display": 2189, {"_type": "Tile", "display": 2189,
"foreground": [131, 213, 238], "foreground": [131, 213, 238],
"background": [30, 20, 75] "background": [0,0,0]
}, },
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false}, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false},
@ -45,7 +45,7 @@
"components": [ "components": [
{"_type": "Tile", "display": 1898, {"_type": "Tile", "display": 1898,
"foreground": [156, 172, 197], "foreground": [156, 172, 197],
"background": [30, 20, 75] "background": [0,0,0]
}, },
{"_type": "Combat", "hp": 40, "max_hp": 40, "damage": 10, "dead": false}, {"_type": "Combat", "hp": 40, "max_hp": 40, "damage": 10, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": true}, {"_type": "Motion", "dx": 0, "dy": 0, "random": true},
@ -60,7 +60,7 @@
"components": [ "components": [
{"_type": "Tile", "display": 2220, {"_type": "Tile", "display": 2220,
"foreground": [205, 164, 246], "foreground": [205, 164, 246],
"background": [30, 20, 75] "background": [0,0,0]
}, },
{"_type": "Combat", "hp": 50, "max_hp": 50, "damage": 2, "dead": false}, {"_type": "Combat", "hp": 50, "max_hp": 50, "damage": 2, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false},
@ -75,7 +75,7 @@
"components": [ "components": [
{"_type": "Tile", "display": 1218, {"_type": "Tile", "display": 1218,
"foreground": [205, 164, 246], "foreground": [205, 164, 246],
"background": [30, 20, 75] "background": [0,0,0]
}, },
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false}, {"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false},

@ -8,7 +8,7 @@
{"_type": "LightSource", "strength": 50, "radius": 2.5}, {"_type": "LightSource", "strength": 50, "radius": 2.5},
{"_type": "Tile", "display": 3848, {"_type": "Tile", "display": 3848,
"foreground": [24, 120, 189], "foreground": [24, 120, 189],
"background": [230,120, 120] "background": [0,0,0]
}, },
{"_type": "Sprite", "name": "torch_horizontal_floor", "width": 256, "height": 256, "scale": 1.0}, {"_type": "Sprite", "name": "torch_horizontal_floor", "width": 256, "height": 256, "scale": 1.0},
{"_type": "Sound", "attack": "pickup", "death": "blank"} {"_type": "Sound", "attack": "pickup", "death": "blank"}
@ -22,7 +22,7 @@
"components": [ "components": [
{"_type": "Tile", "display": 1003, {"_type": "Tile", "display": 1003,
"foreground": [255, 205, 189], "foreground": [255, 205, 189],
"background": [255, 205, 189] "background": [0,0,0]
}, },
{"_type": "Curative", "hp": 20}, {"_type": "Curative", "hp": 20},
{"_type": "Sprite", "name": "healing_potion_small", "width": 256, "height": 256, "scale": 1.0}, {"_type": "Sprite", "name": "healing_potion_small", "width": 256, "height": 256, "scale": 1.0},

@ -7,122 +7,122 @@
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 8284,
"x": 32, "x": 64,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 10398, "display": 11590,
"x": 64, "x": 128,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 10899,
"x": 96, "x": 192,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 9256,
"x": 128, "x": 256,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 8820, "display": 9608,
"x": 160, "x": 320,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 9608, "display": 10747,
"x": 192, "x": 384,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 8285,
"x": 224, "x": 448,
"y": 0 "y": 0
}, },
{ {
"centered": true, "centered": true,
"display": 1003, "display": 1003,
"x": 256, "x": 512,
"y": 0 "y": 0
}, },
{ {
"centered": true, "centered": true,
"display": 3848, "display": 3848,
"x": 288, "x": 576,
"y": 0 "y": 0
}, },
{ {
"centered": true, "centered": true,
"display": 85, "display": 85,
"x": 0, "x": 0,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 8687, "display": 8687,
"x": 32, "x": 64,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 10949, "display": 10949,
"x": 64, "x": 128,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 8793, "display": 8793,
"x": 96, "x": 192,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 95, "display": 95,
"x": 128, "x": 256,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 1898, "display": 1898,
"x": 160, "x": 320,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 2189, "display": 42586,
"x": 192, "x": 384,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 2189, "display": 2189,
"x": 224, "x": 448,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 42603, "display": 41981,
"x": 256, "x": 512,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 2220, "display": 2220,
"x": 288, "x": 576,
"y": 32 "y": 64
}, },
{ {
"centered": true, "centered": true,
"display": 1218, "display": 1218,
"x": 0, "x": 0,
"y": 64 "y": 128
} }
] ]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 7.7 KiB

@ -2,7 +2,7 @@
"floor_tile": { "floor_tile": {
"texture": "assets/textures/floor_gray_stone.png", "texture": "assets/textures/floor_gray_stone.png",
"collision": false, "collision": false,
"display": 10398, "display": 8284,
"ceiling": "ceiling_black", "ceiling": "ceiling_black",
"light": 0, "light": 0,
"foreground": [40, 40, 40], "foreground": [40, 40, 40],
@ -20,10 +20,10 @@
"wall_moss": { "wall_moss": {
"texture": "assets/textures/glowing_moss_wall.png", "texture": "assets/textures/glowing_moss_wall.png",
"collision": true, "collision": true,
"display": 8820, "display": 9256,
"light": 20, "light": 20,
"foreground": [100, 150, 100], "foreground": [100, 100, 180],
"background": [100, 100, 100], "background": [100, 150, 100],
"id": 2 "id": 2
}, },
"ceiling_black": { "ceiling_black": {
@ -37,37 +37,38 @@
"lava_floor": { "lava_floor": {
"texture": "assets/textures/lava_floor.png", "texture": "assets/textures/lava_floor.png",
"collision": false, "collision": false,
"display": 35, "display": 10899,
"ceiling": "ceiling_black", "ceiling": "ceiling_black",
"light": 20, "light": 20,
"foreground": [200, 100, 100], "foreground": [200, 100, 100],
"background": [10, 10, 10], "background": [100, 100, 50],
"id": 5 "id": 5
}, },
"gray_stone_floor_light": { "gray_stone_floor_light": {
"texture": "assets/textures/gray_stone_floor_light.png", "texture": "assets/textures/gray_stone_floor_light.png",
"collision": false, "collision": false,
"display": 35, "display": 11590,
"ceiling": "ceiling_blue_light", "ceiling": "zceiling_blue_light",
"light": 40, "light": 40,
"foreground": [40, 40, 40], "background": [80, 80, 80],
"foreground": [40, 60, 180],
"id": 6 "id": 6
}, },
"ceiling_blue_light": {
"texture": "assets/textures/ceiling_blue_light.png",
"collision": false,
"display": 35,
"light": 0,
"foreground": [100, 100, 100],
"id": 7
},
"wood_wall": { "wood_wall": {
"texture": "assets/textures/wood_wall.png", "texture": "assets/textures/wood_wall.png",
"collision": false, "collision": false,
"display": 35, "display": 10747,
"light": 0, "light": 0,
"foreground": [70, 70, 70], "foreground": [70, 70, 70],
"background": [100, 100, 100], "background": [100, 100, 100],
"id": 8 "id": 8
},
"zceiling_blue_light": {
"texture": "assets/textures/ceiling_blue_light.png",
"collision": false,
"display": 8285,
"light": 0,
"foreground": [100, 100, 100],
"id": 7
} }
} }

@ -96,7 +96,7 @@ TEST_CASE("map image test", "[map-sprite]") {
auto &walls = level.map->tiles(); auto &walls = level.map->tiles();
auto &tile_set = textures::get_map_tile_set(); auto &tile_set = textures::get_map_tile_set();
sf::Vector2i size{32,32}; sf::Vector2i size{64,64};
matrix::dump("TILES?", walls); matrix::dump("TILES?", walls);
std::unordered_map<wchar_t, sf::Vector2i> sprite_coord; std::unordered_map<wchar_t, sf::Vector2i> sprite_coord;
@ -140,8 +140,6 @@ TEST_CASE("map image test", "[map-sprite]") {
REQUIRE(sprite_coord.contains(entity_glyph.display)); REQUIRE(sprite_coord.contains(entity_glyph.display));
auto sprite = render_sprite(sprite_coord, size, entity_glyph.display, map_sprites); auto sprite = render_sprite(sprite_coord, size, entity_glyph.display, map_sprites);
sprite.setColor({255,150,150,255});
sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)}); sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)});
render.draw(sprite); render.draw(sprite);
}); });

@ -12,7 +12,8 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
constexpr const int TILE_COUNT=10; constexpr const int TILE_COUNT=10;
constexpr const sf::Color DEFAULT_COLOR{255, 255, 255, 255}; constexpr const sf::Color DEFAULT_COLOR{255, 255, 255, 255};
constexpr const size_t DEFAULT_DIM=32; constexpr const size_t DEFAULT_DIM=64;
using namespace nlohmann;
using namespace shiterator; using namespace shiterator;
@ -92,30 +93,36 @@ struct MapTileBuilder {
for(each_row_t<MapGrid> it{config.map}; it.next();) { for(each_row_t<MapGrid> it{config.map}; it.next();) {
// a 0 slot means we're done // a 0 slot means we're done
if(config.map[it.y][it.x] == 0) break; if(config.map[it.y][it.x] == 0) break;
cell_pos.x = it.x * $size.x; cell_pos.x = it.x * $size.x;
cell_pos.y = it.y * $size.y; cell_pos.y = it.y * $size.y;
bool is_centered = config.centered[it.y][it.x]; bool is_centered = config.centered[it.y][it.x];
wchar_t display_char = config.map[it.y][it.x]; wchar_t display_char = config.map[it.y][it.x];
std::wstring content{display_char}; std::wstring content{display_char};
auto bg = config.backgrounds.at(display_char);
auto fg = config.colors.at(display_char);
best_size(display_char, is_centered); best_size(display_char, is_centered);
sf::Text icon{$font, content, $font_size}; sf::Text icon{$font, content, $font_size};
icon.setFillColor({0, 0, 0, 255}); icon.setFillColor({255, 255, 255, 255});
$temp_render.draw(icon); $temp_render.draw(icon);
$temp_render.clear({0,0,0,0}); $temp_render.clear({0,0,0,0});
auto font_texture = $font.getTexture($font_size); auto& font_texture = $font.getTexture($font_size);
sf::Sprite sprite{font_texture, $glyph.textureRect}; sf::Sprite sprite{font_texture, $glyph.textureRect};
auto t_size = $glyph.textureRect.size; auto t_size = $glyph.textureRect.size;
dbc::check($size.x - t_size.x >= 0, "font too big on x"); dbc::check($size.x - t_size.x >= 0, "font too big on x");
dbc::check($size.y - t_size.y >= 0, "font too big on y"); dbc::check($size.y - t_size.y >= 0, "font too big on y");
fmt::println("display: {}, bg: {},{},{},{}; fg: {},{},{},{}",
(int)display_char, bg.r, bg.g, bg.b, bg.a, fg.r, fg.g, fg.b, fg.a);
// draw the background first // draw the background first
background.setFillColor(config.backgrounds[display_char]); background.setFillColor(bg);
if(is_centered) { if(is_centered) {
sf::Vector2f center{ sf::Vector2f center{
@ -131,23 +138,22 @@ struct MapTileBuilder {
background.setPosition(cell_pos); background.setPosition(cell_pos);
} }
sprite.setColor(config.colors[display_char]); sprite.setColor(fg);
$render->draw(background); $render->draw(background);
$render->draw(sprite); $render->draw(sprite);
$render->display();
} }
$render->display();
} }
void save_config(MapConfig& config, const std::string &path) { void save_config(MapConfig& config, const std::string &path) {
(void)path; (void)path;
nlohmann::json result = nlohmann::json::array(); json result = json::array();
for(each_row_t<MapGrid> it{config.map}; it.next();) { for(each_row_t<MapGrid> it{config.map}; it.next();) {
if(config.map[it.y][it.x] == 0) break; if(config.map[it.y][it.x] == 0) break;
nlohmann::json val; json val;
val["x"] = $size.x * it.x; val["x"] = $size.x * it.x;
val["y"] = $size.y * it.y; val["y"] = $size.y * it.y;
@ -162,60 +168,69 @@ struct MapTileBuilder {
} }
}; };
void load_config(MapConfig& config, bool is_centered, std::string path, std::function<wchar_t(nlohmann::json&)> finder) void load_config(MapConfig& config, bool is_centered, std::string path, std::function<json&(json&)> finder)
{ {
Config tiles(path); Config tiles(path);
for(auto [key, val] : tiles.json().items()) { for(auto [key, val] : tiles.json().items()) {
config.it.next(); config.it.next();
auto display = finder(val); auto data = finder(val);
wchar_t display = data["display"];
config.map[config.it.y][config.it.x] = display; config.map[config.it.y][config.it.x] = display;
config.centered[config.it.y][config.it.x] = is_centered; config.centered[config.it.y][config.it.x] = is_centered;
if(val.contains("foreground")) { dbc::check(!config.colors.contains(display),
auto fg_color = val["foreground"]; fmt::format("duplicate color for display={} key={}",
(int)display, (std::string)key));
if(data.contains("foreground")) {
auto fg_color = data["foreground"];
sf::Color fg{fg_color[0], fg_color[1], fg_color[2]}; sf::Color fg{fg_color[0], fg_color[1], fg_color[2]};
fmt::println("TILE {}, display: {} has foreground: {},{},{}", key, (int)display, fg.r, fg.g, fg.b);
config.colors.insert_or_assign(display, fg); config.colors.insert_or_assign(display, fg);
} else { } else {
fmt::println("TILE {}, {} has DEFAULT COLOR", key, (int)display);
config.colors.insert_or_assign(display, DEFAULT_COLOR); config.colors.insert_or_assign(display, DEFAULT_COLOR);
} }
if(val.contains("background")) { if(data.contains("background")) {
auto bg_color = val["background"]; auto bg_color = data["background"];
sf::Color bg{bg_color[0], bg_color[1], bg_color[2]}; sf::Color bg{bg_color[0], bg_color[1], bg_color[2]};
fmt::println("TILE {} display: {} has background: {},{},{}", key, (int)display, bg.r, bg.g, bg.b);
config.backgrounds.insert_or_assign(display, bg); config.backgrounds.insert_or_assign(display, bg);
} else { } else {
fmt::println("TILE {} display: {} has transparent background", key, (int)display);
sf::Color bg{0, 0, 0, 0}; sf::Color bg{0, 0, 0, 0};
config.backgrounds.insert_or_assign(display, bg); config.backgrounds.insert_or_assign(display, bg);
} }
} }
} }
wchar_t component_display(nlohmann::json& val) { json& component_display(json& val) {
auto& components = val["components"]; auto& components = val["components"];
for(auto& comp : components) { for(auto& comp : components) {
if(comp["_type"] == "Tile") { if(comp["_type"] == "Tile") {
return comp["display"]; return comp;
} }
} }
dbc::log("BAD CHAR"); dbc::log("BAD CHAR");
return L'!'; return val;
} }
int main() { int main() {
MapConfig config; MapConfig config;
load_config(config, false, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t { load_config(config, false, "./assets/tiles.json", [](json& val) -> json& {
return val["display"]; return val;
}); });
load_config(config, true, "./assets/items.json", component_display); load_config(config, true, "./assets/items.json", component_display);
load_config(config, true, "./assets/devices.json", component_display); load_config(config, true, "./assets/devices.json", component_display);
load_config(config, true, "./assets/enemies.json", component_display); load_config(config, true, "./assets/enemies.json", component_display);
fmt::println("-----------------------------------------");
MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM); MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM);
builder.run(config); builder.run(config);

Loading…
Cancel
Save