big refactoring of level and world class. A level is now basically a set of
[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 (dir == RIGHT)
56     {
57       base.x = pos.x + 32;
58       physic.set_velocity_x(BULLET_XM + xm);
59     }
60   else
61     {
62       base.x = pos.x;
63       physic.set_velocity_x(-BULLET_XM + xm);
64     }
65
66   base.y = pos.y + base.height/2;
67   physic.set_velocity_y(-BULLET_STARTING_YM);
68   old_base = base;
69   kind = kind_;
70 }
71
72 void
73 Bullet::action(float elapsed_time)
74 {
75   elapsed_time *= 0.5f;
76
77   float old_y = base.y;
78
79   physic.apply(elapsed_time, base.x, base.y);
80   collision_swept_object_map(&old_base,&base);
81       
82   if (issolid(base.x, base.y + 4) || issolid(base.x, base.y))
83     {
84       base.y  = old_y;
85       physic.set_velocity_y(-physic.get_velocity_y());
86       life_count -= 1;
87     }
88
89   if(kind == FIRE_BULLET)
90     // @not framerate independant :-/
91     physic.set_velocity_y(physic.get_velocity_y() - 0.5 * elapsed_time);
92   if(physic.get_velocity_y() > 9)
93     physic.set_velocity_y(9);
94   else if(physic.get_velocity_y() < -9)
95     physic.set_velocity_y(-9);
96
97   float scroll_x =
98     Sector::current()->camera->get_translation().x;
99   float scroll_y =
100     Sector::current()->camera->get_translation().y;
101   if (base.x < scroll_x ||
102       base.x > scroll_x + screen->w ||
103       base.y < scroll_y ||
104       base.y > scroll_y + screen->h ||
105       issolid(base.x + 4, base.y + 2) ||
106       issolid(base.x, base.y + 2) ||
107       life_count <= 0)
108     {
109       remove_me();
110     }
111 }
112
113 void 
114 Bullet::draw(DrawingContext& context)
115 {
116   Sprite* sprite = kind == FIRE_BULLET ? img_firebullet : img_icebullet;
117  
118   sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
119 }
120
121 void
122 Bullet::collision(const MovingObject& , int)
123 {
124   // later
125 }
126
127 void
128 Bullet::collision(int c_object)
129 {
130   if(c_object == CO_BADGUY) {
131     remove_me();
132   }
133 }
134
135 //---------------------------------------------------------------------------
136
137 Upgrade::Upgrade(const Vector& pos, Direction dir_, UpgradeKind kind_)
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   float scroll_x =
185     Sector::current()->camera->get_translation().x;
186   float scroll_y =                                                        
187     Sector::current()->camera->get_translation().y;
188   
189   if(base.x < scroll_x - X_OFFSCREEN_DISTANCE ||
190       base.x > scroll_x + screen->w + X_OFFSCREEN_DISTANCE ||
191       base.y < scroll_y - Y_OFFSCREEN_DISTANCE ||
192       base.y > scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
193     {
194     remove_me();
195     return;
196     }
197
198   /* Move around? */
199   physic.apply(elapsed_time, base.x, base.y);
200   if(kind == UPGRADE_GROWUP) {
201     collision_swept_object_map(&old_base, &base);
202   }
203
204   // fall down?
205   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
206     // falling?
207     if(physic.get_velocity_y() != 0) {
208       if(issolid(base.x, base.y + base.height)) {
209         base.y = int(base.y / 32) * 32;
210         old_base = base;                         
211         if(kind == UPGRADE_GROWUP) {
212           physic.enable_gravity(false);
213           physic.set_velocity(dir == LEFT ? -GROWUP_SPEED : GROWUP_SPEED, 0);
214         } else if(kind == UPGRADE_HERRING) {
215           physic.set_velocity(dir == LEFT ? -2 : 2, 3);
216         }
217       }
218     } else {
219       if((physic.get_velocity_x() < 0
220             && !issolid(base.x+base.width, base.y + base.height))
221         || (physic.get_velocity_x() > 0
222             && !issolid(base.x, base.y + base.height))) {
223         physic.enable_gravity(true);
224       }
225     }
226   }
227
228   // horizontal bounce?
229   if(kind == UPGRADE_GROWUP || kind == UPGRADE_HERRING) {
230     if (  (physic.get_velocity_x() < 0
231           && issolid(base.x, (int) base.y + base.height/2)) 
232         ||  (physic.get_velocity_x() > 0
233           && issolid(base.x + base.width, (int) base.y + base.height/2))) {
234         physic.set_velocity(-physic.get_velocity_x(),physic.get_velocity_y());
235         dir = dir == LEFT ? RIGHT : LEFT;
236     }
237   }
238 }
239
240 void
241 Upgrade::draw(DrawingContext& context)
242 {
243   Sprite* sprite;
244   switch(kind) {
245     case UPGRADE_GROWUP: sprite = img_growup; break;
246     case UPGRADE_ICEFLOWER: sprite = img_iceflower; break;
247     case UPGRADE_FIREFLOWER: sprite = img_fireflower; break;
248     case UPGRADE_HERRING: sprite = img_star; break;
249     case UPGRADE_1UP: sprite = img_1up; break;
250     default:
251       assert(!"wrong type in Powerup::draw()");
252   }
253
254   if(base.height < 32) // still raising up?
255     sprite->draw(context, Vector(base.x, base.y + (32 - base.height)),
256         LAYER_TILES - 10);
257   else
258     sprite->draw(context, Vector(base.x, base.y), LAYER_OBJECTS);
259 }
260
261 void
262 Upgrade::bump(Player* player)
263 {
264   // these can't be bumped
265   if(kind != UPGRADE_GROWUP)
266     return;
267
268   play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
269   
270   // determine new direction
271   if (player->base.x + player->base.width/2 > base.x + base.width/2)
272     dir = LEFT;
273   else
274     dir = RIGHT;
275
276   // do a little jump and change direction
277   physic.set_velocity(-physic.get_velocity_x(), 3);
278   physic.enable_gravity(true);
279 }
280
281 void
282 Upgrade::collision(const MovingObject& , int)
283 {
284   // later
285 }
286
287 void
288 Upgrade::collision(void* p_c_object, int c_object, CollisionType type)
289 {
290   Player* pplayer = NULL;
291
292   if(type == COLLISION_BUMP) {
293     if(c_object == CO_PLAYER)
294       pplayer = (Player*) p_c_object;
295     bump(pplayer);
296     return;
297   }
298
299   switch (c_object)
300     {
301     case CO_PLAYER:
302       /* Remove the upgrade: */
303
304       /* p_c_object is CO_PLAYER, so assign it to pplayer */
305       pplayer = (Player*) p_c_object;
306
307       /* Affect the player: */
308
309       if (kind == UPGRADE_GROWUP)
310         {
311           play_sound(sounds[SND_EXCELLENT], SOUND_CENTER_SPEAKER);
312           pplayer->grow(true);
313         }
314       else if (kind == UPGRADE_FIREFLOWER)
315         {
316           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
317           pplayer->grow(true);
318           pplayer->got_power = pplayer->FIRE_POWER;
319         }
320       else if (kind == UPGRADE_ICEFLOWER)
321         {
322           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
323           pplayer->grow(true);
324           pplayer->got_power = pplayer->ICE_POWER;
325         }
326       else if (kind == UPGRADE_FIREFLOWER)
327         {
328           play_sound(sounds[SND_COFFEE], SOUND_CENTER_SPEAKER);
329           pplayer->grow(true);
330           pplayer->got_power = pplayer->FIRE_POWER;
331         }
332       else if (kind == UPGRADE_HERRING)
333         {
334           play_sound(sounds[SND_HERRING], SOUND_CENTER_SPEAKER);
335           pplayer->invincible_timer.start(TUX_INVINCIBLE_TIME);
336           Sector::current()->play_music(HERRING_MUSIC);
337         }
338       else if (kind == UPGRADE_1UP)
339         {
340           if(player_status.lives < MAX_LIVES) {
341             player_status.lives++;
342             play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
343           }
344         }
345
346       remove_me();
347       return;
348     }
349 }
350
351 void load_special_gfx()
352 {
353   img_growup    = sprite_manager->load("egg");
354   img_iceflower = sprite_manager->load("iceflower");
355   img_fireflower = sprite_manager->load("fireflower");
356   img_star      = sprite_manager->load("star");
357   img_1up       = sprite_manager->load("1up");
358
359   img_firebullet    = sprite_manager->load("firebullet");
360   img_icebullet    = sprite_manager->load("icebullet");
361 }
362
363 void free_special_gfx()
364 {
365 }
366