First implementation of the ice power.
[supertux.git] / src / special.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <assert.h>
21 #include <iostream>
22 #include "SDL.h"
23 #include "defines.h"
24 #include "special.h"
25 #include "gameloop.h"
26 #include "screen.h"
27 #include "sound.h"
28 #include "scene.h"
29 #include "globals.h"
30 #include "player.h"
31 #include "sprite_manager.h"
32 #include "resources.h"
33
34 Sprite* img_firebullet;
35 Sprite* img_icebullet;
36 Sprite* img_star;
37 Sprite* img_growup;
38 Sprite* img_iceflower;
39 Sprite* img_1up;
40
41 #define GROWUP_SPEED 1.0f
42
43 #define BULLET_STARTING_YM 0
44 #define BULLET_XM 6
45
46 void
47 Bullet::init(float x, float y, float xm, Direction dir, int kind_)
48 {
49   life_count = 3;
50   base.width = 4;
51   base.height = 4;
52
53   if (dir == RIGHT)
54     {
55       base.x = x + 32;
56       base.xm = BULLET_XM + xm;
57     }
58   else
59     {
60       base.x = x;
61       base.xm = -BULLET_XM + xm;
62     }
63
64   base.y = y;
65   base.ym = BULLET_STARTING_YM;
66   old_base = base;
67   kind = kind_;
68 }
69
70 void
71 Bullet::remove_me()
72 {
73   std::vector<Bullet>& bullets = World::current()->bullets;
74   for(std::vector<Bullet>::iterator i = bullets.begin();
75          i != bullets.end(); ++i) {
76     if( & (*i) == this) {
77       bullets.erase(i);
78       return;
79     }
80   }
81
82   assert(false);
83 }
84
85 void
86 Bullet::action(double frame_ratio)
87 {
88   frame_ratio *= 0.5f;
89
90   float old_y = base.y;
91
92   base.x = base.x + base.xm * frame_ratio;
93   base.y = base.y + base.ym * frame_ratio;
94
95   collision_swept_object_map(&old_base,&base);
96       
97   if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
98     {
99       base.y  = old_y;
100       base.ym = -base.ym;     
101       if (base.ym > 9)
102         base.ym = 9;
103       else if (base.ym < -9)
104         base.ym = -9;
105       life_count -= 1;
106     }
107
108   if(kind == FIRE_BULLET)
109     base.ym = base.ym + 0.5 * frame_ratio;
110   else if(kind == FIRE_BULLET)
111     base.ym = 0;
112
113   if (base.x < scroll_x ||
114       base.x > scroll_x + screen->w ||
115       base.y > screen->h ||
116       issolid(base.x + 4, base.y + 2) ||
117       issolid(base.x, base.y + 2) ||
118       life_count <= 0)
119     {
120       remove_me();
121     }
122
123 }
124
125 void 
126 Bullet::draw()
127 {
128   if (base.x >= scroll_x - base.width &&
129       base.x <= scroll_x + screen->w)
130     {
131       if(kind == FIRE_BULLET)
132         img_firebullet->draw(base.x, base.y);
133       else if(kind == FIRE_BULLET)
134         img_icebullet->draw(base.x, base.y);
135     }
136 }
137
138 void
139 Bullet::collision(int c_object)
140 {
141   if(c_object == CO_BADGUY) {
142     remove_me();
143   }
144 }
145
146 void
147 Upgrade::init(float x_, float y_, Direction dir_, UpgradeKind kind_)
148 {
149   kind = kind_;
150   dir = dir_;
151
152   base.width = 32;
153   base.height = 0;
154   base.x = x_;
155   base.y = y_;
156   old_base = base;
157
158   physic.reset();
159   physic.enable_gravity(false);
160
161   if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
162     physic.set_velocity(dir == LEFT ? -1 : 1, 4);
163     physic.enable_gravity(true);
164     base.height = 32;
165   } else if (kind == UPGRADE_ICEFLOWER) {
166     // nothing
167   } else if (kind == UPGRADE_GROWUP) {
168     physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
169   } else {
170     physic.set_velocity(dir == LEFT ? -2 : 2, 0);
171   }
172 }
173
174 void
175 Upgrade::remove_me()
176 {
177   std::vector<Upgrade>& upgrades = World::current()->upgrades;
178   for(std::vector<Upgrade>::iterator i = upgrades.begin();
179          i != upgrades.end(); ++i) {
180     if( & (*i) == this) {
181       upgrades.erase(i);
182       return;
183     }
184   }
185
186   assert(false);
187 }
188
189 void
190 Upgrade::action(double frame_ratio)
191 {
192   if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_GROWUP) {
193     if (base.height < 32) {
194       /* Rise up! */
195       base.height = base.height + 0.7 * frame_ratio;
196       if(base.height > 32)
197         base.height = 32;
198
199       return;
200     }
201   }
202
203   /* Away from the screen? Kill it! */
204   if(base.x < scroll_x - OFFSCREEN_DISTANCE) {
205       remove_me();
206       return;
207   }
208   if(base.y > screen->h) {
209     remove_me();
210     return;
211   }
212
213   /* Move around? */
214   physic.apply(frame_ratio, base.x, base.y);
215   if(kind == UPGRADE_GROWUP) {
216     collision_swept_object_map(&old_base, &base);
217   }
218
219   // fall down?
220   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
221     // falling?
222     if(physic.get_velocity_y() != 0) {
223       if(issolid(base.x, base.y + base.height)) {
224         base.y = int(base.y / 32) * 32;
225         old_base = base;                         
226         if(kind == UPGRADE_GROWUP) {
227           physic.enable_gravity(false);
228           physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
229         } else if(kind == UPGRADE_HERRING) {
230           physic.set_velocity(dir == LEFT ? -2 : 2, 3);
231         }
232       }
233     } else {
234       if((physic.get_velocity_x() < 0
235             && !issolid(base.x+base.width, base.y + base.height))
236         || (physic.get_velocity_x() > 0
237             && !issolid(base.x, base.y + base.height))) {
238         physic.enable_gravity(true);
239       }
240     }
241   }
242
243   // horizontal bounce?
244   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
245     if (  (physic.get_velocity_x() < 0
246           && issolid(base.x, (int) base.y + base.height/2)) 
247         ||  (physic.get_velocity_x() > 0
248           && issolid(base.x + base.width, (int) base.y + base.height/2))) {
249         physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
250         dir = dir == LEFT ? RIGHT : LEFT;
251     }
252   }
253 }
254
255 void
256 Upgrade::draw()
257 {
258   SDL_Rect dest;
259
260   if (base.height < 32)
261     {
262       /* Rising up... */
263
264       dest.x = (int)(base.x);
265       dest.y = (int)(base.y + 32 - base.height);
266       dest.w = 32;
267       dest.h = (int)base.height;
268
269       if (kind == UPGRADE_GROWUP)
270         img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
271       else if (kind == UPGRADE_ICEFLOWER)
272         img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
273       else if (kind == UPGRADE_HERRING)
274         img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
275       else if (kind == UPGRADE_1UP)
276         img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
277     }
278   else
279     {
280       if (kind == UPGRADE_GROWUP)
281         {
282           img_growup->draw(
283                        base.x, base.y);
284         }
285       else if (kind == UPGRADE_ICEFLOWER)
286         {
287           img_iceflower->draw(
288                        base.x, base.y);
289         }
290       else if (kind == UPGRADE_HERRING)
291         {
292           img_star->draw(
293                        base.x, base.y);
294         }
295       else if (kind == UPGRADE_1UP)
296         {
297           img_1up->draw( base.x, base.y);
298         }
299     }
300 }
301
302 void
303 Upgrade::bump(Player* )
304 {
305   // these can't be bumped
306   if(kind != UPGRADE_GROWUP)
307     return;
308
309   play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
310   
311   // do a little jump and change direction
312   physic.set_velocity(-physic.get_velocity_x(), 3);
313   dir = dir == LEFT ? RIGHT : LEFT;
314   physic.enable_gravity(true);
315 }
316
317 void
318 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
319 {
320   Player* pplayer = NULL;
321
322   if(type == COLLISION_BUMP) {
323     if(c_object == CO_PLAYER)
324       pplayer = (Player*) p_c_object;
325     bump(pplayer);
326     return;
327   }
328
329   switch (c_object)
330     {
331     case CO_PLAYER:
332       /* Remove the upgrade: */
333
334       /* p_c_object is CO_PLAYER, so assign it to pplayer */
335       pplayer = (Player*) p_c_object;
336
337       /* Affect the player: */
338
339       if (kind == UPGRADE_GROWUP)
340         {
341           play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
342           pplayer->grow();
343         }
344       else if (kind == UPGRADE_FIREFLOWER)
345         {
346           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
347           pplayer->grow();
348           pplayer->got_power = pplayer->FIRE_POWER;
349         }
350       else if (kind == UPGRADE_ICEFLOWER)
351         {
352           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
353           pplayer->grow();
354           pplayer->got_power = pplayer->ICE_POWER;
355         }
356       else if (kind == UPGRADE_HERRING)
357         {
358           play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
359           pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
360           World::current()->play_music(HERRING_MUSIC);
361         }
362       else if (kind == UPGRADE_1UP)
363         {
364           if(player_status.lives < MAX_LIVES) {
365             player_status.lives++;
366             play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
367           }
368         }
369
370       remove_me();
371       return;
372     }
373 }
374
375 void load_special_gfx()
376 {
377   img_growup    = sprite_manager->load("egg");
378   img_iceflower = sprite_manager->load("iceflower");
379   img_star      = sprite_manager->load("star");
380   img_1up       = sprite_manager->load("1up");
381
382   img_firebullet    = sprite_manager->load("firebullet");
383   img_icebullet    = sprite_manager->load("icebullet");
384 }
385
386 void free_special_gfx()
387 {
388 }
389