Huge code merge. This reflects the current status of my rewrite/restructuring. A...
[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(itop_type* one, itop_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(itop_type* one, itop_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_rect_detect(int co_one, int co_two)
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].alive)
58         {
59           for(j = 0; j < NUM_BAD_GUYS; ++j)
60             {
61               if(bad_guys[j].alive)
62                 {
63                   if(rectcollision(&bullets[i].it,&bad_guys[j].it) == 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
75 void collision_handler()
76 {
77   int i,j;
78
79   /* CO_BULLET & CO_BADGUY check */
80   for(i = 0; i < NUM_BULLETS; ++i)
81     {
82       if(bullets[i].alive)
83         {
84           for(j = 0; j < NUM_BAD_GUYS; ++j)
85             {
86               if(bad_guys[j].alive)
87                 {
88                   if(rectcollision(&bullets[i].it,&bad_guys[j].it) == YES)
89                     {
90                     /* We have detected a collision and now call the collision functions of the collided objects. */
91                       bullet_collision(&bullets[i], CO_BADGUY);
92                       badguy_collision(&bad_guys[j], &bullets[i], CO_BULLET);
93                     }
94                 }
95             }
96         }
97     }
98     
99     /* CO_BADGUY & CO_BADGUY check */
100   for(i = 0; i < NUM_BAD_GUYS; ++i)
101     {
102       if(bad_guys[i].alive)
103         {
104           for(j = i+1; j < NUM_BAD_GUYS; ++j)
105             {
106               if(j != i && bad_guys[j].alive)
107                 {
108                   if(rectcollision(&bad_guys[i].it,&bad_guys[j].it) == YES)
109                     {
110                     /* We have detected a collision and now call the collision functions of the collided objects. */
111                       badguy_collision(&bad_guys[j], &bad_guys[i], CO_BADGUY);
112                     }
113                 }
114             }
115         }
116     }
117
118     /* CO_BADGUY & CO_PLAYER check */
119   for(i = 0; i < NUM_BAD_GUYS; ++i)
120     {
121       if(bad_guys[i].alive)
122         {
123                   if(rectcollision_offset(&bad_guys[i].it,&tux.it,0,48) == YES && tux.ym < 0)
124                     {
125                     /* We have detected a collision and now call the collision functions of the collided objects. */
126                       badguy_collision(&bad_guys[i], &tux, CO_PLAYER);
127                       }
128                    if(rectcollision(&bad_guys[i].it,&tux.it) == YES)
129                    {
130                       player_collision(&tux, &bad_guys[i], CO_BADGUY);
131                    }
132
133         }
134     }
135
136     /* CO_UPGRADE & CO_PLAYER check */
137   for(i = 0; i < NUM_UPGRADES; ++i)
138     {
139       if(upgrades[i].alive)
140         {
141                   if(rectcollision(&upgrades[i].it,&tux.it) == YES)
142                     {
143                     /* We have detected a collision and now call the collision functions of the collided objects. */
144                       upgrade_collision(&upgrades[i], &tux, CO_PLAYER);
145                       }
146
147         }
148     }
149     
150 }
151
152