summaryrefslogtreecommitdiffhomepage
path: root/src/scenery.hpp
blob: 5fe2f0433f35e499524f6e9b4605168ec3dfcd23 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once
#include "pass-mode.hpp"
#include "tile-defs.hpp"
#include "rotation.hpp"
#include <cstdint>
#include <memory>
#include <type_traits>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Magnum.h>

namespace floormat {

struct chunk;
struct anim_atlas;

enum class scenery_type : std::uint8_t {
    none, generic, door,
};

struct scenery final
{
    struct none_tag_t final {};
    struct generic_tag_t final {};
    struct door_tag_t final {};

    static constexpr auto none    = none_tag_t{};
    static constexpr auto generic = generic_tag_t{};
    static constexpr auto door    = door_tag_t{};

    using frame_t = std::uint16_t;

    std::uint16_t delta = 0;
    frame_t frame = 0;
    Vector2b offset, bbox_offset;
    Vector2ub bbox_size{usTILE_SIZE2/2};
    rotation     r           : 3 = rotation::N;
    scenery_type type        : 3 = scenery_type::none;
    pass_mode    passability : 2 = pass_mode::shoot_through;
    std::uint8_t active      : 1 = false;
    std::uint8_t closing     : 1 = false;
    std::uint8_t interactive : 1 = false;

    constexpr scenery() noexcept;
    constexpr scenery(none_tag_t) noexcept;
    scenery(generic_tag_t, const anim_atlas& atlas, rotation r, frame_t frame,
            pass_mode passability, bool active, bool interactive,
            Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size);
    scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open,
            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{}} {}
constexpr scenery::scenery(none_tag_t) noexcept {}

struct scenery_proto final
{
    std::shared_ptr<anim_atlas> atlas;
    scenery frame;

    scenery_proto() noexcept;
    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;
};

struct scenery_ref final {
    scenery_ref(struct chunk& c, std::size_t i) noexcept;
    scenery_ref(const scenery_ref&) noexcept;
    scenery_ref(scenery_ref&&) noexcept;
    scenery_ref& operator=(const scenery_proto& proto) noexcept;

    operator scenery_proto() const noexcept;
    operator bool() const noexcept;

    struct chunk& chunk() noexcept;
    std::uint8_t index() const noexcept;

    template<std::size_t N> std::tuple_element_t<N, scenery_ref>& get() & { if constexpr(N == 0) return atlas; else return frame; }
    template<std::size_t N> std::tuple_element_t<N, scenery_ref>& get() && { if constexpr(N == 0) return atlas; else return frame; }

    std::shared_ptr<anim_atlas>& atlas;
    scenery& frame;

private:
    struct chunk* c;
    std::uint8_t idx;
};

} // namespace floormat

template<> struct std::tuple_size<floormat::scenery_ref> final : std::integral_constant<std::size_t, 2> {};
template<> struct std::tuple_element<0, floormat::scenery_ref> final { using type = std::shared_ptr<floormat::anim_atlas>; };
template<> struct std::tuple_element<1, floormat::scenery_ref> final { using type = floormat::scenery; };