diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-04-08 14:08:50 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-04-08 14:08:50 +0200 |
commit | 54965220ebe8f0c1cecb6d1cfec856c979aaa92d (patch) | |
tree | 867d5d78313decbfaa2f913b0a2b3e4d54d12292 | |
parent | 0cd263af59f256da2d2f3a77be264d5e64c1ec5d (diff) |
make global_coords ctor less implicit
-rw-r--r-- | editor/draw.cpp | 2 | ||||
-rw-r--r-- | editor/save.cpp | 2 | ||||
-rw-r--r-- | serialize/tile.cpp | 13 | ||||
-rw-r--r-- | src/entity.cpp | 9 | ||||
-rw-r--r-- | src/global-coords.hpp | 4 | ||||
-rw-r--r-- | test/serializer.cpp | 2 |
6 files changed, 15 insertions, 17 deletions
diff --git a/editor/draw.cpp b/editor/draw.cpp index c01c149d..436283e7 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -72,7 +72,7 @@ void app::draw_collision_boxes() auto& world = M->world(); auto& shader = M->shader(); - using rtree_type = std::decay_t<decltype(*world[chunk_coords{}].rtree())>; + using rtree_type = std::decay_t<decltype(*world[chunk_coords_{}].rtree())>; using rect_type = typename rtree_type::Rect; for (int8_t z = z_min; z <= z_max; z++) diff --git a/editor/save.cpp b/editor/save.cpp index c00506a7..6b712589 100644 --- a/editor/save.cpp +++ b/editor/save.cpp @@ -57,7 +57,7 @@ void app::do_new_file() { reset_world(); auto& w = M->world(); - maybe_initialize_chunk_(chunk_coords{}, w[chunk_coords{}]); + maybe_initialize_chunk_(chunk_coords_{}, w[chunk_coords_{}]); } } // namespace floormat diff --git a/serialize/tile.cpp b/serialize/tile.cpp index 61431e61..783a9853 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -2,6 +2,7 @@ #include "src/tile.hpp" #include "src/global-coords.hpp" #include "serialize/tile-atlas.hpp" +#include <tuple> #include <nlohmann/json.hpp> namespace floormat { @@ -19,12 +20,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(local_coords_, x, y) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords, x, y) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(chunk_coords_, x, y, z) -struct global_coords_ final { - chunk_coords chunk; - local_coords local; -}; - -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(global_coords_, chunk, local) +inline void to_json(nlohmann::json& j, global_coords coord) { j = std::tuple<chunk_coords, local_coords, int8_t>{ coord.chunk(), coord.local(), coord.z() }; } +inline void from_json(const nlohmann::json& j, global_coords& coord) { std::tuple<chunk_coords, local_coords, int8_t> t = j; auto [ch, pos, z] = t; coord = { ch, pos, z }; } } // namespace floormat @@ -44,8 +41,8 @@ void adl_serializer<chunk_coords>::from_json(const json& j, chunk_coords& 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}; } +void adl_serializer<global_coords>::to_json(json& j, const global_coords& val) { using nlohmann::to_json; to_json(j, val); } +void adl_serializer<global_coords>::from_json(const json& j, global_coords& val) { using nlohmann::from_json; from_json(j, val); } } // namespace nlohmann diff --git a/src/entity.cpp b/src/entity.cpp index cf7eb662..3f5c67be 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -129,12 +129,13 @@ Pair<global_coords, Vector2b> entity::normalize_coords(global_coords coord, Vect bool entity::can_move_to(Vector2i delta, global_coords coord2, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) { - if (coord2.z() != coord.z()) [[unlikely]] + auto [coord_, offset_] = normalize_coords(coord2, offset, delta); + + if (coord_.z() != coord.z()) [[unlikely]] return false; - auto [coord_, offset_] = normalize_coords(coord2, offset, delta); auto& w = *c->_world; - auto& c_ = coord_.chunk() == coord.chunk() ? *c : w[coord_.chunk()]; + auto& c_ = coord_.chunk() == coord.chunk() ? *c : w[{coord_.chunk(), coord_.z()}]; const auto center = Vector2(coord_.local())*TILE_SIZE2 + Vector2(offset_) + Vector2(bbox_offset), half_bbox = Vector2(bbox_size)*.5f, @@ -194,7 +195,7 @@ bool entity::move_to(size_t& i, Vector2i delta, rotation new_r) else { //fm_debug("change-chunk (%hd;%hd|%hhd;%hhd)", coord_.chunk().x, coord_.chunk().y, coord_.local().x, coord_.local().y); - auto& c2 = w[coord_.chunk()]; + auto& c2 = w[{coord_.chunk(), coord_.z()}]; if (!is_dynamic()) c2.mark_scenery_modified(); c2._add_bbox(bb1); diff --git a/src/global-coords.hpp b/src/global-coords.hpp index 81ed1a70..3e9ac983 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -33,8 +33,8 @@ struct chunk_coords_ final { explicit constexpr operator chunk_coords() const noexcept { return {x, y}; } constexpr chunk_coords_() noexcept = default; - constexpr chunk_coords_(int16_t x, int16_t y, int8_t z = 0) noexcept : x{x}, y{y}, z{z} {} - constexpr chunk_coords_(chunk_coords c, int8_t z = 0) noexcept : x{c.x}, y{c.y}, z{z} {} + constexpr chunk_coords_(int16_t x, int16_t y, int8_t z) noexcept : x{x}, y{y}, z{z} {} + constexpr chunk_coords_(chunk_coords c, int8_t z) noexcept : x{c.x}, y{c.y}, z{z} {} constexpr bool operator==(const chunk_coords_&) const noexcept = default; }; diff --git a/test/serializer.cpp b/test/serializer.cpp index 02e6fdcc..75b0c13c 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -93,7 +93,7 @@ void test_serializer(StringView input, StringView tmp) w = world::deserialize(input); else { - coord = {1, 1}; + coord = {1, 1, 0}; w = world(); make_test_chunk(w, coord); } |