2 // C Implementation: collision
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
14 #include "collision.h"
18 int rectcollision(base_type* one, base_type* two)
21 if (one->x >= two->x - one->width &&
22 one->x <= two->x + two->width &&
23 one->y >= two->y - one->height &&
24 one->y <= two->y + two->height )
34 int rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y)
37 if (one->x >= two->x - one->width +off_x &&
38 one->x <= two->x + two->width + off_x &&
39 one->y >= two->y - one->height + off_y &&
40 one->y <= two->y + two->height + off_y )
50 void collision_handler()
54 /* CO_BULLET & CO_BADGUY check */
55 for(i = 0; i < num_bullets; ++i)
57 if(bullets[i].base.alive)
59 for(j = 0; j < num_bad_guys; ++j)
61 if(bad_guys[j].dying == NO && bad_guys[j].base.alive)
63 if(rectcollision(&bullets[i].base,&bad_guys[j].base) == YES)
65 /* We have detected a collision and now call the collision functions of the collided objects. */
66 bullet_collision(&bullets[i], CO_BADGUY);
67 badguy_collision(&bad_guys[j], &bullets[i], CO_BULLET);
74 /* CO_BADGUY & CO_BADGUY check */
75 for(i = 0; i < num_bad_guys; ++i)
77 if(bad_guys[i].base.alive)
79 for(j = i+1; j < num_bad_guys; ++j)
81 if(j != i && bad_guys[j].base.alive)
83 if(rectcollision(&bad_guys[i].base,&bad_guys[j].base) == YES)
85 /* We have detected a collision and now call the collision functions of the collided objects. */
86 badguy_collision(&bad_guys[j], &bad_guys[i], CO_BADGUY);
87 badguy_collision(&bad_guys[i], &bad_guys[j], CO_BADGUY);
94 /* CO_BADGUY & CO_PLAYER check */
95 for(i = 0; i < num_bad_guys; ++i)
97 if(bad_guys[i].base.alive)
99 if(bad_guys[i].dying == NO && rectcollision_offset(&bad_guys[i].base,&tux.base,0,0) == YES )
101 /* We have detected a collision and now call the collision functions of the collided objects. */
102 if (tux.base.ym > 0 && bad_guys[i].kind != BAD_MONEY)
104 badguy_collision(&bad_guys[i], &tux, CO_PLAYER);
108 player_collision(&tux, &bad_guys[i], CO_BADGUY);
114 /* CO_UPGRADE & CO_PLAYER check */
115 for(i = 0; i < num_upgrades; ++i)
117 if(upgrades[i].base.alive)
119 if(rectcollision(&upgrades[i].base,&tux.base) == YES)
121 /* We have detected a collision and now call the collision functions of the collided objects. */
122 upgrade_collision(&upgrades[i], &tux, CO_PLAYER);