-converted remaining classes to GameObject
[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   if (base.x < scroll_x ||
99       base.x > scroll_x + screen->w ||
100       base.y > screen->h ||
101       issolid(base.x + 4, base.y + 2) ||
102       issolid(base.x, base.y + 2) ||
103       life_count <= 0)
104     {
105       remove_me();
106     }
107 }
108
109 void 
110 Bullet::draw(ViewPort& viewport, int )
111 {
112   if(kind == FIRE_BULLET)
113     img_firebullet->draw(viewport.world2screen(Vector(base.x, base.y)));
114   else if(kind == ICE_BULLET)
115     img_icebullet->draw(viewport.world2screen(Vector(base.x, base.y)));
116 }
117
118 void
119 Bullet::collision(const MovingObject& , int)
120 {
121   // later
122 }
123
124 void
125 Bullet::collision(int c_object)
126 {
127   if(c_object == CO_BADGUY) {
128     remove_me();
129   }
130 }
131
132 //---------------------------------------------------------------------------
133
134 Upgrade::Upgrade(DisplayManager& display_manager, const Vector& pos,
135     Direction dir_, UpgradeKind kind_)
136 {
137   display_manager.add_drawable(this, LAYER_OBJECTS);
138   
139   kind = kind_;
140   dir = dir_;
141
142   base.width = 32;
143   base.height = 0;
144   base.x = pos.x;
145   base.y = pos.y;
146   old_base = base;
147
148   physic.reset();
149   physic.enable_gravity(false);
150
151   if(kind == UPGRADE_1UP || kind == UPGRADE_HERRING) {
152     physic.set_velocity(dir == LEFT ? -1 : 1, 4);
153     physic.enable_gravity(true);
154     base.height = 32;
155   } else if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER) {
156     // nothing
157   } else if (kind == UPGRADE_GROWUP) {
158     physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
159   } else {
160     physic.set_velocity(dir == LEFT ? -2 : 2, 0);
161   }
162 }
163
164 Upgrade::~Upgrade()
165 {
166 }
167
168 void
169 Upgrade::action(float elapsed_time)
170 {
171   if (kind == UPGRADE_ICEFLOWER || kind == UPGRADE_FIREFLOWER
172       || kind == UPGRADE_GROWUP) {
173     if (base.height < 32) {
174       /* Rise up! */
175       base.height = base.height + 0.7 * elapsed_time;
176       if(base.height > 32)
177         base.height = 32;
178
179       return;
180     }
181   }
182
183   /* Away from the screen? Kill it! */
184   if(base.x < scroll_x - OFFSCREEN_DISTANCE) {
185       remove_me();
186       return;
187   }
188   if(base.y > screen->h) {
189     remove_me();
190     return;
191   }
192
193   /* Move around? */
194   physic.apply(elapsed_time, base.x, base.y);
195   if(kind == UPGRADE_GROWUP) {
196     collision_swept_object_map(&old_base, &base);
197   }
198
199   // fall down?
200   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
201     // falling?
202     if(physic.get_velocity_y() != 0) {
203       if(issolid(base.x, base.y + base.height)) {
204         base.y = int(base.y / 32) * 32;
205         old_base = base;                         
206         if(kind == UPGRADE_GROWUP) {
207           physic.enable_gravity(false);
208           physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
209         } else if(kind == UPGRADE_HERRING) {
210           physic.set_velocity(dir == LEFT ? -2 : 2, 3);
211         }
212       }
213     } else {
214       if((physic.get_velocity_x() < 0
215             && !issolid(base.x+base.width, base.y + base.height))
216         || (physic.get_velocity_x() > 0
217             && !issolid(base.x, base.y + base.height))) {
218         physic.enable_gravity(true);
219       }
220     }
221   }
222
223   // horizontal bounce?
224   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
225     if (  (physic.get_velocity_x() < 0
226           && issolid(base.x, (int) base.y + base.height/2)) 
227         ||  (physic.get_velocity_x() > 0
228           && issolid(base.x + base.width, (int) base.y + base.height/2))) {
229         physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
230         dir = dir == LEFT ? RIGHT : LEFT;
231     }
232   }
233 }
234
235 void
236 Upgrade::draw(ViewPort& viewport, int)
237 {
238   SDL_Rect dest;
239
240   if (base.height < 32)
241     {
242       /* Rising up... */
243
244       dest.x = (int)(base.x - viewport.get_translation().x);
245       dest.y = (int)(base.y + 32 - base.height - viewport.get_translation().y);
246       dest.w = 32;
247       dest.h = (int)base.height;
248
249       if (kind == UPGRADE_GROWUP)
250         img_growup->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
251       else if (kind == UPGRADE_ICEFLOWER)
252         img_iceflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
253       else if (kind == UPGRADE_FIREFLOWER)
254         img_fireflower->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
255       else if (kind == UPGRADE_HERRING)
256         img_star->draw_part(0,0,dest.x,dest.y,dest.w,dest.h);
257       else if (kind == UPGRADE_1UP)
258         img_1up->draw_part( 0, 0, dest.x, dest.y, dest.w, dest.h);
259     }
260   else
261     {
262       if (kind == UPGRADE_GROWUP)
263         {
264           img_growup->draw(viewport.world2screen(Vector(base.x, base.y)));
265         }
266       else if (kind == UPGRADE_ICEFLOWER)
267         {
268           img_iceflower->draw(viewport.world2screen(Vector(base.x, base.y)));
269         }
270       else if (kind == UPGRADE_FIREFLOWER)
271         {
272           img_fireflower->draw(viewport.world2screen(Vector(base.x, base.y)));
273         }
274       else if (kind == UPGRADE_HERRING)
275         {
276           img_star->draw(viewport.world2screen(Vector(base.x, base.y)));
277         }
278       else if (kind == UPGRADE_1UP)
279         {
280           img_1up->draw(viewport.world2screen(Vector(base.x, base.y)));
281         }
282     }
283 }
284
285 void
286 Upgrade::bump(Player* player)
287 {
288   // these can't be bumped
289   if(kind != UPGRADE_GROWUP)
290     return;
291
292   play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
293   
294   // determine new direction
295   if (player->base.x + player->base.width/2 > base.x + base.width/2)
296     dir = LEFT;
297   else
298     dir = RIGHT;
299
300   // do a little jump and change direction
301   physic.set_velocity(-physic.get_velocity_x(), 3);
302   physic.enable_gravity(true);
303 }
304
305 void
306 Upgrade::collision(const MovingObject& , int)
307 {
308   // later
309 }
310
311 void
312 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
313 {
314   Player* pplayer = NULL;
315
316   if(type == COLLISION_BUMP) {
317     if(c_object == CO_PLAYER)
318       pplayer = (Player*) p_c_object;
319     bump(pplayer);
320     return;
321   }
322
323   switch (c_object)
324     {
325     case CO_PLAYER:
326       /* Remove the upgrade: */
327
328       /* p_c_object is CO_PLAYER, so assign it to pplayer */
329       pplayer = (Player*) p_c_object;
330
331       /* Affect the player: */
332
333       if (kind == UPGRADE_GROWUP)
334         {
335           play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
336           pplayer->grow();
337         }
338       else if (kind == UPGRADE_FIREFLOWER)
339         {
340           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
341           pplayer->grow();
342           pplayer->got_power = pplayer->FIRE_POWER;
343         }
344       else if (kind == UPGRADE_ICEFLOWER)
345         {
346           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
347           pplayer->grow();
348           pplayer->got_power = pplayer->ICE_POWER;
349         }
350       else if (kind == UPGRADE_FIREFLOWER)
351         {
352           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
353           pplayer->grow();
354           pplayer->got_power = pplayer->FIRE_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_fireflower = sprite_manager->load("fireflower");
380   img_star      = sprite_manager->load("star");
381   img_1up       = sprite_manager->load("1up");
382
383   img_firebullet    = sprite_manager->load("firebullet");
384   img_icebullet    = sprite_manager->load("icebullet");
385 }
386
387 void free_special_gfx()
388 {
389 }
390