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