forked from devpack/mayhem
-
Notifications
You must be signed in to change notification settings - Fork 2
/
collision.cpp
executable file
·337 lines (288 loc) · 10.5 KB
/
collision.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "collision.h"
#include "platform_data.h"
#include "player_view.h"
#include "utils.h"
#include "allegro_compatibility.h"
bool test_collision(struct player_view *pv, struct level_data *currentlevel) {
struct vaisseau_data *vaisseau = pv->player->ship;
struct platform_data *plt;
bool test_it = true;
for (int i = 0; i < currentlevel->nbplatforms; i++) {
plt = ¤tlevel->platformdata[i];
int xmin = plt->xmin - 9; // centrage
int xmax = plt->xmax - 23; // centrage
int yflat = plt->yflat - 29;
if ((xmin <= vaisseau->xpos) && (vaisseau->xpos <= xmax) &&
((vaisseau->ypos == yflat) || ((vaisseau->ypos - 1) == yflat) ||
((vaisseau->ypos - 2) == yflat) || ((vaisseau->ypos - 3) == yflat)) &&
((vaisseau->angle & 0xff) <= 12 || (vaisseau->angle & 0xff) >= 243))
test_it = false;
if (vaisseau->shield && (xmin <= vaisseau->xpos) &&
(vaisseau->xpos <= xmax) &&
((vaisseau->ypos == yflat) || ((vaisseau->ypos - 1) == yflat) ||
((vaisseau->ypos - 2) == yflat) || ((vaisseau->ypos - 3) == yflat) ||
((vaisseau->ypos + 1) == yflat)) &&
((vaisseau->angle & 0xff) <= 12 || (vaisseau->angle & 0xff) >= 243))
test_it = false;
if ((vaisseau->thrust) && (xmin <= vaisseau->xpos) &&
(vaisseau->xpos <= xmax) &&
((vaisseau->ypos == yflat) || ((vaisseau->ypos - 1) == yflat) ||
((vaisseau->ypos + 1) == yflat)))
test_it = false;
}
if (!test_it)
return test_it;
constexpr int size = 31;
int num_frames = vaisseau->coll_map.num_frames;
double frame = vaisseau->angle / vaisseau->anglestep;
assert(frame >= 0 && frame < num_frames);
int y_max = currentlevel->coll_map.height;
int x_max = currentlevel->coll_map.width;
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
int x_pos = vaisseau->xpos + x;
int y_pos = vaisseau->ypos + y;
bool inside = x_pos >= 0 && x_pos < x_max && y_pos >= 0 && y_pos < y_max;
if (inside && currentlevel->coll_map.is_collide_pixel(x_pos, y_pos, 0) &&
vaisseau->coll_map.is_collide_pixel(x, y, frame)) {
if (currentlevel->wall_collision) {
return true;
} else {
bounce_vaisseau(vaisseau);
return false;
}
}
}
}
return false;
}
void bounce_vaisseau(struct vaisseau_data *vaisseau) {
vaisseau->ax = (0);
vaisseau->ay = (0);
vaisseau->vx = -vaisseau->vx / 2;
vaisseau->vy = -vaisseau->vy / 2;
}
bool pixel_collision_detect_inbox(struct vaisseau_data *vaisseau1, int xl1,
int yt1, struct vaisseau_data *vaisseau2,
int xl2, int yt2, int w, int h) {
int x, y;
int frame0 = vaisseau1->angle / vaisseau1->anglestep;
int frame1 = vaisseau2->angle / vaisseau2->anglestep;
for (x = 0; x < w; x++) {
for (y = 0; y < h; y++) {
if (vaisseau1->coll_map.is_collide_pixel(xl1 + x, yt1 + y, frame0) &&
vaisseau2->coll_map.is_collide_pixel(xl2 + x, yt2 + y, frame1)) {
return true;
}
}
}
return false;
}
//
// Implementation de la detection des collisions entre vaisseaux
//�@First we look for a bounding box, if none, sure, there are no collision
// If there is, we find the limit of the bounding box and
// we pixel iterate through it...
//
bool test_collision_ship2ship(struct vaisseau_data *vaisseau1,
struct vaisseau_data *vaisseau2) {
// first find the bouding box
int xl1, xl2, yt1, yt2;
int w, h;
if (vaisseau1->xpos + 32 >= vaisseau2->xpos &&
vaisseau1->xpos <= vaisseau2->xpos) {
xl2 = 0;
xl1 = vaisseau2->xpos - vaisseau1->xpos;
w = vaisseau1->xpos + 32 - vaisseau2->xpos;
} else if (vaisseau2->xpos + 32 >= vaisseau1->xpos &&
vaisseau2->xpos <= vaisseau1->xpos) {
xl1 = 0;
xl2 = vaisseau1->xpos - vaisseau2->xpos;
w = vaisseau2->xpos + 32 - vaisseau1->xpos;
} else
return false;
if (vaisseau1->ypos + 32 >= vaisseau2->ypos &&
vaisseau1->ypos <= vaisseau2->ypos) {
yt2 = 0;
yt1 = vaisseau2->ypos - vaisseau1->ypos;
h = vaisseau1->ypos + 32 - vaisseau2->ypos;
} else if (vaisseau2->ypos + 32 >= vaisseau1->ypos &&
vaisseau2->ypos <= vaisseau1->ypos) {
yt1 = 0;
yt2 = vaisseau1->ypos - vaisseau2->ypos;
h = vaisseau2->ypos + 32 - vaisseau1->ypos;
} else
return false;
// if we arrive here we might have a bounding box
// with collision
return pixel_collision_detect_inbox(vaisseau1, xl1, yt1, vaisseau2, xl2, yt2,
w, h);
}
bool testcollision_bullet4pix(collision_map &coll_map, int x, int y, int w,
int h) {
if (x < 0 || (x + 1) >= w || (y - 1) < 0 || y >= h)
return true;
else {
unsigned long address_bmp; // pour le sprite
unsigned char pixelcolor;
int j;
for (j = y - 1; j <= y; j++) {
if (coll_map.is_collide_pixel(x, j, 0) ||
coll_map.is_collide_pixel(x + 1, j, 0)) {
return true;
}
}
}
return false;
}
bool testcollision_bullet1pix(vaisseau_data *v, int x, int y) {
return v->coll_map.is_collide_pixel(x, y, v->angle / v->anglestep);
}
bool collision_tir_ship(struct vaisseau_data *v, struct vaisseau_data *allv,
int nombre_vaisseau) {
struct tir_data *shoot;
while (nombre_vaisseau--) {
for (int i = 0; i < MAX_TIR; i++) {
shoot = &allv->tir[i];
if (shoot->free)
continue; // only iterate through the non free shoot
int xtrans = shoot->x - v->xpos;
int ytrans = shoot->y - v->ypos;
// if outside of the bounding box
if (xtrans < 0 || xtrans >= 32 || ytrans < 0 || ytrans >= 32)
continue;
bool b = testcollision_bullet1pix(v, xtrans, ytrans);
if (xtrans + 1 < 32) {
if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans - 1);
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans);
} else if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
if (b) {
shoot->free = true;
bool isProtecting = (v->shield && v->shield_force > 0);
if (isProtecting && !v->landed) {
v->impactx = shoot->dx;
v->impacty = shoot->dy;
}
return !isProtecting;
}
}
allv++;
}
return false;
}
bool collision_backtir_ship(struct vaisseau_data *v, struct vaisseau_data *allv,
int nombre_vaisseau) {
struct tir_data *backshoot;
while (nombre_vaisseau--) {
for (int i = 0; i < MAX_TIR; i++) {
backshoot = &allv->backtir[i];
if (backshoot->free)
continue; // only iterate through the non free shoot
int xtrans = backshoot->x - v->xpos;
int ytrans = backshoot->y - v->ypos;
// if outside of the bounding box
if (xtrans < 0 || xtrans >= 32 || ytrans < 0 || ytrans >= 32)
continue;
bool b = testcollision_bullet1pix(v, xtrans, ytrans);
if (xtrans + 1 < 32) {
if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans - 1);
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans);
} else if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
if (b) {
backshoot->free = true;
bool isProtecting = (v->shield && v->shield_force > 0);
if (isProtecting && !v->landed) {
v->impactx = backshoot->dx;
v->impacty = backshoot->dy;
}
return !isProtecting;
}
}
allv++;
}
return false;
}
bool collision_debris_ship(struct vaisseau_data *v, struct vaisseau_data *allv,
int nombre_vaisseau) {
struct debris_data *debris;
while (nombre_vaisseau--) {
for (int i = 0; i < 8; i++) {
debris = &allv->debris[i];
if (!debris->active)
continue;
int xtrans = debris->x - v->xpos;
int ytrans = debris->y - v->ypos;
// if outside of the bounding box
if (xtrans < 0 || xtrans >= 32 || ytrans < 0 || ytrans >= 32)
continue;
bool b = testcollision_bullet1pix(v, xtrans, ytrans);
if (xtrans + 1 < 32) {
if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans - 1);
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans);
} else if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
if (b) {
debris->active = false;
bool isProtecting = (v->shield && v->shield_force > 0);
if (isProtecting && !v->landed) {
v->impactx = (debris->vx);
v->impacty = (debris->vy);
}
return !isProtecting;
}
}
allv++;
}
return false;
}
bool collision_dca_ship(struct vaisseau_data *v, struct dca_data *alldca,
int nb_dca) {
struct tir_data *dca_tir;
while (nb_dca--) {
for (int i = 0; i < MAX_DCA_TIR; i++) {
dca_tir = &alldca->dca_tir[i];
if (dca_tir->free)
continue;
int xtrans = dca_tir->x - v->xpos;
int ytrans = dca_tir->y - v->ypos;
// if outside of the bounding box
if (xtrans < 0 || xtrans >= 32 || ytrans < 0 || ytrans >= 32)
continue;
bool b = testcollision_bullet1pix(v, xtrans, ytrans);
if (xtrans + 1 < 32) {
if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans - 1);
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
b |= testcollision_bullet1pix(v, xtrans + 1, ytrans);
} else if (ytrans - 1 >= 0) {
b |= testcollision_bullet1pix(v, xtrans, ytrans - 1);
}
if (b) {
dca_tir->free = true;
bool isProtecting = (v->shield && v->shield_force > 0);
if (isProtecting && !v->landed) {
v->impactx = (dca_tir->dx);
v->impacty = (dca_tir->dy);
}
return !isProtecting;
}
}
alldca++;
}
return false;
}