From 960e346159dbf152d9847f0998e1e717fb7dbfef Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 5 Dec 2022 06:30:59 +0100 Subject: src: add pass_mode field to tile_atlas --- serialize/tile-atlas.cpp | 37 +++++++++++++++++++++++++++++-------- serialize/world-reader.cpp | 4 +++- 2 files changed, 32 insertions(+), 9 deletions(-) (limited to 'serialize') diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index a92dfc28..6a51da4c 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -3,12 +3,25 @@ #include "serialize/corrade-string.hpp" #include "serialize/magnum-vector2i.hpp" #include "loader/loader.hpp" -#include - +#include "serialize/pass-mode.hpp" +#include +#include #include using namespace floormat; +namespace { + +struct proxy { + String name; + Vector2ub size; + Optional passability; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(proxy, name, size) + +} // namespace + namespace nlohmann { void adl_serializer>::to_json(json& j, const std::shared_ptr& x) @@ -17,18 +30,26 @@ void adl_serializer>::to_json(json& j, const std::sh if (!x) j = nullptr; else - to_json(j, std::tuple{x->name(), x->num_tiles2()}); + { + to_json(j, proxy{x->name(), x->num_tiles2(), NullOpt}); + if (auto p = x->pass_mode()) + j["pass-mode"] = *p; + } } -void adl_serializer>::from_json(const json& j, std::shared_ptr& x) +void adl_serializer>::from_json(const json& j, std::shared_ptr& val) { if (j.is_null()) - x = nullptr; + val = nullptr; else { - std::tuple proxy = j; - const auto& [name, num_tiles] = proxy; - x = loader.tile_atlas(name, num_tiles); + using nlohmann::from_json; + proxy x; + from_json(j, x); + Optional p; + if (j.contains("pass-mode")) + p = {InPlaceInit, j["pass-mode"]}; + val = loader.tile_atlas(x.name, x.size, p); } } diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index b0e14254..98704599 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -45,7 +45,9 @@ void reader_state::read_atlases(reader_t& s) size[0] << s; size[1] << s; const auto& [buf, len] = s.read_asciiz_string(); - atlases.push_back(loader.tile_atlas({buf, len}, size)); + auto atlas = loader.tile_atlas({buf, len}); + fm_soft_assert(size == atlas->num_tiles2()); + atlases.push_back(std::move(atlas)); } } -- cgit v1.2.3