From da63f006c273b7f9f22c81da8ee8404e30c4a04b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 5 Nov 2024 21:33:28 -0500 Subject: [PATCH] Config and save system almost there. --- gui.cpp | 7 +++---- main.cpp | 27 +++++---------------------- save.cpp | 22 ++++++++++++++++++++++ save.hpp | 1 + 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/gui.cpp b/gui.cpp index ca667ee..d977969 100644 --- a/gui.cpp +++ b/gui.cpp @@ -19,6 +19,7 @@ #include "systems.hpp" #include "events.hpp" #include "render.hpp" +#include "save.hpp" using std::string; using namespace fmt; @@ -57,10 +58,8 @@ void GUI::resize_map(int new_size) { } void GUI::save_world() { - tser::BinaryArchive archive; - archive.save($world); - std::string_view archive_view = archive.get_buffer(); - $log.log(format("Game saved! {} bytes.", archive_view.size())); + $log.log("Game saved!"); + save::to_file("./savefile.world", $world); } void GUI::create_renderer() { diff --git a/main.cpp b/main.cpp index f604477..640484c 100644 --- a/main.cpp +++ b/main.cpp @@ -6,8 +6,7 @@ #include "dbc.hpp" #include "collider.hpp" #include "render.hpp" -#include "config.hpp" - +#include "save.hpp" #include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor using namespace ftxui; @@ -25,9 +24,6 @@ void configure_world(DinkyECS::World &world, Map &game_map) { Player player{world.entity()}; world.set_the(player); - spatial_map collider; - world.set_the(collider); - world.set(player.entity, {game_map.place_entity(0)}); world.set(player.entity, {0, 0}); world.set(player.entity, {100, 10}); @@ -55,27 +51,14 @@ void configure_world(DinkyECS::World &world, Map &game_map) { int main() { DinkyECS::World world; - 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(); + save::load_configs(world); configure_world(world, game_map); + + spatial_map collider; + world.set_the(collider); System::init_positions(world); GUI gui(world, game_map); diff --git a/save.cpp b/save.cpp index 59be989..036942b 100644 --- a/save.cpp +++ b/save.cpp @@ -2,6 +2,7 @@ #include #include "dbc.hpp" #include +#include "config.hpp" using namespace components; @@ -62,4 +63,25 @@ void save::from_file(std::string path, DinkyECS::World &world_out) { inject(world_out, save_data.position); inject(world_out, save_data.combat); inject(world_out, save_data.motion); + + save::load_configs(world_out); +} + +void save::load_configs(DinkyECS::World &world) { + 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"] + }); } diff --git a/save.hpp b/save.hpp index 93b9d9f..c7ea42b 100644 --- a/save.hpp +++ b/save.hpp @@ -26,4 +26,5 @@ namespace save { void to_file(std::string path, DinkyECS::World &world); void from_file(std::string path, DinkyECS::World &world_out); + void load_configs(DinkyECS::World &world); }