ea6f6fc2189ba4745a448ed4ba13b7a9b46d73e2
[supertux.git] / src / special.c
1 //
2 // C Implementation: special
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de> & Bill Kendrick, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "SDL.h"
14 #include "defines.h"
15 #include "special.h"
16 #include "gameloop.h"
17 #include "screen.h"
18 #include "sound.h"
19 #include "scene.h"
20 #include "globals.h"
21 #include "player.h"
22
23 void create_special_bitmasks()
24 {
25   bm_bullet = bitmask_create_SDL(img_bullet.sdl_surface);
26 }
27
28 void bullet_init(bullet_type* pbullet, float x, float y, float xm, int dir)
29 {
30   pbullet->base.width = 4;
31   pbullet->base.height = 4;
32   pbullet->base.alive = YES;
33
34   if (dir == RIGHT)
35     {
36       pbullet->base.x = x + 32;
37       pbullet->base.xm = BULLET_XM + xm;
38     }
39   else
40     {
41       pbullet->base.x = x;
42       pbullet->base.xm = -BULLET_XM + xm;
43     }
44
45   pbullet->base.y = y;
46   pbullet->base.ym = BULLET_STARTING_YM;
47 }
48
49 void bullet_action(bullet_type* pbullet)
50 {
51   if (pbullet->base.alive)
52     {
53       pbullet->base.x = pbullet->base.x + pbullet->base.xm * frame_ratio;
54       pbullet->base.y = pbullet->base.y + pbullet->base.ym * frame_ratio;
55
56       if (issolid(pbullet->base.x, pbullet->base.y))
57         {
58           if (issolid(pbullet->base.x, pbullet->base.y - pbullet->base.ym))
59             pbullet->base.alive = NO;
60           else
61             {
62               if (pbullet->base.ym >= 0)
63                 {
64                   pbullet->base.y = (int)(pbullet->base.y / 32) * 32 - 8;
65                 }
66               pbullet->base.ym = -pbullet->base.ym;
67             }
68         }
69
70       pbullet->base.ym = pbullet->base.ym + GRAVITY;
71
72       if (pbullet->base.x < scroll_x ||
73           pbullet->base.x > scroll_x + screen->w)
74         {
75           pbullet->base.alive = NO;
76         }
77     }
78
79 }
80
81 void bullet_draw(bullet_type* pbullet)
82 {
83   if (pbullet->base.alive  &&
84       pbullet->base.x >= scroll_x - pbullet->base.width &&
85       pbullet->base.x <= scroll_x + screen->w)
86     {
87       texture_draw(&img_bullet, pbullet->base.x - scroll_x, pbullet->base.y,
88                    NO_UPDATE);
89     }
90 }
91
92 void bullet_collision(bullet_type* pbullet, int c_object)
93 {
94
95   if(c_object == CO_BADGUY)
96     pbullet->base.alive = NO;
97
98 }
99
100 void upgrade_init(upgrade_type *pupgrade, float x, float y, int kind)
101 {
102   pupgrade->base.width = 32;
103   pupgrade->base.height = 0;
104   pupgrade->base.alive = YES;
105   pupgrade->kind = kind;
106   pupgrade->base.x = x;
107   pupgrade->base.y = y;
108   pupgrade->base.xm = 2;
109   pupgrade->base.ym = -2;
110   pupgrade->base.height = 0;
111 }
112
113 void upgrade_action(upgrade_type *pupgrade)
114 {
115
116   if (pupgrade->base.alive)
117     {
118       if (pupgrade->base.height < 32)
119         {
120           /* Rise up! */
121
122           pupgrade->base.height = pupgrade->base.height + 0.7 * frame_ratio;
123           if(pupgrade->base.height > 32)
124           pupgrade->base.height = 32;
125         }
126       else
127         {
128           /* Move around? */
129
130           if (pupgrade->kind == UPGRADE_MINTS ||
131               pupgrade->kind == UPGRADE_HERRING)
132             {
133               pupgrade->base.x = pupgrade->base.x + pupgrade->base.xm * frame_ratio;
134               pupgrade->base.y = pupgrade->base.y + pupgrade->base.ym * frame_ratio;
135
136               if (issolid(pupgrade->base.x, pupgrade->base.y + 31) ||
137                   issolid(pupgrade->base.x + 31, pupgrade->base.y + 31))
138                 {
139                   if (pupgrade->base.ym > 0)
140                     {
141                       if (pupgrade->kind == UPGRADE_MINTS)
142                         {
143                           pupgrade->base.ym = 0;
144                         }
145                       else if (pupgrade->kind == UPGRADE_HERRING)
146                         {
147                           pupgrade->base.ym = -8;
148                         }
149
150                       pupgrade->base.y = (int)(pupgrade->base.y / 32) * 32;
151                     }
152                 }
153               else
154                 pupgrade->base.ym = pupgrade->base.ym + GRAVITY;
155
156               if (issolid(pupgrade->base.x, pupgrade->base.y))
157                 {
158                   pupgrade->base.xm = -pupgrade->base.xm;
159                 }
160             }
161
162
163           /* Off the screen?  Kill it! */
164
165           if (pupgrade->base.x < scroll_x - pupgrade->base.width)
166             pupgrade->base.alive = NO;
167
168         }
169     }
170 }
171
172 void upgrade_draw(upgrade_type* pupgrade)
173 {
174   if (pupgrade->base.alive)
175     {
176       if (pupgrade->base.height < 32)
177         {
178           /* Rising up... */
179
180           dest.x = pupgrade->base.x - scroll_x;
181           dest.y = pupgrade->base.y + 32 - pupgrade->base.height;
182           dest.w = 32;
183           dest.h = pupgrade->base.height;
184
185           if (pupgrade->kind == UPGRADE_MINTS)
186             texture_draw_part(&img_mints,0,0,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
187           else if (pupgrade->kind == UPGRADE_COFFEE)
188             texture_draw_part(&img_coffee,0,0,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
189           else if (pupgrade->kind == UPGRADE_HERRING)
190             texture_draw_part(&img_golden_herring,0,0,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
191         }
192       else
193         {
194           if (pupgrade->kind == UPGRADE_MINTS)
195             {
196               texture_draw(&img_mints,
197                            pupgrade->base.x - scroll_x, pupgrade->base.y,
198                            NO_UPDATE);
199             }
200           else if (pupgrade->kind == UPGRADE_COFFEE)
201             {
202               texture_draw(&img_coffee,
203                            pupgrade->base.x - scroll_x, pupgrade->base.y,
204                            NO_UPDATE);
205             }
206           else if (pupgrade->kind == UPGRADE_HERRING)
207             {
208               texture_draw(&img_golden_herring,
209                            pupgrade->base.x - scroll_x, pupgrade->base.y,
210                            NO_UPDATE);
211             }
212         }
213     }
214 }
215
216 void upgrade_collision(upgrade_type* pupgrade, void* p_c_object, int c_object)
217 {
218   player_type* pplayer = NULL;
219
220   switch (c_object)
221     {
222     case CO_PLAYER:
223       /* Remove the upgrade: */
224
225       /* p_c_object is CO_PLAYER, so assign it to pplayer */
226       pplayer = p_c_object;
227
228       pupgrade->base.alive = NO;
229
230       /* Affect the player: */
231
232       if (pupgrade->kind == UPGRADE_MINTS)
233         {
234           play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
235           pplayer->size = BIG;
236           timer_start(&super_bkgd_timer, 350);
237         }
238       else if (pupgrade->kind == UPGRADE_COFFEE)
239         {
240           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
241           pplayer->got_coffee = YES;
242           timer_start(&super_bkgd_timer, 250);
243         }
244       else if (pupgrade->kind == UPGRADE_HERRING)
245         {
246           play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
247           timer_start(&pplayer->invincible_timer,TUX_INVINCIBLE_TIME);
248           timer_start(&super_bkgd_timer, 250);
249           /* play the herring song ^^ */
250           if (current_music != HURRYUP_MUSIC)
251             {
252               current_music = HERRING_MUSIC;
253               if (playing_music())
254                 halt_music();
255             }
256         }
257       break;
258     }
259 }
260