-
Notifications
You must be signed in to change notification settings - Fork 8
/
collision.cpp
executable file
·405 lines (346 loc) · 11.9 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "collision.h"
#include "utils.h"
#include "player_view.h"
#include "platform_data.h"
bool collision_testonepixel_separate(int x1,int y1, BITMAP * bmp1, int x2, int y2, BITMAP * bmp2,PALETTEPTR commonpalette)
{
bool collided = FALSE;
unsigned long address_bmp; // pour le sprite
unsigned char pixelcolor;
bmp_select(bmp2);
address_bmp = bmp_read_line(bmp2, y2); // address ligne y du fond
pixelcolor = bmp_read8(address_bmp+x2); // lit l'octet x dans ligne y
if ( (commonpalette[pixelcolor].r !=0) ||
(commonpalette[pixelcolor].g !=0) ||
(commonpalette[pixelcolor].b !=0))
{
bmp_select(bmp1);
address_bmp = bmp_read_line(bmp1, y1); // address ligne y du fond
pixelcolor = bmp_read8(address_bmp+x1); // lit l'octet x dans ligne y
if ( (commonpalette[pixelcolor].r !=0) ||
(commonpalette[pixelcolor].g !=0) ||
(commonpalette[pixelcolor].b !=0))
collided=TRUE;
}
return collided;
}
bool collision_testonepixel(int x,int y, BITMAP * bmp1, BITMAP * bmp2,PALETTEPTR commonpalette)
{
return collision_testonepixel_separate(x,y,bmp1,x,y,bmp2,commonpalette);
}
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;
BITMAP * little_screen = sprite_buffer_screen();
// 32*32 de back_map_buffer2 ou sera blitté le sprite -> little_screen
blit(pv->back_map_buffer, little_screen, pv->bordersize+pv->w/2, pv->bordersize+pv->h/2, 0, 0, 32, 32);
int y; // loop pour toute les lignes
int x; // loop pour ts les octet ds ligne
int xorig,yorig;
xorig=0;
yorig=0;
int size=31;
while(size>=1)
{
for(x=xorig;x<xorig+size;x++)
{
if (collision_testonepixel(x,yorig,vaisseau->sprite_buffer_rota,little_screen,vaisseau->gfx->sprite_colors))
return TRUE;
}
for(y=yorig;y<yorig+size;y++)
{
if (collision_testonepixel(xorig+size,y,vaisseau->sprite_buffer_rota,little_screen,vaisseau->gfx->sprite_colors))
return TRUE;
}
for(x=xorig+size;x>=xorig+1;x--)
{
if (collision_testonepixel(x,yorig+size,vaisseau->sprite_buffer_rota,little_screen,vaisseau->gfx->sprite_colors))
return TRUE;
}
for(y=yorig+size;y>=yorig+1;y--)
{
if (collision_testonepixel(xorig,y,vaisseau->sprite_buffer_rota,little_screen,vaisseau->gfx->sprite_colors))
return TRUE;
}
xorig+=1;
yorig+=1;
size-=2;
}
return FALSE;
}
bool pixel_collision_detect_inbox(BITMAP *bmp1,int xl1, int yt1,BITMAP *bmp2,int xl2, int yt2,int w, int h,PALETTEPTR commonpalette)
{
int x,y;
for (x=0;x<w;x++)
for(y=0;y<h;y++)
if (collision_testonepixel_separate(xl1+x,yt1+y,bmp1,xl2+x,yt2+y,bmp2,commonpalette))
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,PALETTEPTR commonpalette)
{
// 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->sprite_buffer_rota,xl1,yt1,
vaisseau2->sprite_buffer_rota,xl2,yt2,
w,h,commonpalette);
}
bool testcollision_bullet4pix(BITMAP *bmp,int x,int y)
{
if(x<0 || x>=bmp->w || y<0 || y>=bmp->h) return TRUE;
else
{
unsigned long address_bmp; // pour le sprite
unsigned char pixelcolor;
bmp_select(bmp);
int j;
for(j=y-1;j<=y;j++)
{
address_bmp = bmp_read_line(bmp, j); // address ligne j {y-1,y} du fond
pixelcolor = bmp_read8(address_bmp+x); // lit l'octet x dans ligne y
if (pixelcolor != 0) // i pray that black == 0!
return true;
pixelcolor = bmp_read8(address_bmp+x+1); // lit l'octet x dans ligne y
if (pixelcolor != 0)
return true;
}
}
return false;
}
bool testcollision_bullet1pix(BITMAP *bmp,int x,int y,PALETTEPTR palette)
{
bmp_select(bmp);
int pixel=bmp_read8(bmp_read_line(bmp, y)+x); // lit l'octet x dans ligne y
return palette[pixel].r!=0 || palette[pixel].g!=0 || palette[pixel].b!=0 ;
}
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->sprite_buffer_rota,xtrans,ytrans,v->gfx->sprite_colors);
if (xtrans+1 < 32)
{
if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans-1,v->gfx->sprite_colors);
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans,v->gfx->sprite_colors);
}
else if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
if (b)
{
shoot->free=true;
bool isProtecting=(v->shield && v->shield_force>0);
if (isProtecting && !v->landed)
{
v->impactx=fixtoi(shoot->dx);
v->impacty=fixtoi(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->sprite_buffer_rota,xtrans,ytrans,v->gfx->sprite_colors);
if (xtrans+1 < 32)
{
if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans-1,v->gfx->sprite_colors);
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans,v->gfx->sprite_colors);
}
else if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
if (b)
{
backshoot->free=true;
bool isProtecting=(v->shield && v->shield_force>0);
if (isProtecting && !v->landed)
{
v->impactx=fixtoi(backshoot->dx);
v->impacty=fixtoi(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->sprite_buffer_rota,xtrans,ytrans,v->gfx->sprite_colors);
if (xtrans+1 < 32)
{
if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans-1,v->gfx->sprite_colors);
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans,v->gfx->sprite_colors);
}
else if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
if (b)
{
debris->active=false;
bool isProtecting=(v->shield && v->shield_force>0);
if (isProtecting && !v->landed)
{
v->impactx=fixtoi(debris->vx);
v->impacty=fixtoi(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->sprite_buffer_rota,xtrans,ytrans,v->gfx->sprite_colors);
if (xtrans+1 < 32)
{
if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans-1,v->gfx->sprite_colors);
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans+1,ytrans,v->gfx->sprite_colors);
}
else if (ytrans-1>=0)
{
b |= testcollision_bullet1pix(v->sprite_buffer_rota,xtrans,ytrans-1,v->gfx->sprite_colors);
}
if (b)
{
dca_tir->free=true;
bool isProtecting=(v->shield && v->shield_force>0);
if (isProtecting && !v->landed)
{
v->impactx=fixtoi(dca_tir->dx);
v->impacty=fixtoi(dca_tir->dy);
}
return !isProtecting;
}
}
alldca++;
}
return false;
}