Skip to content

Commit

Permalink
add to_json && version field #6
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen committed Jan 5, 2023
1 parent bea7f1c commit ebaaf6d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 15 deletions.
1 change: 1 addition & 0 deletions resources/data/ExampleMap.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": "0.1-DEV",
"world": {
"width": 50,
"height": 35
Expand Down
2 changes: 1 addition & 1 deletion resources/data/NewWorld.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/data/Savegame.json

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/world/Island.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,17 @@ void mdcii::world::Island::CreateLayersFromJson(const nlohmann::json& t_json)
gridLayer->PrepareCpuDataForRendering();
gridLayer->PrepareGpuDataForRendering();
}

//-------------------------------------------------
// Json
//-------------------------------------------------

void mdcii::world::to_json(nlohmann::json& t_json, const Island& t_island)
{
t_json = nlohmann::json{
{ "width", t_island.width },
{ "height", t_island.height },
{ "x", t_island.startWorldX },
{ "y", t_island.startWorldY }
};
}
6 changes: 6 additions & 0 deletions src/world/Island.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,10 @@ namespace mdcii::world
*/
void CreateLayersFromJson(const nlohmann::json& t_json);
};

//-------------------------------------------------
// Json
//-------------------------------------------------

void to_json(nlohmann::json& t_json, const Island& t_island);
}
17 changes: 17 additions & 0 deletions src/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ void mdcii::world::World::Init()
nlohmann::json j = read_json_from_file(Game::RESOURCES_REL_PATH + m_mapFilePath);
for (const auto& [k, v] : j.items())
{
if (k == "version" && v.get<std::string>() != Game::VERSION)
{
throw MDCII_EXCEPTION("[World::Init()] Invalid map file format.");
}

if (k == "world")
{
width = v.at("width").get<int32_t>();
Expand Down Expand Up @@ -456,3 +461,15 @@ void mdcii::world::World::CleanUp() const
event::EventManager::event_dispatcher.removeListener(event::MdciiEventType::MOUSE_BUTTON_PRESSED, m_mouseButtonPressed);
event::EventManager::event_dispatcher.removeListener(event::MdciiEventType::MOUSE_MOVED, m_mouseMoved);
}

//-------------------------------------------------
// Json
//-------------------------------------------------

void mdcii::world::to_json(nlohmann::json& t_json, const World& t_world)
{
t_json = nlohmann::json{
{ "width", t_world.width },
{ "height", t_world.height }
};
}
7 changes: 7 additions & 0 deletions src/world/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <magic_enum.hpp>
#include <glm/vec2.hpp>
#include "event/EventManager.h"
#include "data/json.hpp"

//-------------------------------------------------
// Forward declarations
Expand Down Expand Up @@ -436,4 +437,10 @@ namespace mdcii::world
*/
void CleanUp() const;
};

//-------------------------------------------------
// Json
//-------------------------------------------------

void to_json(nlohmann::json& t_json, const World& t_world);
}
21 changes: 8 additions & 13 deletions src/world/WorldGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,19 @@ void mdcii::world::WorldGui::SaveGameGui()
throw MDCII_EXCEPTION("[WorldGui::SaveGameGui()] Error while opening file " + fileName + ".");
}

// write world
// todo: to_json && meta data
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();
// version
json["version"] = Game::VERSION;

// world
json["world"] = *m_world;

// 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 = *island;
islandJson["layers"] = nlohmann::json::array();

// coast
Expand Down

0 comments on commit ebaaf6d

Please sign in to comment.