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