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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include "scenery.hpp"
#include "anim-atlas.hpp"
#include "chunk.hpp"
#include "compat/assert.hpp"
#include "rotation.inl"
#include <algorithm>
namespace floormat {
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::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(struct chunk& c, std::size_t i) noexcept :
atlas{c.scenery_atlas_at(i)}, frame{c.scenery_at(i)},
c{&c}, idx{std::uint8_t(i)}
{}
scenery_ref::scenery_ref(const scenery_ref&) noexcept = default;
scenery_ref::scenery_ref(scenery_ref&&) noexcept = default;
struct chunk& scenery_ref::chunk() noexcept { return *c; }
std::uint8_t scenery_ref::index() const noexcept { return idx; }
scenery_ref& scenery_ref::operator=(const scenery_proto& proto) noexcept
{
atlas = proto.atlas;
frame = proto.frame;
return *this;
}
scenery_ref::operator scenery_proto() const noexcept { return { atlas, frame }; }
scenery_ref::operator bool() const noexcept { return atlas != nullptr; }
scenery::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) :
frame{frame},
offset{offset},
bbox_offset{rotate_point(bbox_offset, atlas.first_rotation(), r)},
bbox_size{rotate_size(bbox_size, atlas.first_rotation(), r)},
r{r}, type{scenery_type::generic},
passability{passability},
active{active}, interactive{interactive}
{
fm_assert(r < rotation_COUNT);
fm_assert(frame < atlas.group(r).frames.size());
}
scenery::scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open,
Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) :
frame{frame_t(is_open ? 0 : atlas.group(r).frames.size()-1)},
offset{offset},
bbox_offset{rotate_point(bbox_offset, atlas.first_rotation(), r)},
bbox_size{rotate_size(bbox_size, atlas.first_rotation(), r)},
r{r}, type{scenery_type::door},
passability{is_open ? pass_mode::pass : pass_mode::blocked},
interactive{true}
{
fm_assert(r < rotation_COUNT);
fm_assert(atlas.group(r).frames.size() >= 2);
}
void scenery_ref::rotate(rotation new_r)
{
c->with_scenery_bbox_update(idx, [&] {
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_ref::can_activate() const noexcept
{
return frame.interactive;
}
void scenery_ref::update(float dt)
{
auto& s = frame;
if (!s.active)
return;
switch (s.type)
{
default:
case scenery_type::none:
case scenery_type::generic:
break;
case scenery_type::door:
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(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);
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)
s.passability = pass_mode::pass;
else if (fr >= nframes-1)
s.passability = pass_mode::blocked;
else
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_ref::activate()
{
auto& s = frame;
if (!*this || s.active)
return false;
switch (s.type)
{
default:
case scenery_type::none:
case scenery_type::generic:
break;
case scenery_type::door:
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;
}
#ifdef __GNUG__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
bool scenery::operator==(const scenery&) const noexcept = default;
#ifdef __GNUG__
#pragma GCC diagnostic pop
#endif
} // namespace floormat
|