673d05d9b8d88d872e0641a6ce7f9a50dbbf33bc
[supertux.git] / src / collision.c
1 //
2 // C Implementation: collision
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "defines.h"
14 #include "collision.h"
15 #include "bitmask.h"
16 #include "scene.h"
17
18 int rectcollision(base_type* one, base_type* two)
19 {
20
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 )
25     {
26       return YES;
27     }
28   else
29     {
30       return NO;
31     }
32 }
33
34 int rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y)
35 {
36
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 )
41     {
42       return YES;
43     }
44   else
45     {
46       return NO;
47     }
48 }
49
50 void collision_handler()
51 {
52   int i,j;
53
54   /* CO_BULLET & CO_BADGUY check */
55   for(i = 0; i < num_bullets; ++i)
56     {
57       if(bullets[i].base.alive)
58         {
59           for(j = 0; j < num_bad_guys; ++j)
60             {
61               if(bad_guys[j].dying == NO && bad_guys[j].base.alive)
62                 {
63                   if(rectcollision(&bullets[i].base,&bad_guys[j].base) == YES)
64                     {
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);
68                     }
69                 }
70             }
71         }
72     }
73     
74     /* CO_BADGUY & CO_BADGUY check */
75   for(i = 0; i < num_bad_guys; ++i)
76     {
77       if(bad_guys[i].base.alive)
78         {
79           for(j = i+1; j < num_bad_guys; ++j)
80             {
81               if(j != i && bad_guys[j].base.alive)
82                 {
83                   if(rectcollision(&bad_guys[i].base,&bad_guys[j].base) == YES)
84                     {
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                     }
88                 }
89             }
90         }
91     }
92
93     /* CO_BADGUY & CO_PLAYER check */
94   for(i = 0; i < num_bad_guys; ++i)
95     {
96       if(bad_guys[i].base.alive)
97         {
98                   if(bad_guys[i].dying == NO && rectcollision_offset(&bad_guys[i].base,&tux.base,0,0) == YES && tux.base.ym > 0)
99                     {
100                     /* We have detected a collision and now call the collision functions of the collided objects. */
101                       badguy_collision(&bad_guys[i], &tux, CO_PLAYER);
102                       }
103                    if(rectcollision(&bad_guys[i].base,&tux.base) == YES)
104                    {
105                       player_collision(&tux, &bad_guys[i], CO_BADGUY);
106                    }
107
108         }
109     }
110
111     /* CO_UPGRADE & CO_PLAYER check */
112   for(i = 0; i < num_upgrades; ++i)
113     {
114       if(upgrades[i].base.alive)
115         {
116                   if(rectcollision(&upgrades[i].base,&tux.base) == YES)
117                     {
118                     /* We have detected a collision and now call the collision functions of the collided objects. */
119                       upgrade_collision(&upgrades[i], &tux, CO_PLAYER);
120                       }
121
122         }
123     }
124     
125 }
126
127