-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.cc
223 lines (158 loc) · 6.95 KB
/
object.cc
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "object.h"
namespace Engine {
std::unordered_set<const Object *> Object::invalid{ nullptr };
std::set<Object *> Object::marked{};
void Object::delayedDestroy (void) {
std::set<Object *> swapper;
while (!Object::marked.empty()) {
swapper.clear();
Object::marked.swap(swapper);
for (auto obj : swapper) {
if (Object::isValid(obj, false)) {
obj->beforeDestroy();
obj->removeParent();
for (const auto &child : obj->children) {
child->destroy();
}
obj->children.clear();
Object::invalid.insert(obj);
obj->afterDestroy();
delete obj;
}
}
}
}
void Object::move (float_max_t delta_time, bool collision_detect) {
if (collision_detect) {
static std::list<Object *> moving;
for (auto &child : this->children) {
if (child->isMoving()) {
if (child->collides()) {
moving.push_back(child);
} else {
child->setPosition(child->getPosition() + child->getSpeed() * delta_time);
}
}
}
if (!moving.empty()) {
constexpr unsigned collision_samples = 4;
const float_max_t multiplier = delta_time / static_cast<float_max_t>(collision_samples);
Spatial::Vec<3> point;
static std::unordered_map<const Object *, std::unordered_set<const Object *>> collided;
for (unsigned i = 0; i < collision_samples; ++i) {
for (auto child_it = moving.begin(), child_end = moving.end(); child_it != child_end; ) {
auto &child = *child_it;
bool valid = Object::isValid(child) && child->collides();
if (valid) {
const Spatial::Vec<3> delta_speed = child->getSpeed() * multiplier;
for (auto &other : this->children) {
if (
child != other &&
Object::isValid(other) &&
other->collides() &&
!collided[child].count(other) &&
child->detectCollision(other, delta_speed, other->getSpeed() * multiplier, point)
) {
child->onCollision(other, point);
if (Object::isValid(other)) {
other->onCollision(child, point);
}
if (!(Object::isValid(child) && child->collides())) {
valid = false;
collided[child].clear();
break;
} else {
collided[other].insert(child);
}
}
}
}
if (valid) {
child_it++;
} else {
child->setPosition(child->getPosition() + child->getSpeed() * static_cast<float_max_t>(collision_samples - i) * multiplier);
child_it = moving.erase(child_it);
}
}
for (auto &child : moving) {
child->setPosition(child->getPosition() + child->getSpeed() * multiplier);
}
collided.clear();
}
}
moving.clear();
} else {
for (auto &child : this->children) {
child->setPosition(child->getPosition() + child->getSpeed() * delta_time);
}
}
}
void Object::update (float_max_t now, float_max_t delta_time, unsigned tick, bool collision_detect) {
static bool destroy_shared = true;
const bool destroy_local = destroy_shared;
destroy_shared = false;
if (Object::isValid(this)) {
this->beforeUpdate(now, delta_time, tick);
this->move(delta_time, collision_detect);
this->setSpeed(this->getSpeed() + this->acceleration * delta_time);
for (auto &child : this->children) {
child->update(now, delta_time, tick, collision_detect);
}
this->afterUpdate(now, delta_time, tick);
}
if (destroy_local) {
destroy_shared = true;
Object::delayedDestroy();
}
}
void Object::alwaysUpdate (float_max_t now, float_max_t delta_time, unsigned tick, bool collision_detect) {
if (Object::isValid(this)) {
this->beforeAlwaysUpdate(now, delta_time, tick);
for (auto &child : this->children) {
child->alwaysUpdate(now, delta_time, tick, collision_detect);
}
this->afterAlwaysUpdate(now, delta_time, tick);
}
}
void Object::draw (bool only_border) const {
if (Object::isValid(this)) {
if (this->display) {
Mesh *mesh = this->getMesh();
Draw::push();
Draw::translate(this->getPosition());
Draw::rotate(this->getOrientation());
// Shader::push(this->shader);
this->beforeDraw(only_border);
if (mesh) {
mesh->draw(only_border);
}
for (const auto &child : this->getChildren()) {
child->draw(only_border);
}
this->afterDraw(only_border);
// Shader::pop();
Draw::pop();
}
}
}
void Object::debugInfo (std::ostream &out, const std::string shift) const {
if (Object::isValid(this)) {
Mesh *mesh = this->getMesh();
std::string next_shift = shift + ' ';
out << shift << "Type: " << this->getType() << std::endl;
out << shift << "Position: " << this->getPosition() << std::endl;
if (mesh) {
out << shift << "Mesh:" << std::endl;
mesh->debugInfo(out, next_shift);
}
out << shift << "Speed: " << this->getSpeed() << std::endl;
if (!this->getChildren().empty()) {
out << shift << "Children:" << std::endl;
for (auto &child : this->getChildren()) {
child->debugInfo(out, next_shift);
}
}
out << std::endl;
}
}
};