diff --git a/assets/devices.json b/assets/devices.json index 5a464a4..fa58b10 100644 --- a/assets/devices.json +++ b/assets/devices.json @@ -7,7 +7,7 @@ "inventory_count": 0, "randomized": false, "components": [ - {"_type": "Tile", "chr": "\u2ac5", + {"_type": "Tile", "display": "\u2ac5", "foreground": [24, 205, 189], "background": [24, 205, 189] }, @@ -23,7 +23,7 @@ "inventory_count": 0, "placement": "fixed", "components": [ - {"_type": "Tile", "chr": "\u2259", + {"_type": "Tile", "display": "\u2259", "foreground": [24, 205, 189], "background": [24, 205, 189] }, @@ -38,7 +38,7 @@ "description": "Spikes stab you from the floor.", "inventory_count": 0, "components": [ - {"_type": "Tile", "chr": "\u1ac7", + {"_type": "Tile", "display": "\u1ac7", "foreground": [24, 205, 189], "background": [24, 205, 189] }, diff --git a/assets/enemies.json b/assets/enemies.json index 91b112f..0cad1c6 100644 --- a/assets/enemies.json +++ b/assets/enemies.json @@ -1,7 +1,7 @@ { "PLAYER_TILE": { "components": [ - {"_type": "Tile", "chr": "\ua66b", + {"_type": "Tile", "display": "\ua66b", "foreground": [255, 200, 125], "background": [30, 20, 75] }, @@ -13,7 +13,7 @@ }, "EVIL_EYE": { "components": [ - {"_type": "Tile", "chr": "\u08ac", + {"_type": "Tile", "display": "\u08ac", "foreground": [75, 200, 125], "background": [30, 20, 75] }, diff --git a/assets/items.json b/assets/items.json index a5c5284..e190e3a 100644 --- a/assets/items.json +++ b/assets/items.json @@ -6,7 +6,7 @@ "inventory_count": 1, "components": [ {"_type": "LightSource", "strength": 70, "radius": 2.0}, - {"_type": "Tile", "chr": "\u0f08", + {"_type": "Tile", "display": "\u0f08", "foreground": [24, 120, 189], "background": [230,120, 120] } @@ -19,7 +19,7 @@ "inventory_count": 1, "components": [ {"_type": "Weapon", "damage": 15}, - {"_type": "Tile", "chr": "\u1e37", + {"_type": "Tile", "display": "\u1e37", "foreground": [24, 120, 189], "background": [24, 120, 189] } @@ -32,7 +32,7 @@ "inventory_count": 1, "components": [ {"_type": "LightSource", "strength": 70, "radius": 1.8}, - {"_type": "Tile", "chr": "\u0236", + {"_type": "Tile", "display": "\u0236", "foreground": [24, 205, 210], "background": [24, 205, 210] }, @@ -44,7 +44,7 @@ "name": "Small Chest", "description": "A small chest of gold. You wonder who would leave something like this around.", "components": [ - {"_type": "Tile", "chr": "\uaaea", + {"_type": "Tile", "display": "\uaaea", "foreground": [150, 100, 189], "background": [150, 100, 189] }, @@ -58,7 +58,7 @@ "description": "A torch on a wall you can't pick up.", "inventory_count": 0, "components": [ - {"_type": "Tile", "chr": "\u077e", + {"_type": "Tile", "display": "\u077e", "foreground": [24, 205, 210], "background": [24, 205, 210] }, @@ -71,7 +71,7 @@ "description": "A small healing potion.", "inventory_count": 1, "components": [ - {"_type": "Tile", "chr": "\u03eb", + {"_type": "Tile", "display": "\u03eb", "foreground": [255, 205, 189], "background": [255, 205, 189] }, diff --git a/components.cpp b/components.cpp index b44ead6..8e8575c 100644 --- a/components.cpp +++ b/components.cpp @@ -7,7 +7,6 @@ namespace components { ENROLL_COMPONENT(Weapon, damage); ENROLL_COMPONENT(Curative, hp); ENROLL_COMPONENT(EnemyConfig, hearing_distance); - ENROLL_COMPONENT(Tile, chr, foreground, background); ENROLL_COMPONENT(Motion, dx, dy, random); ENROLL_COMPONENT(Combat, hp, damage, dead); ENROLL_COMPONENT(LightSource, strength, radius); diff --git a/components.hpp b/components.hpp index bbc3b1d..fcdb869 100644 --- a/components.hpp +++ b/components.hpp @@ -25,7 +25,7 @@ namespace components { }; struct Tile { - std::string chr; + std::string display; std::array foreground; std::array background; }; @@ -78,4 +78,7 @@ namespace components { }; void configure(ComponentMap& component_map); + + // these need to be here if you're using components::convert outside of components.cpp + ENROLL_COMPONENT(Tile, display, foreground, background); } diff --git a/dinky_components.hpp b/dinky_components.hpp index 73cf955..0cca176 100644 --- a/dinky_components.hpp +++ b/dinky_components.hpp @@ -19,6 +19,12 @@ namespace components { using ReflFuncSignature = std::function; using ComponentMap = std::unordered_map; + template COMPONENT convert(nlohmann::json &data) { + COMPONENT result; + from_json(data, result); + return result; + } + template void enroll(ComponentMap &m) { m[NameOf::name] = [](DinkyECS::World& world, DinkyECS::Entity ent, nlohmann::json &j) { COMPONENT c; diff --git a/gui.cpp b/gui.cpp index af940aa..8efe135 100644 --- a/gui.cpp +++ b/gui.cpp @@ -37,7 +37,7 @@ namespace gui { for(size_t y = 0; y < end_y; ++y) { for(size_t x = 0; x < end_x; ++x) { - const TileCell& tile = tiles.at(start.x+x, start.y+y); + const Tile& tile = tiles.at(start.x+x, start.y+y); // light value is an integer that's a percent float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT; int dnum = debug.PATHS ? paths[start.y+y][start.x+x] : WALL_PATH_LIMIT; @@ -47,12 +47,12 @@ namespace gui { $canvas.DrawText(x * 2, y * 4, num, [dnum, tile, light_value](auto &pixel) { pixel.foreground_color = Color::HSV(dnum * 20, 150, 200); - pixel.background_color = Color::HSV(30, 20, tile.bg_v * 50 * PERCENT); + pixel.background_color = Color::HSV(30, 20, tile.foreground[2] * 50 * PERCENT); }); } else { $canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) { - pixel.foreground_color = Color::HSV(tile.fg_h, tile.fg_s, tile.fg_v * light_value); - pixel.background_color = Color::HSV(tile.bg_h, tile.bg_s, tile.bg_v * light_value); + pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value); + pixel.background_color = Color::HSV(tile.background[0], tile.background[1], tile.background[2] * light_value); }); } } diff --git a/systems.cpp b/systems.cpp index c9c37db..b31c847 100644 --- a/systems.cpp +++ b/systems.cpp @@ -238,12 +238,12 @@ void System::draw_entities(DinkyECS::World &world, Map &map, const Matrix &light Point loc = map.map_to_camera(pos.location, cam_orig); float light_value = lights[pos.location.y][pos.location.x] * PERCENT; - const TileCell& cell = tiles.at(pos.location.x, pos.location.y); + const Tile& cell = tiles.at(pos.location.x, pos.location.y); // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing - canvas.DrawText(loc.x*2, loc.y*4, tile.chr, [tile, light_value, cell](auto &pixel) { + canvas.DrawText(loc.x*2, loc.y*4, tile.display, [tile, light_value, cell](auto &pixel) { pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value); - pixel.background_color = Color::HSV(cell.bg_h, cell.bg_s, cell.bg_v * light_value); + pixel.background_color = Color::HSV(cell.background[0], cell.background[1], cell.background[2] * light_value); }); } }); diff --git a/tilemap.cpp b/tilemap.cpp index 8987144..ee4f566 100644 --- a/tilemap.cpp +++ b/tilemap.cpp @@ -3,19 +3,20 @@ #include "constants.hpp" using nlohmann::json; +using components::Tile; 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, {""})) + $display(height, TileRow(width, {"", {0,0,0}, {0,0,0}})) { } void TileMap::dump(int show_x, int show_y) { for(matrix::each_row it{$tile_ids}; it.next();) { - const TileCell &cell = $display[it.y][it.x]; + const Tile &cell = $display[it.y][it.x]; if(int(it.x) == show_x && int(it.y) == show_y) { fmt::print("{}<", cell.display); @@ -30,15 +31,7 @@ void TileMap::dump(int show_x, int show_y) { void TileMap::set_tile(size_t x, size_t y, string tile_name) { std::wstring tile_id = $config.wstring(tile_name, "display"); json tile_conf = $config[tile_name]; - TileCell tile{ - tile_conf["display"], - tile_conf["foreground"][0], - tile_conf["foreground"][1], - tile_conf["foreground"][2], - tile_conf["background"][0], - tile_conf["background"][1], - tile_conf["background"][2]}; - + auto tile = components::convert(tile_conf); $tile_ids[y][x] = tile_id[0]; $display[y][x] = tile; } @@ -50,7 +43,7 @@ void TileMap::load(matrix::Matrix &walls) { } } -const TileCell &TileMap::at(size_t x, size_t y) { +const Tile &TileMap::at(size_t x, size_t y) { return $display[y][x]; } diff --git a/tilemap.hpp b/tilemap.hpp index 495300b..099152e 100644 --- a/tilemap.hpp +++ b/tilemap.hpp @@ -7,18 +7,9 @@ #include "point.hpp" #include "matrix.hpp" #include "config.hpp" +#include "components.hpp" -struct TileCell { - std::string display; - uint8_t fg_h = 0; - uint8_t fg_s = 0; - uint8_t fg_v = 0; - uint8_t bg_h = 0; - uint8_t bg_s = 0; - uint8_t bg_v = 0; -}; - -typedef std::vector TileRow; +typedef std::vector TileRow; typedef std::vector TileGrid; class TileMap { @@ -37,7 +28,7 @@ public: size_t width() { return $width; } size_t height() { return $height; } void load(matrix::Matrix &walls); - const TileCell &at(size_t x, size_t y); + const components::Tile &at(size_t x, size_t y); void set_tile(size_t x, size_t y, std::string tile_name); std::vector tile_names(bool collision);