From 7ebee3863c061b1d0b64839b56bbc70ff4e5d924 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 1 Mar 2023 12:32:37 +0100 Subject: move scenery modify logic to scenery_ref --- src/scenery.cpp | 63 +++++++++++++++++++++++++++++++-------------------------- src/scenery.hpp | 10 ++++----- 2 files changed, 39 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/scenery.cpp b/src/scenery.cpp index 7c32248b..86ebea73 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -64,72 +64,77 @@ scenery::scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open, fm_assert(atlas.group(r).frames.size() >= 2); } -void scenery::rotate(rotation new_r) +void scenery_ref::rotate(rotation new_r) { - bbox_offset = rotate_point(bbox_offset, r, new_r); - bbox_size = rotate_size(bbox_size, r, new_r); - r = new_r; + auto& s = frame; + s.bbox_offset = rotate_point(s.bbox_offset, s.r, new_r); + s.bbox_size = rotate_size(s.bbox_size, s.r, new_r); + s.r = new_r; } -bool scenery::can_activate(const anim_atlas&) const noexcept +bool scenery_ref::can_activate() noexcept { - return interactive; + return frame.interactive; } -void scenery::update(float dt, const anim_atlas& anim) +void scenery_ref::update(float dt) { - if (!active) + auto& s = frame; + if (!s.active) return; - switch (type) + switch (s.type) { default: case scenery_type::none: case scenery_type::generic: break; case scenery_type::door: - const auto hz = std::uint8_t(anim.info().fps); + fm_assert(atlas); + auto& anim = *atlas; + const auto hz = std::uint8_t(atlas->info().fps); const auto nframes = (int)anim.info().nframes; fm_debug_assert(anim.info().fps > 0 && anim.info().fps <= 0xff); - auto delta_ = int(delta) + int(65535u * dt); + auto delta_ = int(s.delta) + int(65535u * dt); delta_ = std::min(65535, delta_); const auto frame_time = int(1.f/hz * 65535); const auto n = (std::uint8_t)std::clamp(delta_ / frame_time, 0, 255); - delta = (std::uint16_t)std::clamp(delta_ - frame_time*n, 0, 65535); - fm_debug_assert(delta >= 0); - const std::int8_t dir = closing ? 1 : -1; - const int fr = frame + dir*n; - active = fr > 0 && fr < nframes-1; + s.delta = (std::uint16_t)std::clamp(delta_ - frame_time*n, 0, 65535); + fm_debug_assert(s.delta >= 0); + const std::int8_t dir = s.closing ? 1 : -1; + const int fr = s.frame + dir*n; + s.active = fr > 0 && fr < nframes-1; if (fr <= 0) - passability = pass_mode::pass; + s.passability = pass_mode::pass; else if (fr >= nframes-1) - passability = pass_mode::blocked; + s.passability = pass_mode::blocked; else - passability = pass_mode::see_through; - frame = (frame_t)std::clamp(fr, 0, nframes-1); - if (!active) - delta = closing = 0; + s.passability = pass_mode::see_through; + s.frame = (scenery::frame_t)std::clamp(fr, 0, nframes-1); + if (!s.active) + s.delta = s.closing = 0; break; } } -bool scenery::activate(const anim_atlas& atlas) +bool scenery_ref::activate() { - if (active) + auto& s = frame; + if (!*this || s.active) return false; - switch (type) + switch (s.type) { default: case scenery_type::none: case scenery_type::generic: break; case scenery_type::door: - fm_assert(frame == 0 || frame == atlas.info().nframes-1); - closing = frame == 0; - frame += closing ? 1 : -1; - active = true; + fm_assert(s.frame == 0 || s.frame == atlas->info().nframes-1); + s.closing = s.frame == 0; + s.frame += s.closing ? 1 : -1; + s.active = true; return true; } return false; diff --git a/src/scenery.hpp b/src/scenery.hpp index 5fe2f043..7d2cb502 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -49,11 +49,6 @@ struct scenery final Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size); bool operator==(const scenery&) const noexcept; - - bool can_activate(const anim_atlas& anim) const noexcept; - bool activate(const anim_atlas& atlas); - void update(float dt, const anim_atlas& anim); - void rotate(rotation r); }; constexpr scenery::scenery() noexcept : scenery{scenery::none_tag_t{}} {} @@ -100,6 +95,11 @@ struct scenery_ref final { std::shared_ptr& atlas; scenery& frame; + bool can_activate() noexcept; + bool activate(); + void update(float dt); + void rotate(rotation r); + private: struct chunk* c; std::uint8_t idx; -- cgit v1.2.3