From 713d400d17f63d9c8a90f146d645d4d9b38b328d Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 4 Nov 2024 05:07:49 -0500 Subject: [PATCH] Cereal works pretty well so I may use it, but there's one more library I want to try first called tser. --- assets/config.json | 4 ++-- components.hpp | 12 ++++++++++++ main.cpp | 19 +++++++++++++++++-- map.cpp | 1 - map.hpp | 5 ----- meson.build | 8 ++------ systems.cpp | 11 ++++++----- tests/config.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ wraps/cereal.wrap | 12 ++++++++++++ 9 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 wraps/cereal.wrap diff --git a/assets/config.json b/assets/config.json index e1946c2..e1f0d3f 100644 --- a/assets/config.json +++ b/assets/config.json @@ -6,8 +6,8 @@ "ENEMY_TILE": "Ω", "BG_TILE": "█" }, - "enemies": { - + "enemy": { + "HEARING_DISTANCE": 8 }, "player": { diff --git a/components.hpp b/components.hpp index 7e46b51..7f0ebb1 100644 --- a/components.hpp +++ b/components.hpp @@ -25,4 +25,16 @@ namespace Components { struct Tile { std::string chr = "!"; }; + + struct MapConfig { + std::string WALL_TILE; + std::string FLOOR_TILE; + std::string PLAYER_TILE; + std::string ENEMY_TILE; + std::string BG_TILE; + }; + + struct EnemyConfig { + int HEARING_DISTANCE; + }; } diff --git a/main.cpp b/main.cpp index 0ff9ab9..f604477 100644 --- a/main.cpp +++ b/main.cpp @@ -17,6 +17,7 @@ using namespace ftxui; * system. */ void configure_world(DinkyECS::World &world, Map &game_map) { + const auto &config = world.get_the(); // this sets up the gui event system world.set_the(Events::GUI::START); @@ -30,13 +31,13 @@ void configure_world(DinkyECS::World &world, Map &game_map) { world.set(player.entity, {game_map.place_entity(0)}); world.set(player.entity, {0, 0}); world.set(player.entity, {100, 10}); - world.set(player.entity, {PLAYER_TILE}); + world.set(player.entity, {config.PLAYER_TILE}); auto enemy = world.entity(); world.set(enemy, {game_map.place_entity(1)}); world.set(enemy, {0,0}); world.set(enemy, {20, 10}); - world.set(enemy, {ENEMY_TILE}); + world.set(enemy, {config.ENEMY_TILE}); auto enemy2 = world.entity(); world.set(enemy2, {game_map.place_entity(2)}); @@ -57,6 +58,20 @@ int main() { Config config("./assets/config.json"); world.set_the(config); + auto map = config["map"]; + world.set_the({ + map["WALL_TILE"], + map["FLOOR_TILE"], + map["PLAYER_TILE"], + map["ENEMY_TILE"], + map["BG_TILE"] + }); + + auto enemy = config["enemy"]; + world.set_the({ + enemy["HEARING_DISTANCE"] + }); + Map game_map(GAME_MAP_X, GAME_MAP_Y); game_map.generate(); diff --git a/map.cpp b/map.cpp index 702d41a..fc423fd 100644 --- a/map.cpp +++ b/map.cpp @@ -6,7 +6,6 @@ #include "rand.hpp" #include - using std::vector, std::pair; using namespace fmt; diff --git a/map.hpp b/map.hpp index 680fa22..d368952 100644 --- a/map.hpp +++ b/map.hpp @@ -11,11 +11,6 @@ #define INV_SPACE 1 #define WALL_VALUE 1 #define SPACE_VALUE 0 -#define WALL_TILE "█" -#define FLOOR_TILE "\u2849" -#define PLAYER_TILE "♣" -#define ENEMY_TILE "Ω" -#define BG_TILE L'█' struct Room { size_t x = 0; diff --git a/meson.build b/meson.build index abed8bb..8722b84 100644 --- a/meson.build +++ b/meson.build @@ -8,10 +8,11 @@ ftxui_screen = dependency('ftxui-screen') ftxui_dom = dependency('ftxui-dom') ftxui_component = dependency('ftxui-component') sfml = dependency('sfml') +cereal = dependency('cereal') dependencies = [catch2, fmt, ftxui_screen, ftxui_dom, ftxui_component, - json, sfml] + json, sfml, cereal] runtests = executable('runtests', [ 'dbc.cpp', @@ -48,9 +49,4 @@ roguish = executable('roguish', [ ], dependencies: dependencies) -collider = executable('collider', [ - './scratchpad/collider.cpp' - ], - dependencies: dependencies) - test('tests', runtests) diff --git a/systems.cpp b/systems.cpp index 9cf08e3..6d792c8 100644 --- a/systems.cpp +++ b/systems.cpp @@ -13,9 +13,9 @@ using namespace fmt; using namespace Components; using ftxui::Color; -#define HEARING_DISTANCE 8 - void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) { + // TODO: this will be on each enemy not a global thing + const auto &config = world.get_the(); const auto &player_position = world.get(player.entity); game_map.set_target(player_position.location); game_map.make_paths(); @@ -23,7 +23,7 @@ void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player world.query([&](const auto &ent, auto &position, auto &motion) { if(ent != player.entity) { Point out = position.location; // copy - if(game_map.distance(out) < HEARING_DISTANCE) { + if(game_map.distance(out) < config.HEARING_DISTANCE) { game_map.neighbors(out, false); motion = { int(out.x - position.location.x), int(out.y - position.location.y)}; } @@ -133,6 +133,7 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas } void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) { + const auto& config = world.get_the(); const auto& player = world.get_the(); const auto& player_position = world.get(player.entity); Point start = game_map.center_camera(player_position.location, view_x, view_y); @@ -143,9 +144,9 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv for(size_t x = 0; x < end_x; ++x) { for(size_t y = 0; y < end_y; ++y) { - string tile = walls[start.y+y][start.x+x] == 1 ? WALL_TILE : FLOOR_TILE; + string tile = walls[start.y+y][start.x+x] == 1 ? config.WALL_TILE : config.FLOOR_TILE; // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing - if(tile == WALL_TILE) { + if(tile == config.WALL_TILE) { canvas.DrawText(x * 2, y * 4, tile, Color::RGB(70, 70, 70)); } else { canvas.DrawText(x * 2, y * 4, tile, Color::RGB(20, 20, 20)); diff --git a/tests/config.cpp b/tests/config.cpp index 5ccdd85..b4881b7 100644 --- a/tests/config.cpp +++ b/tests/config.cpp @@ -4,6 +4,7 @@ #include "config.hpp" #include #include "dinkyecs.hpp" +#include "components.hpp" using namespace fmt; using std::string; @@ -60,3 +61,46 @@ TEST_CASE("can go into a world", "[config]") { Config &cfg = world.get_the(); REQUIRE(cfg["types"]["NUMBER"] == 1234); } + +#include +#include +#include +#include +#include +#include + +struct MyData +{ + int x, y, z; + std::string tiles; + + template + serialize(Archive &ar) { ar(x, y, z, tiles); } +}; + + +TEST_CASE("test using serialization", "[config]") { + { + std::ofstream os("cereal.json", std::ios::binary); + cereal::JSONOutputArchive oarchive(os); + + MyData m1{1,2,3, "\u2849█Ω♣"}; + MyData m2{5,6,7, "\u2849█Ω♣"}; + MyData m3{8,9,10, "\u2849█Ω♣"}; + + oarchive(m1, m2, m3); + } + + { + std::ifstream is("cereal.json", std::ios::binary); + cereal::JSONInputArchive iarchive(is); + + MyData m1, m2, m3; + + iarchive(m1, m2, m3); + + REQUIRE(m1.tiles == "\u2849█Ω♣"); + REQUIRE(m2.tiles == "\u2849█Ω♣"); + REQUIRE(m3.tiles == "\u2849█Ω♣"); + } +} diff --git a/wraps/cereal.wrap b/wraps/cereal.wrap new file mode 100644 index 0000000..f0bc435 --- /dev/null +++ b/wraps/cereal.wrap @@ -0,0 +1,12 @@ +[wrap-file] +directory = cereal-1.3.2 +source_url = https://github.com/USCiLab/cereal/archive/v1.3.2.tar.gz +source_filename = cereal-1.3.2.tar.gz +source_hash = 16a7ad9b31ba5880dac55d62b5d6f243c3ebc8d46a3514149e56b5e7ea81f85f +patch_filename = cereal_1.3.2-1_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/cereal_1.3.2-1/get_patch +patch_hash = fd2f047a40a0d291c643fdafe4ce743f0eadbef667b6afe43a332e1ba0862603 + +[provide] +cereal = cereal_dep +