Skip to content

Commit

Permalink
a savegame can be created and loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen committed Jan 3, 2023
1 parent 9c8e254 commit 8ea3dab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions resources/data/Savegame.json

Large diffs are not rendered by default.

58 changes: 44 additions & 14 deletions src/world/WorldGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "state/State.h"
#include "data/Text.h"
#include "file/OriginalResourcesManager.h"
#include "layer/TerrainLayer.h"

//-------------------------------------------------
// Ctors. / Dtor.
Expand Down Expand Up @@ -211,26 +212,55 @@ void mdcii::world::WorldGui::SaveGameGui()
{
Log::MDCII_LOG_DEBUG("[WorldGui::SaveGameGui()] Start saving the game in file {}...", fileName);

nlohmann::json j;
nlohmann::json t = nlohmann::json::object();
nlohmann::json b = nlohmann::json::object();
nlohmann::json json;

std::ofstream file{ fileName };
if (!file)
{
throw MDCII_EXCEPTION("[WorldGui::SaveGameGui()] Error while opening file " + fileName + ".");
}

/*
j["width"] = m_world->width;
j["height"] = m_world->height;
j["layers"] = nlohmann::json::array();
t["terrain"] = m_world->GetLayer(WorldLayerType::TERRAIN).tiles;
b["buildings"] = m_world->GetLayer(WorldLayerType::BUILDINGS).tiles;
j["layers"].push_back(t);
j["layers"].push_back(b);
*/

file << j;
// write world
auto worldJson = nlohmann::json::object();
worldJson["width"] = m_world->width;
worldJson["height"] = m_world->height;
json["world"] = worldJson;

// write islands
auto islandsJson = nlohmann::json::array();

for (const auto& island : m_world->terrain->islands)
{
// island
auto islandJson = nlohmann::json::object();
islandJson["width"] = island->width;
islandJson["height"] = island->height;
islandJson["x"] = island->startWorldX;
islandJson["y"] = island->startWorldY;
islandJson["layers"] = nlohmann::json::array();

// coast
auto c = nlohmann::json::object();
c["coast"] = island->coastLayer->tiles;

// terrain
auto t = nlohmann::json::object();
t["terrain"] = island->terrainLayer->tiles;

// buildings
auto b = nlohmann::json::object();
b["buildings"] = island->buildingsLayer->tiles;

islandJson["layers"].push_back(c);
islandJson["layers"].push_back(t);
islandJson["layers"].push_back(b);

islandsJson.push_back(islandJson);
}

json["islands"] = islandsJson;

file << json;

Log::MDCII_LOG_DEBUG("[WorldGui::SaveGameGui()] The game has been successfully saved in file {}.", fileName);
}
Expand Down

0 comments on commit 8ea3dab

Please sign in to comment.