moved savelevel() code to level.h/c where it belongs to. :)
[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                       badguy_collision(&bad_guys[i], &bad_guys[j], CO_BADGUY);                    
88                     }
89                 }
90             }
91         }
92     }
93
94     /* CO_BADGUY & CO_PLAYER check */
95   for(i = 0; i < num_bad_guys; ++i)
96     {
97       if(bad_guys[i].base.alive)
98         {
99                   if(bad_guys[i].dying == NO && rectcollision_offset(&bad_guys[i].base,&tux.base,0,0) == YES && tux.base.ym > 0)
100                     {
101                     /* We have detected a collision and now call the collision functions of the collided objects. */
102                       badguy_collision(&bad_guys[i], &tux, CO_PLAYER);
103                       }
104                    if(rectcollision(&bad_guys[i].base,&tux.base) == YES)
105                    {
106                       player_collision(&tux, &bad_guys[i], CO_BADGUY);
107                    }
108
109         }
110     }
111
112     /* CO_UPGRADE & CO_PLAYER check */
113   for(i = 0; i < num_upgrades; ++i)
114     {
115       if(upgrades[i].base.alive)
116         {
117                   if(rectcollision(&upgrades[i].base,&tux.base) == YES)
118                     {
119                     /* We have detected a collision and now call the collision functions of the collided objects. */
120                       upgrade_collision(&upgrades[i], &tux, CO_PLAYER);
121                       }
122
123         }
124     }
125     
126 }
127
128