From b2ed598c1f3fc5e8ffd624323ac0956f74810f09 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 6 Nov 2024 15:06:10 -0500 Subject: [PATCH] Kind of working save now, but does have problems with dead things. --- components.hpp | 2 +- main.cpp | 15 ++++++++++++--- save.cpp | 2 ++ save.hpp | 3 ++- tests/save.cpp | 9 +++++++-- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/components.hpp b/components.hpp index ab74de0..eb49511 100644 --- a/components.hpp +++ b/components.hpp @@ -28,7 +28,7 @@ namespace components { }; struct Tile { - std::string chr = "!"; + std::string chr; DEFINE_SERIALIZABLE(Tile, chr); }; diff --git a/main.cpp b/main.cpp index 640484c..f4c4e4d 100644 --- a/main.cpp +++ b/main.cpp @@ -8,8 +8,10 @@ #include "render.hpp" #include "save.hpp" #include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor +#include using namespace ftxui; +namespace fs = std::filesystem; /* * This needs to be turned into a real world generator @@ -48,14 +50,21 @@ void configure_world(DinkyECS::World &world, Map &game_map) { } -int main() { +int main(int argc, char *argv[]) { DinkyECS::World world; Map game_map(GAME_MAP_X, GAME_MAP_Y); - game_map.generate(); save::load_configs(world); - configure_world(world, game_map); + + if(argc == 2) { + fmt::println("Loading save file {}", argv[1]); + fs::path save_path{argv[1]}; + save::from_file(save_path, world, game_map); + } else { + game_map.generate(); + configure_world(world, game_map); + } spatial_map collider; world.set_the(collider); diff --git a/save.cpp b/save.cpp index 831db50..2d2b06c 100644 --- a/save.cpp +++ b/save.cpp @@ -26,6 +26,7 @@ void save::to_file(fs::path path, DinkyECS::World &world, Map &map) { extract(world, save_data.position); extract(world, save_data.combat); extract(world, save_data.motion); + extract(world, save_data.tile); archive.save(save_data); std::string_view archive_view = archive.get_buffer(); @@ -66,6 +67,7 @@ void save::from_file(fs::path path, DinkyECS::World &world_out, Map &map_out) { inject(world_out, save_data.position); inject(world_out, save_data.combat); inject(world_out, save_data.motion); + inject(world_out, save_data.tile); map_out = Map(save_data.map.input_map, save_data.map.walls, save_data.map.limit); diff --git a/save.hpp b/save.hpp index cdaac04..2b93b22 100644 --- a/save.hpp +++ b/save.hpp @@ -32,8 +32,9 @@ namespace save { std::map position; std::map motion; std::map combat; + std::map tile; - DEFINE_SERIALIZABLE(SaveData, facts, map, position, motion, combat); + DEFINE_SERIALIZABLE(SaveData, facts, map, position, motion, combat, tile); }; void to_file(fs::path path, DinkyECS::World &world, Map &map); diff --git a/tests/save.cpp b/tests/save.cpp index 09726af..91250fe 100644 --- a/tests/save.cpp +++ b/tests/save.cpp @@ -66,6 +66,7 @@ TEST_CASE("basic save a world", "[save]") { world.set(player.entity, {10,10}); world.set(player.entity, {0, 0}); world.set(player.entity, {100, 10}); + world.set(player.entity, {"@"}); save::to_file("./savetest.world", world, map); @@ -78,15 +79,19 @@ TEST_CASE("basic save a world", "[save]") { REQUIRE(position1.location.x == position2.location.x); REQUIRE(position1.location.y == position2.location.y); - Combat &combat1 = in_world.get(player.entity); + Combat &combat1 = world.get(player.entity); Combat &combat2 = in_world.get(player.entity); REQUIRE(combat1.hp == combat2.hp); - Motion &motion1 = in_world.get(player.entity); + Motion &motion1 = world.get(player.entity); Motion &motion2 = in_world.get(player.entity); REQUIRE(motion1.dx == motion2.dx); REQUIRE(motion1.dy == motion2.dy); + Tile &tile1 = world.get(player.entity); + Tile &tile2 = in_world.get(player.entity); + REQUIRE(tile1.chr == tile2.chr); + REQUIRE(map.width() == in_map.width()); REQUIRE(map.height() == in_map.height()); REQUIRE(map.$walls == in_map.$walls);