- added UpgradeKind name to nameless enum
[supertux.git] / src / world.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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 <math.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "globals.h"
24 #include "scene.h"
25 #include "screen.h"
26 #include "defines.h"
27 #include "world.h"
28 #include "level.h"
29 #include "tile.h"
30 #include "resources.h"
31
32 Surface* img_distro[4];
33
34 World* World::current_ = 0;
35
36 World::World(const std::string& filename)
37 {
38   // FIXME: Move this to action and draw and everywhere else where the
39   // world calls child functions
40   current_ = this;
41
42   level = new Level(filename);
43   tux.init();
44
45   set_defaults();
46
47   get_level()->load_gfx();
48   activate_bad_guys();
49   activate_particle_systems();
50   get_level()->load_song();
51 }
52
53 World::World(const std::string& subset, int level_nr)
54 {
55   // FIXME: Move this to action and draw and everywhere else where the
56   // world calls child functions
57   current_ = this;
58
59   level = new Level(subset, level_nr);
60   tux.init();
61
62   set_defaults();
63
64   get_level()->load_gfx();
65   activate_bad_guys();
66   activate_particle_systems();
67   get_level()->load_song();
68 }
69
70 World::~World()
71 {
72   delete level;
73 }
74
75 void
76 World::set_defaults()
77 {
78   // Set defaults: 
79   scroll_x = 0;
80
81   player_status.score_multiplier = 1;
82
83   counting_distros = false;
84   distro_counter = 0;
85
86   /* set current song/music */
87   set_current_music(LEVEL_MUSIC);
88 }
89
90 void
91 World::activate_bad_guys()
92 {
93   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
94        i != level->badguy_data.end();
95        ++i)
96     {
97       add_bad_guy(i->x, i->y, i->kind);
98     }
99 }
100
101 void
102 World::activate_particle_systems()
103 {
104   if (level->particle_system == "clouds")
105     {
106       particle_systems.push_back(new CloudParticleSystem);
107     }
108   else if (level->particle_system == "snow")
109     {
110       particle_systems.push_back(new SnowParticleSystem);
111     }
112   else if (level->particle_system != "")
113     {
114       st_abort("unknown particle system specified in level", "");
115     }
116 }
117
118 void
119 World::draw()
120 {
121   int y,x;
122
123   /* Draw the real background */
124   if(get_level()->bkgd_image[0] != '\0')
125     {
126       int s = ((int)scroll_x / 2)%640;
127       level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
128       level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
129     }
130   else
131     {
132       drawgradient(level->bkgd_top, level->bkgd_bottom);
133     }
134     
135   /* Draw particle systems (background) */
136   std::vector<ParticleSystem*>::iterator p;
137   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
138     {
139       (*p)->draw(scroll_x, 0, 0);
140     }
141
142   /* Draw background: */
143   for (y = 0; y < 15; ++y)
144     {
145       for (x = 0; x < 21; ++x)
146         {
147           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
148                      level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
149         }
150     }
151
152   /* Draw interactive tiles: */
153   for (y = 0; y < 15; ++y)
154     {
155       for (x = 0; x < 21; ++x)
156         {
157           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
158                      level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
159         }
160     }
161
162   /* (Bouncy bricks): */
163   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
164     bouncy_bricks[i].draw();
165
166   for (unsigned int i = 0; i < bad_guys.size(); ++i)
167     bad_guys[i].draw();
168
169   tux.draw();
170
171   for (unsigned int i = 0; i < bullets.size(); ++i)
172     bullets[i].draw();
173
174   for (unsigned int i = 0; i < floating_scores.size(); ++i)
175     floating_scores[i].draw();
176
177   for (unsigned int i = 0; i < upgrades.size(); ++i)
178     upgrades[i].draw();
179
180   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
181     bouncy_distros[i].draw();
182
183   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
184     broken_bricks[i].draw();
185
186   /* Draw foreground: */
187   for (y = 0; y < 15; ++y)
188     {
189       for (x = 0; x < 21; ++x)
190         {
191           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
192                      level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
193         }
194     }
195
196   /* Draw particle systems (foreground) */
197   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
198     {
199       (*p)->draw(scroll_x, 0, 1);
200     }
201 }
202
203 void
204 World::action(double frame_ratio)
205 {
206   tux.action(frame_ratio);
207
208   /* Handle bouncy distros: */
209   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
210     bouncy_distros[i].action(frame_ratio);
211
212   /* Handle broken bricks: */
213   for (unsigned int i = 0; i < broken_bricks.size(); i++)
214     broken_bricks[i].action(frame_ratio);
215
216   /* Handle distro counting: */
217   if (counting_distros)
218     {
219       distro_counter--;
220
221       if (distro_counter <= 0)
222         counting_distros = -1;
223     }
224
225   // Handle all kinds of game objects
226   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
227     bouncy_bricks[i].action(frame_ratio);
228   
229   for (unsigned int i = 0; i < floating_scores.size(); i++)
230     floating_scores[i].action(frame_ratio);
231
232   for (unsigned int i = 0; i < bullets.size(); ++i)
233     bullets[i].action(frame_ratio);
234   
235   for (unsigned int i = 0; i < upgrades.size(); i++)
236     upgrades[i].action(frame_ratio);
237
238   for (unsigned int i = 0; i < bad_guys.size(); i++)
239     bad_guys[i].action(frame_ratio);
240
241   /* update particle systems */
242   std::vector<ParticleSystem*>::iterator p;
243   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
244     {
245       (*p)->simulate(frame_ratio);
246     }
247
248   /* Handle all possible collisions. */
249   collision_handler();
250 }
251
252
253 void
254 World::collision_handler()
255 {
256   // CO_BULLET & CO_BADGUY check
257   for(unsigned int i = 0; i < bullets.size(); ++i)
258     {
259       for(unsigned int j = 0; j < bad_guys.size(); ++j)
260         {
261           if(bad_guys[j].dying != DYING_NOT)
262             continue;
263           if(rectcollision(&bullets[i].base, &bad_guys[j].base))
264             {
265               // We have detected a collision and now call the
266               // collision functions of the collided objects.
267               // collide with bad_guy first, since bullet_collision will
268               // delete the bullet
269               bad_guys[j].collision(0, CO_BULLET);
270               bullets[i].collision(CO_BADGUY);
271               break; // bullet is invalid now, so break
272             }
273         }
274     }
275
276   /* CO_BADGUY & CO_BADGUY check */
277   for(unsigned int i = 0; i < bad_guys.size(); ++i)
278     {
279       if(bad_guys[i].dying != DYING_NOT)
280         continue;
281       
282       for(unsigned int j = i+1; j < bad_guys.size(); ++j)
283         {
284           if(j == i || bad_guys[j].dying != DYING_NOT)
285             continue;
286
287           if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
288             {
289               // We have detected a collision and now call the
290               // collision functions of the collided objects.
291               bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
292               bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
293             }
294         }
295     }
296
297   if(tux.dying != DYING_NOT) return;
298     
299   // CO_BADGUY & CO_PLAYER check 
300   for(unsigned int i = 0; i < bad_guys.size(); ++i)
301     {
302       if(bad_guys[i].dying != DYING_NOT)
303         continue;
304       
305       if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
306         {
307           // We have detected a collision and now call the collision
308           // functions of the collided objects.
309           if (tux.previous_base.y < tux.base.y &&
310               tux.previous_base.y + tux.previous_base.height 
311               < bad_guys[i].base.y + bad_guys[i].base.height/2)
312             {
313               bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
314             }
315           else
316             {
317               tux.collision(&bad_guys[i], CO_BADGUY);
318               bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
319             }
320         }
321     }
322
323   // CO_UPGRADE & CO_PLAYER check
324   for(unsigned int i = 0; i < upgrades.size(); ++i)
325     {
326       if(rectcollision(&upgrades[i].base, &tux.base))
327         {
328           // We have detected a collision and now call the collision
329           // functions of the collided objects.
330           upgrades[i].collision(&tux, CO_PLAYER);
331         }
332     }
333 }
334
335 void
336 World::add_score(float x, float y, int s)
337 {
338   player_status.score += s;
339
340   FloatingScore new_floating_score;
341   new_floating_score.init(x,y,s);
342   floating_scores.push_back(new_floating_score);
343 }
344
345 void
346 World::add_bouncy_distro(float x, float y)
347 {
348   BouncyDistro new_bouncy_distro;
349   new_bouncy_distro.init(x,y);
350   bouncy_distros.push_back(new_bouncy_distro);
351 }
352
353 void
354 World::add_broken_brick(Tile* tile, float x, float y)
355 {
356   add_broken_brick_piece(tile, x, y, -1, -4);
357   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
358
359   add_broken_brick_piece(tile, x + 16, y, 1, -4);
360   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
361 }
362
363 void
364 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
365 {
366   BrokenBrick new_broken_brick;
367   new_broken_brick.init(tile, x, y, xm, ym);
368   broken_bricks.push_back(new_broken_brick);
369 }
370
371 void
372 World::add_bouncy_brick(float x, float y)
373 {
374   BouncyBrick new_bouncy_brick;
375   new_bouncy_brick.init(x,y);
376   bouncy_bricks.push_back(new_bouncy_brick);
377 }
378
379 void
380 World::add_bad_guy(float x, float y, BadGuyKind kind)
381 {
382   bad_guys.push_back(BadGuy());
383   BadGuy& new_bad_guy = bad_guys.back();
384   
385   new_bad_guy.init(x,y,kind);
386 }
387
388 void
389 World::add_upgrade(float x, float y, int dir, UpgradeKind kind)
390 {
391   Upgrade new_upgrade;
392   new_upgrade.init(x,y,dir,kind);
393   upgrades.push_back(new_upgrade);
394 }
395
396 void 
397 World::add_bullet(float x, float y, float xm, int dir)
398 {
399   Bullet new_bullet;
400   new_bullet.init(x,y,xm,dir);
401   bullets.push_back(new_bullet);
402   
403   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
404 }
405
406 /* Break a brick: */
407 void
408 World::trybreakbrick(float x, float y, bool small)
409 {
410   Level* plevel = get_level();
411   
412   Tile* tile = gettile(x, y);
413   if (tile->brick)
414     {
415       if (tile->data > 0)
416         {
417           /* Get a distro from it: */
418           add_bouncy_distro(((int)(x + 1) / 32) * 32,
419                                   (int)(y / 32) * 32);
420
421           if (!counting_distros)
422             {
423               counting_distros = true;
424               distro_counter = 50;
425             }
426
427           if (distro_counter <= 0)
428             plevel->change(x, y, TM_IA, tile->next_tile);
429
430           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
431           player_status.score = player_status.score + SCORE_DISTRO;
432           player_status.distros++;
433         }
434       else if (!small)
435         {
436           /* Get rid of it: */
437           plevel->change(x, y, TM_IA, tile->next_tile);
438           
439           /* Replace it with broken bits: */
440           add_broken_brick(tile, 
441                                  ((int)(x + 1) / 32) * 32,
442                                  (int)(y / 32) * 32);
443           
444           /* Get some score: */
445           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
446           player_status.score = player_status.score + SCORE_BRICK;
447         }
448     }
449 }
450
451 /* Empty a box: */
452 void
453 World::tryemptybox(float x, float y, int col_side)
454 {
455   Tile* tile = gettile(x,y);
456   if (!tile->fullbox)
457     return;
458
459   // according to the collision side, set the upgrade direction
460   if(col_side == LEFT)
461     col_side = RIGHT;
462   else
463     col_side = LEFT;
464
465   int posx = ((int)(x+1) / 32) * 32;
466   int posy = (int)(y/32) * 32 - 32;
467   switch(tile->data)
468     {
469     case 1: // Box with a distro!
470       add_bouncy_distro(posx, posy);
471       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
472       player_status.score = player_status.score + SCORE_DISTRO;
473       player_status.distros++;
474       break;
475
476     case 2: // Add an upgrade!
477       if (tux.size == SMALL)     /* Tux is small, add mints! */
478         add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
479       else     /* Tux is big, add an iceflower: */
480         add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
481       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
482       break;
483
484     case 3: // Add a golden herring
485       add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
486       break;
487
488     case 4: // Add a 1up extra
489       add_upgrade(posx, posy, col_side, UPGRADE_1UP);
490       break;
491     default:
492       break;
493     }
494
495   /* Empty the box: */
496   level->change(x, y, TM_IA, tile->next_tile);
497 }
498
499 /* Try to grab a distro: */
500 void
501 World::trygrabdistro(float x, float y, int bounciness)
502 {
503   Tile* tile = gettile(x, y);
504   if (tile && tile->distro)
505     {
506       level->change(x, y, TM_IA, tile->next_tile);
507       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
508
509       if (bounciness == BOUNCE)
510         {
511           add_bouncy_distro(((int)(x + 1) / 32) * 32,
512                                   (int)(y / 32) * 32);
513         }
514
515       player_status.score = player_status.score + SCORE_DISTRO;
516       player_status.distros++;
517     }
518 }
519
520 /* Try to bump a bad guy from below: */
521 void
522 World::trybumpbadguy(float x, float y)
523 {
524   /* Bad guys: */
525   for (unsigned int i = 0; i < bad_guys.size(); i++)
526     {
527       if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
528           bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
529         {
530           bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
531         }
532     }
533
534
535   /* Upgrades: */
536   for (unsigned int i = 0; i < upgrades.size(); i++)
537     {
538       if (upgrades[i].base.height == 32 &&
539           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
540           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
541         {
542           upgrades[i].base.xm = -upgrades[i].base.xm;
543           upgrades[i].base.ym = -8;
544           play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
545         }
546     }
547 }
548
549 /* EOF */
550