summaryrefslogtreecommitdiffhomepage
path: root/serialize/tile.cpp
blob: 2eb3a81a8a55020823ec9583459fb4b3f0edb7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "src/tile.hpp"
#include "src/chunk.hpp"
#include "serialize/tile.hpp"
#include "serialize/tile-atlas.hpp"
#include "global-coords.hpp"
#include <nlohmann/json.hpp>

namespace floormat {

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile_image, atlas, variant)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile, ground_image, wall_north, wall_west, passability)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords, x, y)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y)

struct global_coords_ final {
    chunk_coords chunk;
    local_coords local;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global_coords_, chunk, local)

} // namespace floormat

namespace nlohmann {

using namespace floormat;

void adl_serializer<tile_image>::to_json(json& j, const tile_image& val) { using nlohmann::to_json; if (val.atlas) to_json(j, val); else j = nullptr; }
void adl_serializer<tile_image>::from_json(const json& j, tile_image& val) { using nlohmann::from_json; from_json(j, val); }

void adl_serializer<tile>::to_json(json& j, const tile& val) { using nlohmann::to_json; to_json(j, val); }
void adl_serializer<tile>::from_json(const json& j, tile& val) { using nlohmann::from_json; from_json(j, val); }

void adl_serializer<chunk>::to_json(json& j, const chunk& val) { using nlohmann::to_json; to_json(j, val.tiles()); }
void adl_serializer<chunk>::from_json(const json& j, chunk& val) { using nlohmann::from_json; from_json(j, val.tiles()); }

void adl_serializer<local_coords>::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, val); }
void adl_serializer<local_coords>::from_json(const json& j, local_coords& val) { using nlohmann::from_json; from_json(j, val); }

void adl_serializer<chunk_coords>::to_json(json& j, const chunk_coords& val) { using nlohmann::to_json; to_json(j, val); }
void adl_serializer<chunk_coords>::from_json(const json& j, chunk_coords& val) { using nlohmann::from_json; from_json(j, val); }

void adl_serializer<global_coords>::to_json(json& j, const global_coords& val) { using nlohmann::to_json; to_json(j, global_coords_{val.chunk(), val.local()}); }
void adl_serializer<global_coords>::from_json(const json& j, global_coords& val) { using nlohmann::from_json; global_coords_ x; from_json(j, x); val = {x.chunk, x.local}; }

} // namespace nlohmann