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
|
#include "scenery.hpp"
#include "anim-atlas.hpp"
#include "chunk.hpp"
#include "compat/assert.hpp"
#include "world.hpp"
#include "src/rotation.inl"
#include <algorithm>
namespace floormat {
scenery_proto::scenery_proto()
{
type = entity_type::scenery;
}
scenery_proto& scenery_proto::operator=(const scenery_proto&) = default;
scenery_proto::scenery_proto(const scenery_proto&) = default;
scenery_proto::~scenery_proto() noexcept = default;
scenery_proto::operator bool() const { return atlas != nullptr; }
bool scenery::can_activate(size_t) const
{
return atlas && interactive;
}
entity_update_status scenery::update(size_t, float dt)
{
auto& s = *this;
if (!s.active)
return entity_update_status::not_updated;
switch (s.sc_type)
{
default:
case scenery_type::none:
case scenery_type::generic:
return entity_update_status::not_updated;
case scenery_type::door: {
fm_assert(atlas);
auto& anim = *atlas;
const auto hz = 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 = (uint8_t)std::clamp(delta_ / frame_time, 0, 255);
s.delta = (uint16_t)std::clamp(delta_ - frame_time*n, 0, 65535);
fm_debug_assert(s.delta >= 0);
if (n == 0)
return entity_update_status::not_updated;
const int8_t dir = s.closing ? 1 : -1;
const int fr = s.frame + dir*n;
s.active = fr > 0 && fr < nframes-1;
pass_mode old_pass = pass, p;
if (fr <= 0)
p = pass_mode::pass;
else if (fr >= nframes-1)
p = pass_mode::blocked;
else
p = pass_mode::see_through;
set_bbox(offset, bbox_offset, bbox_size, p);
const auto new_frame = (uint16_t)std::clamp(fr, 0, nframes-1);
//Debug{} << "frame" << new_frame << nframes-1;
s.frame = new_frame;
if (!s.active)
s.delta = s.closing = 0;
//if ((p == pass_mode::pass) != (old_pass == pass_mode::pass)) Debug{} << "update: need reposition" << (s.frame == 0 ? "-1" : "1");
return (p == pass_mode::pass) != (old_pass == pass_mode::pass)
? entity_update_status::updated_repositioning
: entity_update_status::updated;
}
}
}
Vector2 scenery::ordinal_offset(Vector2b offset) const
{
if (sc_type == scenery_type::door)
{
constexpr auto vec0 = Vector2b(-iTILE_SIZE2[0], -iTILE_SIZE[1]/2+1);
constexpr auto vec1 = Vector2b(0, -iTILE_SIZE[1]/2+1);
const auto vec0_ = rotate_point(vec0, rotation::N, r);
const auto vec1_ = rotate_point(vec1, rotation::N, r);
const auto vec = frame == 0 ? vec0_ : vec1_;
return Vector2(offset) + Vector2(vec);
}
else
return Vector2(offset);
}
bool scenery::activate(size_t)
{
auto& s = *this;
if (s.active)
return false;
switch (s.sc_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;
}
bool scenery_proto::operator==(const entity_proto& e0) const
{
if (type != e0.type)
return false;
if (!entity_proto::operator==(e0))
return false;
const auto& s0 = static_cast<const scenery_proto&>(e0);
return sc_type == s0.sc_type && active == s0.active &&
closing == s0.closing && interactive == s0.interactive;
}
entity_type scenery::type() const noexcept { return entity_type::scenery; }
scenery::operator scenery_proto() const
{
scenery_proto ret;
static_cast<entity_proto&>(ret) = entity::operator entity_proto();
ret.sc_type = sc_type;
ret.active = active;
ret.closing = closing;
ret.interactive = interactive;
return ret;
}
scenery::scenery(object_id id, struct chunk& c, const scenery_proto& proto) :
entity{id, c, proto}, sc_type{proto.sc_type}, active{proto.active},
closing{proto.closing}, interactive{proto.interactive}
{
}
} // namespace floormat
|