diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-03 13:35:13 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-03 13:35:13 -0400 |
commit | 752438f0be9e4940556ebcc633165cd0b9c17c6f (patch) | |
tree | 47edbcd5eaabf58d483abb77bf5f946e7eaf2532 | |
parent | 6441c3e2081abe085b98578b67253f0f3ade0ca2 (diff) |
json save and load, made GameState class
-rw-r--r-- | src/engine.cpp | 15 | ||||
-rw-r--r-- | src/gamestate.hpp | 96 |
2 files changed, 101 insertions, 10 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index afd325d..9c0b09b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -20,6 +20,7 @@ */ #include "engine.hpp" +#include "gamestate.hpp" #include "gamerun.hpp" #include "input.hpp" #include "player.hpp" @@ -45,7 +46,9 @@ int Engine::init(void) systems.add<ScriptSystem>(); systems.configure(); + // Load game script and entity data systems.system<ScriptSystem>()->init(); + GameState::load("save.json", entities); return 0; } @@ -119,16 +122,8 @@ void Engine::run(void) // Done, bring logic thread back logicThread.join(); - std::ofstream saveFile ("save.json"); - cereal::JSONOutputArchive archive (saveFile); - std::string name ("entity"); - int i = 0; - for (entityx::Entity e : entities.entities_for_debugging()) { - archive.setNextName((name + std::to_string(i++)).c_str()); - archive.startNode(); - entities.entity_serialize(e, true, archive); - archive.finishNode(); - } + // Save the entities' data + GameState::save("save.json", entities); } bool Engine::shouldRun(void) diff --git a/src/gamestate.hpp b/src/gamestate.hpp new file mode 100644 index 0000000..196c6b7 --- /dev/null +++ b/src/gamestate.hpp @@ -0,0 +1,96 @@ +/** + * @file gamestate.hpp + * Provides functionality to load and save entity data to or from JSON files. + * + * Copyright (C) 2019 Clyne Sullivan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef GAMELOAD_HPP_ +#define GAMELOAD_HPP_ + +#include <cereal/cereal.hpp> +#include <cereal/archives/json.hpp> +#include <entityx/entityx.h> + +#include <fstream> + +/** + * @class GameState + * Manages save files that contain entity data. + */ +class GameState +{ +public: + /** + * Loads entity data from a JSON file into entities contained in the + * entity manager. + * + * The order of entities and the components they have must match between + * those in the manager and those in the file. + * + * @param file The file to load from + * @param entities The entity manager to load into + */ + static void load(const std::string& file, entityx::EntityManager &entities) + { + if (std::ifstream saveFile (file); saveFile.good()) { + cereal::JSONInputArchive archive (saveFile); + serializeEntities(archive, false, entities); + } + } + + /** + * Saves entity data from the entities in the provided manager into a JSON + * file, + * + * @param file The file to load from + * @param entities The entity manager to get entity data from + */ + static void save(const std::string& file, entityx::EntityManager &entities) + { + if (std::ofstream saveFile (file); saveFile.good()) { + cereal::JSONOutputArchive archive (saveFile); + serializeEntities(archive, true, entities); + } + } + +private: + /** + * Calls the serialization funciton for each entity in the given entity + * manager. + * @param archive The archive to save/load with (cereal::JSON...) + * @param save True to save data, false to load + * @param entities The entity manager to iterate through + */ + template<class Archive> + static void serializeEntities(Archive& archive, + bool save, + entityx::EntityManager& entities) + { + std::string name ("entity"); + int i = 0; + + for (auto entity : entities.entities_for_debugging()) { + archive.setNextName((name + std::to_string(i++)).c_str()); + archive.startNode(); + entities.entity_serialize(entity, save, archive); + archive.finishNode(); + } + } +}; + +#endif // GAMELOAD_HPP_ + |