Fixes.
[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_fireflower;
40 Sprite* img_1up;
41
42 #define GROWUP_SPEED 1.0f
43
44 #define BULLET_STARTING_YM 0
45 #define BULLET_XM 6
46
47 Bullet::Bullet(DisplayManager& display_manager, const Vector& pos, float xm,
48     int dir, int kind_)
49 {
50   display_manager.add_drawable(this, LAYER_OBJECTS);
51   
52   life_count = 3;
53   base.width = 4;
54   base.height = 4;
55
56   if (dir == RIGHT)
57     {
58       base.x = pos.x + 32;
59       physic.set_velocity_x(BULLET_XM + xm);
60     }
61   else
62     {
63       base.x = pos.x;
64       physic.set_velocity_x(-BULLET_XM + xm);
65     }
66
67   base.y = pos.y + base.height/2;
68   physic.set_velocity_y(-BULLET_STARTING_YM);
69   old_base = base;
70   kind = kind_;
71 }
72
73 void
74 Bullet::action(float elapsed_time)
75 {
76   elapsed_time *= 0.5f;
77
78   float old_y = base.y;
79
80   physic.apply(elapsed_time, base.x, base.y);
81   collision_swept_object_map(&old_base,&base);
82       
83   if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
84     {
85       base.y  = old_y;
86       physic.set_velocity_y(-physic.get_velocity_y());
87       life_count -= 1;
88     }
89
90   if(kind == FIRE_BULLET)
91     // @not framerate independant :-/
92     physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
93   if(physic.get_velocity_y() > 9)
94     physic.set_velocity_y(9);
95   else if(physic.get_velocity_y() < -9)
96     physic.set_velocity_y(-9);
97
98   float scroll_x =
99     World::current()->displaymanager.get_viewport().get_translation().x;
100   float scroll_y =
101     World::current()->displaymanager.get_viewport().get_translation().y;
102   if (base.x < scroll_x ||
103       base.x > scroll_x + screen->w ||
104       base.y < scroll_y ||
105       base.y > scroll_y + screen->h ||
106       issolid(base.x + 4, base.y + 2) ||
107       issolid(base.x, base.y + 2) ||
108       life_count <= 0)
109     {
110       remove_me();
111     }
112 }
113
114 void 
115 Bullet::draw(ViewPort& viewport, int )
116 {
117   if(kind == FIRE_BULLET)
118     img_firebullet->draw(viewport.world2screen(Vector(base.x, base.y)));
119   else if(kind == ICE_BULLET)
120     img_icebullet->draw(viewport.world2screen(Vector(base.x, base.y)));
121 }
122
123 void
124 Bullet::collision(const MovingObject& , int)
125 {
126   // later
127 }
128
129 void
130 Bullet::collision(int c_object)
131 {
132   if(c_object == CO_BADGUY) {
133     remove_me();
134   }
135 }
136
137 //---------------------------------------------------------------------------
138
139 Upgrade::Upgrade(DisplayManager& display_manager, const Vector& pos,
140     Direction dir_, UpgradeKind kind_)
141 {
142   display_manager.add_drawable(this, LAYER_OBJECTS);
143   
144   kind = kind_;
145   dir = dir_;
146
147   base.width = 32;
148   base.height = 0;
149   base.x = pos.x;
150   base.y = pos.y;
151   old_base = base;
152
153   physic.reset();
154   physic.enable_gravity(false);
155
156   if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
157     physic.set_velocity(dir == LEFT ? -1 : 1, 4);
158     physic.enable_gravity(true);
159     base.height = 32;
160   } else if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER) {
161     // nothing
162   } else if (kind == UPGRADE_GROWUP) {
163     physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
164   } else {
165     physic.set_velocity(dir == LEFT ? -2 : 2, 0);
166   }
167 }
168
169 Upgrade::~Upgrade()
170 {
171 }
172
173 void
174 Upgrade::action(float elapsed_time)
175 {
176   if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
177       || kind == UPGRADE_GROWUP) {
178     if (base.height < 32) {
179       /* Rise up! */
180       base.height = base.height + 0.7 * elapsed_time;
181       if(base.height > 32)
182         base.height = 32;
183
184       return;
185     }
186   }
187
188   /* Away from the screen? Kill it! */
189   float scroll_x =
190     World::current()->displaymanager.get_viewport().get_translation().x;
191   float scroll_y =                                                        
192     World::current()->displaymanager.get_viewport().get_translation().y;
193   
194   if(base.x < scroll_x - X_OFFSCREEN_DISTANCE ||
195       base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE ||
196       base.y < scroll_y - Y_OFFSCREEN_DISTANCE ||
197       base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
198     {
199     remove_me();
200     return;
201     }
202
203   /* Move around? */
204   physic.apply(elapsed_time, base.x, base.y);
205   if(kind == UPGRADE_GROWUP) {
206     collision_swept_object_map(&old_base, &base);
207   }
208
209   // fall down?
210   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
211     // falling?
212     if(physic.get_velocity_y() != 0) {
213       if(issolid(base.x, base.y + base.height)) {
214         base.y = int(base.y / 32) * 32;
215         old_base = base;                         
216         if(kind == UPGRADE_GROWUP) {
217           physic.enable_gravity(false);
218           physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
219         } else if(kind == UPGRADE_HERRING) {
220           physic.set_velocity(dir == LEFT ? -2 : 2, 3);
221         }
222       }
223     } else {
224       if((physic.get_velocity_x() < 0
225             && !issolid(base.x+base.width, base.y + base.height))
226         || (physic.get_velocity_x() > 0
227             && !issolid(base.x, base.y + base.height))) {
228         physic.enable_gravity(true);
229       }
230     }
231   }
232
233   // horizontal bounce?
234   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
235     if (  (physic.get_velocity_x() < 0
236           && issolid(base.x, (int) base.y + base.height/2)) 
237         ||  (physic.get_velocity_x() > 0
238           && issolid(base.x + base.width, (int) base.y + base.height/2))) {
239         physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
240         dir = dir == LEFT ? RIGHT : LEFT;
241     }
242   }
243 }
244
245 void
246 Upgrade::draw(ViewPort& viewport, int)
247 {
248   SDL_Rect dest;
249
250   if (base.height < 32)
251     {
252       /* Rising up... */
253
254       dest.x = (int)(base.x - viewport.get_translation().x);
255       dest.y = (int)(base.y + 32 - base.height - viewport.get_translation().y);
256       dest.w = 32;
257       dest.h = (int)base.height;
258
259       if (kind == UPGRADE_GROWUP)
260         img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
261       else if (kind == UPGRADE_ICEFLOWER)
262         img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
263       else if (kind == UPGRADE_FIREFLOWER)
264         img_fireflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
265       else if (kind == UPGRADE_HERRING)
266         img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
267       else if (kind == UPGRADE_1UP)
268         img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
269     }
270   else
271     {
272       if (kind == UPGRADE_GROWUP)
273         {
274           img_growup->draw(viewport.world2screen(Vector(base.x, base.y)));
275         }
276       else if (kind == UPGRADE_ICEFLOWER)
277         {
278           img_iceflower->draw(viewport.world2screen(Vector(base.x, base.y)));
279         }
280       else if (kind == UPGRADE_FIREFLOWER)
281         {
282           img_fireflower->draw(viewport.world2screen(Vector(base.x, base.y)));
283         }
284       else if (kind == UPGRADE_HERRING)
285         {
286           img_star->draw(viewport.world2screen(Vector(base.x, base.y)));
287         }
288       else if (kind == UPGRADE_1UP)
289         {
290           img_1up->draw(viewport.world2screen(Vector(base.x, base.y)));
291         }
292     }
293 }
294
295 void
296 Upgrade::bump(Player* player)
297 {
298   // these can't be bumped
299   if(kind != UPGRADE_GROWUP)
300     return;
301
302   play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
303   
304   // determine new direction
305   if (player->base.x + player->base.width/2 > base.x + base.width/2)
306     dir = LEFT;
307   else
308     dir = RIGHT;
309
310   // do a little jump and change direction
311   physic.set_velocity(-physic.get_velocity_x(), 3);
312   physic.enable_gravity(true);
313 }
314
315 void
316 Upgrade::collision(const MovingObject& , int)
317 {
318   // later
319 }
320
321 void
322 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
323 {
324   Player* pplayer = NULL;
325
326   if(type == COLLISION_BUMP) {
327     if(c_object == CO_PLAYER)
328       pplayer = (Player*) p_c_object;
329     bump(pplayer);
330     return;
331   }
332
333   switch (c_object)
334     {
335     case CO_PLAYER:
336       /* Remove the upgrade: */
337
338       /* p_c_object is CO_PLAYER, so assign it to pplayer */
339       pplayer = (Player*) p_c_object;
340
341       /* Affect the player: */
342
343       if (kind == UPGRADE_GROWUP)
344         {
345           play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
346           pplayer->grow();
347         }
348       else if (kind == UPGRADE_FIREFLOWER)
349         {
350           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
351           pplayer->grow();
352           pplayer->got_power = pplayer->FIRE_POWER;
353         }
354       else if (kind == UPGRADE_ICEFLOWER)
355         {
356           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
357           pplayer->grow();
358           pplayer->got_power = pplayer->ICE_POWER;
359         }
360       else if (kind == UPGRADE_FIREFLOWER)
361         {
362           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
363           pplayer->grow();
364           pplayer->got_power = pplayer->FIRE_POWER;
365         }
366       else if (kind == UPGRADE_HERRING)
367         {
368           play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
369           pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
370           World::current()->play_music(HERRING_MUSIC);
371         }
372       else if (kind == UPGRADE_1UP)
373         {
374           if(player_status.lives < MAX_LIVES) {
375             player_status.lives++;
376             play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
377           }
378         }
379
380       remove_me();
381       return;
382     }
383 }
384
385 void load_special_gfx()
386 {
387   img_growup    = sprite_manager->load("egg");
388   img_iceflower = sprite_manager->load("iceflower");
389   img_fireflower = sprite_manager->load("fireflower");
390   img_star      = sprite_manager->load("star");
391   img_1up       = sprite_manager->load("1up");
392
393   img_firebullet    = sprite_manager->load("firebullet");
394   img_icebullet    = sprite_manager->load("icebullet");
395 }
396
397 void free_special_gfx()
398 {
399 }
400