diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-27 17:27:56 +0100 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-27 17:27:56 +0100 |
| commit | beb6685f605b89ce4b2dd99f87b9319e4f7f4d94 (patch) | |
| tree | b4db6babfa2a7c5518db59e1678ef4e434ae0d70 | |
| parent | d2fcaad5425acb4a50138e793e3dcecc6600e4d3 (diff) | |
more WIP
| -rw-r--r-- | compat/prelude.hpp | 11 | ||||
| -rw-r--r-- | editor/scenery-editor.cpp | 30 | ||||
| -rw-r--r-- | editor/scenery-editor.hpp | 15 | ||||
| -rw-r--r-- | editor/update.cpp | 6 | ||||
| -rw-r--r-- | entity/types.hpp | 5 | ||||
| -rw-r--r-- | entity/util.hpp | 5 | ||||
| -rw-r--r-- | loader/loader.hpp | 3 | ||||
| -rw-r--r-- | serialize/binary-serializer.hpp | 7 | ||||
| -rw-r--r-- | serialize/scenery.cpp | 52 | ||||
| -rw-r--r-- | serialize/scenery.hpp | 23 | ||||
| -rw-r--r-- | src/scenery.cpp | 50 | ||||
| -rw-r--r-- | src/scenery.hpp | 29 |
12 files changed, 116 insertions, 120 deletions
diff --git a/compat/prelude.hpp b/compat/prelude.hpp index 3be26e01..30273a31 100644 --- a/compat/prelude.hpp +++ b/compat/prelude.hpp @@ -10,8 +10,17 @@ typedef unsigned int size_t; typedef __SIZE_TYPE__ size_t; #endif +namespace Corrade::Containers { + +class String; +template<typename T> class BasicStringView; +using StringView = BasicStringView<const char>; + +template<typename T> class ArrayView; + +} // namespace Corrade::Containers + namespace Corrade::Containers::Literals {} -namespace Corrade::Containers {} namespace Corrade::Utility::Path {} namespace Corrade::Utility { class Debug; class Error; } namespace Magnum::Math::Literals {} diff --git a/editor/scenery-editor.cpp b/editor/scenery-editor.cpp index 482b2131..c8f9f910 100644 --- a/editor/scenery-editor.cpp +++ b/editor/scenery-editor.cpp @@ -8,8 +8,6 @@ namespace floormat { using rotation_ = rotation; using rotation_t = std::underlying_type_t<rotation_>; -scenery_editor::pair::operator bool() const { return atlas != nullptr; } - scenery_editor::scenery_editor() noexcept { load_atlases(); @@ -17,45 +15,45 @@ scenery_editor::scenery_editor() noexcept void scenery_editor::set_rotation(rotation_ r) { - _selected.r = r; + _selected.frame.r = r; } rotation_ scenery_editor::rotation() const { - return _selected.r; + return _selected.frame.r; } void scenery_editor::next_rotation() { - auto r_1 = (rotation_t)_selected.r + 1; + // todo + auto r_1 = (rotation_t)_selected.frame.r + 1; auto rot = (rotation_)r_1; if (rot >= rotation_COUNT) rot = (rotation_)0; - _selected.r = rot; + _selected.frame.r = rot; } void scenery_editor::prev_rotation() { - if (_selected.r == (rotation_)0) - _selected.r = (rotation_)((rotation_t)rotation_COUNT - 1); + if (_selected.frame.r == (rotation_)0) + _selected.frame.r = (rotation_)((rotation_t)rotation_COUNT - 1); else - _selected.r = (rotation_)((rotation_t)_selected.r - 1); + _selected.frame.r = (rotation_)((rotation_t)_selected.frame.r - 1); } //return atlas == _selected.atlas && r == _selected.s.r && frame == _selected.s.frame; -void scenery_editor::select_tile(const std::shared_ptr<anim_atlas>& atlas, rotation_ r, frame_t frame) +void scenery_editor::select_tile(const scenery_proto& proto) { - fm_assert(frame < atlas->group(r).frames.size()); - _selected = { atlas, r, frame }; + _selected = proto; } void scenery_editor::clear_selection() { - _selected = { nullptr, rotation_COUNT, scenery::NO_FRAME }; + _selected = {}; } -auto scenery_editor::get_selected() -> pair +const scenery_proto& scenery_editor::get_selected() { return _selected; } @@ -65,9 +63,9 @@ bool scenery_editor::is_atlas_selected(const std::shared_ptr<anim_atlas>& atlas) return _selected.atlas == atlas; } -bool scenery_editor::is_item_selected(const std::shared_ptr<anim_atlas>& atlas, rotation_ r, frame_t frame) const +bool scenery_editor::is_item_selected(const scenery_proto& proto) const { - return is_atlas_selected(atlas) && _selected.r == r && _selected.frame == frame; + return _selected == proto; } void scenery_editor::load_atlases() diff --git a/editor/scenery-editor.hpp b/editor/scenery-editor.hpp index 85ccd3be..b461fd0c 100644 --- a/editor/scenery-editor.hpp +++ b/editor/scenery-editor.hpp @@ -12,13 +12,6 @@ struct scenery_editor final { using frame_t = scenery::frame_t; - struct pair final { - std::shared_ptr<anim_atlas> atlas; - enum rotation r = rotation_COUNT; - frame_t frame = scenery::NO_FRAME; - operator bool() const; - }; - scenery_editor() noexcept; void set_rotation(enum rotation r); @@ -26,17 +19,17 @@ struct scenery_editor final void next_rotation(); void prev_rotation(); - void select_tile(const std::shared_ptr<anim_atlas>& atlas, enum rotation r, frame_t frame); + void select_tile(const scenery_proto& proto); void clear_selection(); - pair get_selected(); + const scenery_proto& get_selected(); bool is_atlas_selected(const std::shared_ptr<anim_atlas>& atlas) const; - bool is_item_selected(const std::shared_ptr<anim_atlas>& atlas, enum rotation r, frame_t frame) const; + bool is_item_selected(const scenery_proto& proto) const; private: void load_atlases(); std::map<StringView, std::shared_ptr<anim_atlas>> _atlases; - pair _selected; + scenery_proto _selected; }; } // namespace floormat diff --git a/editor/update.cpp b/editor/update.cpp index 0c347f1d..f2ac1bc6 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -30,9 +30,9 @@ void app::maybe_initialize_chunk_(const chunk_coords& pos, chunk& c) c[{K, K }].wall_west() = { _wall2, 0 }; c[{K, K+1}].wall_north() = { _wall1, 0 }; c[{K+1, K }].wall_west() = { _wall2, 0 }; - c[{K+3, K+1}].scenery() = { scenery::door, rotation::N, _door, false }; - c[{ 3, 4 }].scenery() = { scenery::generic, rotation::W, _table }; - c[{K, K+1}].scenery() = { scenery::generic, rotation::N, _control_panel, true }; + c[{K+3, K+1}].scenery() = { scenery::door, _door, rotation::N, }; + c[{ 3, 4 }].scenery() = { scenery::generic, _table, rotation::W, }; + c[{K, K+1}].scenery() = { scenery::generic, _control_panel, rotation::N, scenery::frame_t{0}, true }; c.mark_modified(); } diff --git a/entity/types.hpp b/entity/types.hpp index 055f6de0..2987f2e8 100644 --- a/entity/types.hpp +++ b/entity/types.hpp @@ -2,11 +2,6 @@ #include <cstdint> #include <type_traits> -namespace Corrade::Containers { -template<typename T> class BasicStringView; -using StringView = BasicStringView<const char>; -} // namespace Corrade::Containers - namespace floormat::entities { enum class erased_field_type : unsigned { diff --git a/entity/util.hpp b/entity/util.hpp index 3643866d..d2b32634 100644 --- a/entity/util.hpp +++ b/entity/util.hpp @@ -1,11 +1,6 @@ #pragma once #include <type_traits> -namespace Corrade::Containers { -template<typename T> class BasicStringView; -using StringView = BasicStringView<const char>; -} // namespace Corrade::Containers - namespace floormat::entities { template<typename T, typename = void> struct pass_by_value : std::bool_constant<std::is_fundamental_v<T>> {}; diff --git a/loader/loader.hpp b/loader/loader.hpp index 3320b839..e3f306ea 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -1,9 +1,6 @@ #pragma once #include <memory> -namespace Corrade::Containers { template<typename T> class ArrayView; class String; } -namespace Corrade::Containers { template<typename T> class BasicStringView; using StringView = BasicStringView<const char>; } -namespace Magnum::Math { template<class T> class Vector2; } namespace Magnum { using Vector2ub = Math::Vector2<unsigned char>; } namespace floormat { diff --git a/serialize/binary-serializer.hpp b/serialize/binary-serializer.hpp index 3d39880c..057b3c4d 100644 --- a/serialize/binary-serializer.hpp +++ b/serialize/binary-serializer.hpp @@ -6,13 +6,6 @@ #include <concepts> #include <type_traits> -namespace Corrade::Containers { - -template<typename T> class BasicStringView; -using StringView = BasicStringView<const char>; - -} // namespace Corrade::Containers - namespace floormat::Serialize { static_assert(std::endian::native == std::endian::big || std::endian::native == std::endian::little); diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index b2490739..8bff3d69 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -18,7 +18,7 @@ constexpr struct { { scenery_type::none, "none"_s }, { scenery_type::generic, "generic"_s }, { scenery_type::door, "door"_s }, - { scenery_type::object, "object"_s }, + //{ scenery_type::object, "object"_s }, }; constexpr struct { @@ -87,34 +87,50 @@ void adl_serializer<scenery_proto>::to_json(json& j, const scenery_proto& val) j["type"] = f.type; fm_assert(val.atlas); j["atlas-name"] = val.atlas->name(); - fm_assert(f.frame != scenery::NO_FRAME); j["frame"] = f.frame; j["rotation"] = f.r; j["passable"] = f.passable; j["blocks-view"] = f.blocks_view; j["active"] = f.active; + j["animated"] = f.animated; + j["interactive"] = f.interactive; } void adl_serializer<scenery_proto>::from_json(const json& j, scenery_proto& val) { - auto& f = val.frame; - f.type = j["type"]; + const auto get = [&](const StringView& name, auto& value) + { + auto s = std::string_view{name.data(), name.size()}; + if (j.contains(s)) + value = j[s]; + }; + StringView atlas_name = j["atlas-name"]; - val.atlas = loader.anim_atlas(atlas_name); + fm_assert(!atlas_name.isEmpty()); + auto atlas = loader.anim_atlas(atlas_name); + auto& f = val.frame; f = {}; - if (j.contains("frame")) - f.frame = j["frame"]; - if (j.contains("animated")) - f.animated = j["animated"]; - fm_assert(f.animated == (f.frame == scenery::NO_FRAME)); - if (j.contains("rotation")) - f.r = j["rotation"]; - if (j.contains("passable")) - f.passable = j["passable"]; - if (j.contains("blocks-view")) - f.blocks_view = j["blocks-view"]; - if (j.contains("active")) - f.active = j["active"]; + + auto type = scenery_type::generic; get("type", type); + auto frame = f.frame; get("frame", frame); + auto r = f.r; get("rotation", r); + bool passable = f.passable; get("passable", passable); + bool blocks_view = f.blocks_view; get("blocks-view", blocks_view); + bool active = f.active; get("active", active); + bool animated = f.animated; get("animated", animated); + + switch (f.type) + { + default: + fm_abort("unhandled scenery type '%hhu'", f.type); + case scenery_type::generic: + f = { scenery::generic, *atlas, r, frame, passable, blocks_view, animated, active }; + break; + case scenery_type::door: + f = { scenery::door, *atlas, r, false }; + } } + + } // namespace nlohmann diff --git a/serialize/scenery.hpp b/serialize/scenery.hpp index e7946913..190a4d2a 100644 --- a/serialize/scenery.hpp +++ b/serialize/scenery.hpp @@ -1,9 +1,20 @@ #pragma once #include "src/scenery.hpp" -#include <Corrade/Containers/StringView.h> #include <vector> +#include <Corrade/Containers/StringView.h> #include <nlohmann/json_fwd.hpp> +namespace floormat::Serialize { + +struct serialized_scenery final { + StringView name, descr; + scenery_proto proto; + + static std::vector<serialized_scenery> deserialize(StringView filename); +}; + +} // namespace floormat::Serialize + namespace nlohmann { template<> struct adl_serializer<floormat::scenery_type> { @@ -19,13 +30,11 @@ template<> struct adl_serializer<floormat::rotation> { template<> struct adl_serializer<floormat::scenery_proto> { static void to_json(json& j, const floormat::scenery_proto& val); static void from_json(const json& j, floormat::scenery_proto& val); +}; - struct item { - Corrade::Containers::StringView name, description; - floormat::scenery_proto proto; - }; - - static std::vector<item> deserialize_list(Corrade::Containers::StringView file); +template<> struct adl_serializer<floormat::Serialize::serialized_scenery> { + static void to_json(json& j, const floormat::Serialize::serialized_scenery& val); + static void from_json(const json& j, floormat::Serialize::serialized_scenery& val); }; } // namespace nlohmann diff --git a/src/scenery.cpp b/src/scenery.cpp index 03adc39e..dfb72b65 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -5,22 +5,13 @@ namespace floormat { -scenery_proto::scenery_proto() noexcept : scenery_proto{scenery::none} {} -scenery_proto::scenery_proto(scenery::none_tag_t) noexcept : frame{scenery::none} {} -scenery_proto::scenery_proto(const std::shared_ptr<anim_atlas>& atlas, const scenery& frame) : +scenery_proto::scenery_proto() noexcept = default; +scenery_proto::scenery_proto(const std::shared_ptr<anim_atlas>& atlas, const scenery& frame) noexcept : atlas{atlas}, frame{frame} {} -scenery_proto::scenery_proto(scenery::generic_tag_t, rotation r, const std::shared_ptr<anim_atlas>& atlas, bool passable, scenery::frame_t frame) : - atlas{atlas}, frame{scenery::generic, r, *atlas, frame, passable} -{} - -scenery_proto::scenery_proto(scenery::door_tag_t, rotation r, const std::shared_ptr<anim_atlas>& atlas, bool is_open) : - atlas{atlas}, frame{scenery::door, r, *atlas, is_open} -{} - scenery_proto& scenery_proto::operator=(const scenery_proto&) noexcept = default; - +scenery_proto::scenery_proto(const scenery_proto&) noexcept = default; scenery_proto::operator bool() const noexcept { return atlas != nullptr; } scenery_ref::scenery_ref(std::shared_ptr<anim_atlas>& atlas, scenery& frame) noexcept : atlas{atlas}, frame{frame} {} @@ -39,36 +30,27 @@ scenery_ref::operator bool() const noexcept { return atlas != nullptr; } scenery::scenery() noexcept : scenery{none_tag_t{}} {} scenery::scenery(none_tag_t) noexcept : passable{true} {} -scenery::scenery(generic_tag_t, rotation r, const anim_atlas& atlas, frame_t frame, - bool passable, bool blocks_view, bool animated, bool active) : - frame{frame}, r{r}, passable{passable}, - blocks_view{blocks_view}, active{active}, animated{animated}, - type{scenery_type::generic} +scenery::scenery(generic_tag_t, const anim_atlas& atlas, rotation r, frame_t frame, + bool passable, bool blocks_view, bool animated, bool active, bool interactive) : + frame{frame}, r{r}, type{scenery_type::generic}, + passable{passable}, blocks_view{blocks_view}, active{active}, animated{animated}, + interactive{interactive} { + fm_assert(r < rotation_COUNT); fm_assert(frame < atlas.group(r).frames.size()); } -scenery::scenery(door_tag_t, rotation r, const anim_atlas& atlas, bool is_open) : +scenery::scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open) : frame{frame_t(is_open ? 0 : atlas.group(r).frames.size()-1)}, - r{r}, - passable{is_open}, blocks_view{!is_open}, type{scenery_type::door} -{} + r{r}, type{scenery_type::door}, + passable{is_open}, blocks_view{!is_open}, interactive{true} +{ + fm_assert(r < rotation_COUNT); +} bool scenery::can_activate() const noexcept { -#if 0 - return true; -#else - switch (type) - { - default: - return false; - case scenery_type::object: - return true; - case scenery_type::door: - return !active; - } -#endif + return interactive; } void scenery::update(float dt, const anim_atlas& anim) diff --git a/src/scenery.hpp b/src/scenery.hpp index c1e2b5ea..d34067d9 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -1,6 +1,7 @@ #pragma once #include <cstdint> #include <memory> +#include <type_traits> namespace floormat { @@ -13,7 +14,7 @@ enum class rotation : std::uint8_t { constexpr inline rotation rotation_COUNT = rotation{8}; enum class scenery_type : std::uint8_t { - none, generic, door, object, + none, generic, door, }; struct scenery final @@ -22,7 +23,6 @@ struct scenery final struct generic_tag_t final {}; struct door_tag_t final {}; - static constexpr auto NO_FRAME = (std::uint16_t)-1; static constexpr inline auto none = none_tag_t{}; static constexpr inline auto generic = generic_tag_t{}; static constexpr inline auto door = door_tag_t{}; @@ -30,19 +30,20 @@ struct scenery final using frame_t = std::uint16_t; float delta = 0; - frame_t frame = NO_FRAME; + frame_t frame = 0; rotation r : 3 = rotation::N; + scenery_type type : 3 = scenery_type::none; std::uint8_t passable : 1 = false; std::uint8_t blocks_view : 1 = false; // todo std::uint8_t active : 1 = false; std::uint8_t closing : 1 = false; std::uint8_t animated : 1 = false; // todo - scenery_type type : 3 = scenery_type::none; + std::uint8_t interactive : 1 = false; scenery() noexcept; scenery(none_tag_t) noexcept; - scenery(generic_tag_t, rotation r, const anim_atlas& atlas, frame_t frame = 0, bool passable = false, bool blocks_view = false, bool animated = false, bool active = false); - scenery(door_tag_t, rotation r, const anim_atlas& atlas, bool is_open = false); + scenery(generic_tag_t, const anim_atlas& atlas, rotation r, frame_t frame = 0, bool passable = false, bool blocks_view = false, bool animated = false, bool active = false, bool interactive = false); + scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open = false); bool can_activate() const noexcept; bool activate(const anim_atlas& atlas); @@ -54,11 +55,19 @@ struct scenery_proto final { scenery frame; scenery_proto() noexcept; - explicit scenery_proto(scenery::none_tag_t) noexcept; - scenery_proto(const std::shared_ptr<anim_atlas>& atlas, const scenery& frame); - scenery_proto(scenery::generic_tag_t, rotation r, const std::shared_ptr<anim_atlas>& atlas, bool passable = false, scenery::frame_t frame = 0); - scenery_proto(scenery::door_tag_t, rotation r, const std::shared_ptr<anim_atlas>& atlas, bool is_open = false); + scenery_proto(const std::shared_ptr<anim_atlas>& atlas, const scenery& frame) noexcept; scenery_proto& operator=(const scenery_proto&) noexcept; + scenery_proto(const scenery_proto&) noexcept; + + template<typename... Ts> + scenery_proto(scenery::generic_tag_t, const std::shared_ptr<anim_atlas>& atlas, Ts&&... args) : + atlas{atlas}, frame{scenery::generic, *atlas, std::forward<Ts>(args)...} + {} + + template<typename... Ts> + scenery_proto(scenery::door_tag_t, const std::shared_ptr<anim_atlas>& atlas, Ts&&... args) : + atlas{atlas}, frame{scenery::door, *atlas, std::forward<Ts>(args)...} + {} operator bool() const noexcept; }; |
