removed debug stuff
[supertux.git] / src / world.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 //  02111-1307, USA.
22
23 #include <iostream>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "globals.h"
28 #include "scene.h"
29 #include "screen.h"
30 #include "defines.h"
31 #include "world.h"
32 #include "level.h"
33 #include "tile.h"
34 #include "resources.h"
35
36 Surface* img_distro[4];
37
38 World* World::current_ = 0;
39
40 World::World(const std::string& filename)
41 {
42   // FIXME: Move this to action and draw and everywhere else where the
43   // world calls child functions
44   current_ = this;
45
46   level = new Level(filename);
47   tux.init();
48
49   set_defaults();
50
51   get_level()->load_gfx();
52   activate_bad_guys();
53   activate_particle_systems();
54   get_level()->load_song();
55
56   apply_bonuses();
57 }
58
59 World::World(const std::string& subset, int level_nr)
60 {
61   // FIXME: Move this to action and draw and everywhere else where the
62   // world calls child functions
63   current_ = this;
64
65   level = new Level(subset, level_nr);
66   tux.init();
67
68   set_defaults();
69
70   get_level()->load_gfx();
71   activate_bad_guys();
72   activate_particle_systems();
73   get_level()->load_song();
74
75   apply_bonuses();
76 }
77
78 void
79 World::apply_bonuses()
80 {
81   // Apply bonuses from former levels
82   switch (player_status.bonus)
83     {
84     case PlayerStatus::NO_BONUS:
85       break;
86
87     case PlayerStatus::FLOWER_BONUS:
88       tux.got_coffee = true;
89       // fall through
90
91     case PlayerStatus::GROWUP_BONUS:
92       // FIXME: Move this to Player class
93       tux.size = BIG;
94       tux.base.height = 64;
95       tux.base.y -= 32;
96       break;
97     }
98 }
99
100 World::~World()
101 {
102   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
103     delete *i;
104   
105   delete level;
106 }
107
108 void
109 World::set_defaults()
110 {
111   // Set defaults: 
112   scroll_x = 0;
113
114   player_status.score_multiplier = 1;
115
116   counting_distros = false;
117   distro_counter = 0;
118
119   /* set current song/music */
120   currentmusic = LEVEL_MUSIC;
121 }
122
123 void
124 World::activate_bad_guys()
125 {
126   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
127        i != level->badguy_data.end();
128        ++i)
129     {
130       add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
131     }
132 }
133
134 void
135 World::activate_particle_systems()
136 {
137   if (level->particle_system == "clouds")
138     {
139       particle_systems.push_back(new CloudParticleSystem);
140     }
141   else if (level->particle_system == "snow")
142     {
143       particle_systems.push_back(new SnowParticleSystem);
144     }
145   else if (level->particle_system != "")
146     {
147       st_abort("unknown particle system specified in level", "");
148     }
149 }
150
151 void
152 World::draw()
153 {
154   int y,x;
155
156   /* Draw the real background */
157   if(get_level()->bkgd_image[0] != '\0')
158     {
159       int s = ((int)scroll_x / 2)%640;
160       level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
161       level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
162     }
163   else
164     {
165       drawgradient(level->bkgd_top, level->bkgd_bottom);
166     }
167     
168   /* Draw particle systems (background) */
169   std::vector<ParticleSystem*>::iterator p;
170   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
171     {
172       (*p)->draw(scroll_x, 0, 0);
173     }
174
175   /* Draw background: */
176   for (y = 0; y < 15; ++y)
177     {
178       for (x = 0; x < 21; ++x)
179         {
180           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
181                      level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
182         }
183     }
184
185   /* Draw interactive tiles: */
186   for (y = 0; y < 15; ++y)
187     {
188       for (x = 0; x < 21; ++x)
189         {
190           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
191                      level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
192         }
193     }
194
195   /* (Bouncy bricks): */
196   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
197     bouncy_bricks[i].draw();
198
199   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
200     (*i)->draw();
201
202   tux.draw();
203
204   for (unsigned int i = 0; i < bullets.size(); ++i)
205     bullets[i].draw();
206
207   for (unsigned int i = 0; i < floating_scores.size(); ++i)
208     floating_scores[i].draw();
209
210   for (unsigned int i = 0; i < upgrades.size(); ++i)
211     upgrades[i].draw();
212
213   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
214     bouncy_distros[i].draw();
215
216   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
217     broken_bricks[i].draw();
218
219   /* Draw foreground: */
220   for (y = 0; y < 15; ++y)
221     {
222       for (x = 0; x < 21; ++x)
223         {
224           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
225                      level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
226         }
227     }
228
229   /* Draw particle systems (foreground) */
230   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
231     {
232       (*p)->draw(scroll_x, 0, 1);
233     }
234 }
235
236 void
237 World::action(double frame_ratio)
238 {
239   tux.action(frame_ratio);
240
241   /* Handle bouncy distros: */
242   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
243     bouncy_distros[i].action(frame_ratio);
244
245   /* Handle broken bricks: */
246   for (unsigned int i = 0; i < broken_bricks.size(); i++)
247     broken_bricks[i].action(frame_ratio);
248
249   /* Handle distro counting: */
250   if (counting_distros)
251     {
252       distro_counter--;
253
254       if (distro_counter <= 0)
255         counting_distros = -1;
256     }
257
258   // Handle all kinds of game objects
259   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
260     bouncy_bricks[i].action(frame_ratio);
261   
262   for (unsigned int i = 0; i < floating_scores.size(); i++)
263     floating_scores[i].action(frame_ratio);
264
265   for (unsigned int i = 0; i < bullets.size(); ++i)
266     bullets[i].action(frame_ratio);
267   
268   for (unsigned int i = 0; i < upgrades.size(); i++)
269     upgrades[i].action(frame_ratio);
270
271   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
272     (*i)->action(frame_ratio);
273
274   /* update particle systems */
275   std::vector<ParticleSystem*>::iterator p;
276   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
277     {
278       (*p)->simulate(frame_ratio);
279     }
280
281   /* Handle all possible collisions. */
282   collision_handler();
283   
284   // Cleanup marked badguys
285   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
286       /* ++i handled at end of the loop */) {
287     if ((*i)->is_removable()) {
288       delete *i;
289       i = bad_guys.erase(i);
290     } else {
291       ++i;
292     }
293   }
294 }
295
296 void
297 World::collision_handler()
298 {
299   // CO_BULLET & CO_BADGUY check
300   for(unsigned int i = 0; i < bullets.size(); ++i)
301     {
302       for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
303         {
304           if((*j)->dying != DYING_NOT)
305             continue;
306           
307           if(rectcollision(bullets[i].base, (*j)->base))
308             {
309               // We have detected a collision and now call the
310               // collision functions of the collided objects.
311               // collide with bad_guy first, since bullet_collision will
312               // delete the bullet
313               (*j)->collision(0, CO_BULLET);
314               bullets[i].collision(CO_BADGUY);
315               break; // bullet is invalid now, so break
316             }
317         }
318     }
319
320   /* CO_BADGUY & CO_BADGUY check */
321   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
322     {
323       if((*i)->dying != DYING_NOT)
324         continue;
325       
326       BadGuys::iterator j = i;
327       ++j;
328       for (; j != bad_guys.end(); ++j)
329         {
330           if(j == i || (*j)->dying != DYING_NOT)
331             continue;
332
333           if(rectcollision((*i)->base, (*j)->base))
334             {
335               // We have detected a collision and now call the
336               // collision functions of the collided objects.
337               (*j)->collision(*i, CO_BADGUY);
338               (*i)->collision(*j, CO_BADGUY);
339             }
340         }
341     }
342
343   if(tux.dying != DYING_NOT) return;
344     
345   // CO_BADGUY & CO_PLAYER check 
346   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
347     {
348       if((*i)->dying != DYING_NOT)
349         continue;
350       
351       if(rectcollision_offset((*i)->base, tux.base, 0, 0))
352         {
353           // We have detected a collision and now call the collision
354           // functions of the collided objects.
355           if (tux.previous_base.y < tux.base.y &&
356               tux.previous_base.y + tux.previous_base.height 
357               < (*i)->base.y + (*i)->base.height/2)
358             {
359               (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
360             }
361           else
362             {
363               tux.collision(*i, CO_BADGUY);
364               (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
365             }
366         }
367     }
368
369   // CO_UPGRADE & CO_PLAYER check
370   for(unsigned int i = 0; i < upgrades.size(); ++i)
371     {
372       if(rectcollision(upgrades[i].base, tux.base))
373         {
374           // We have detected a collision and now call the collision
375           // functions of the collided objects.
376           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
377         }
378     }
379 }
380
381 void
382 World::add_score(float x, float y, int s)
383 {
384   player_status.score += s;
385
386   FloatingScore new_floating_score;
387   new_floating_score.init(x,y,s);
388   floating_scores.push_back(new_floating_score);
389 }
390
391 void
392 World::add_bouncy_distro(float x, float y)
393 {
394   BouncyDistro new_bouncy_distro;
395   new_bouncy_distro.init(x,y);
396   bouncy_distros.push_back(new_bouncy_distro);
397 }
398
399 void
400 World::add_broken_brick(Tile* tile, float x, float y)
401 {
402   add_broken_brick_piece(tile, x, y, -1, -4);
403   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
404
405   add_broken_brick_piece(tile, x + 16, y, 1, -4);
406   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
407 }
408
409 void
410 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
411 {
412   BrokenBrick new_broken_brick;
413   new_broken_brick.init(tile, x, y, xm, ym);
414   broken_bricks.push_back(new_broken_brick);
415 }
416
417 void
418 World::add_bouncy_brick(float x, float y)
419 {
420   BouncyBrick new_bouncy_brick;
421   new_bouncy_brick.init(x,y);
422   bouncy_bricks.push_back(new_bouncy_brick);
423 }
424
425 BadGuy*
426 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
427 {
428   BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
429   bad_guys.push_back(badguy);
430   return badguy;
431 }
432
433 void
434 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
435 {
436   Upgrade new_upgrade;
437   new_upgrade.init(x,y,dir,kind);
438   upgrades.push_back(new_upgrade);
439 }
440
441 void 
442 World::add_bullet(float x, float y, float xm, Direction dir)
443 {
444   if(bullets.size() > MAX_BULLETS-1)
445     return;
446
447   Bullet new_bullet;
448   new_bullet.init(x,y,xm,dir);
449   bullets.push_back(new_bullet);
450   
451   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
452 }
453
454 void
455 World::play_music(int musictype)
456 {
457   currentmusic = musictype;
458   switch(currentmusic) {
459     case HURRYUP_MUSIC:
460       music_manager->play_music(get_level()->get_level_music_fast());
461       break;
462     case LEVEL_MUSIC:
463       music_manager->play_music(get_level()->get_level_music());
464       break;
465     case HERRING_MUSIC:
466       music_manager->play_music(herring_song);
467       break;
468     default:
469       music_manager->halt_music();
470       break;
471   }
472 }
473
474 int
475 World::get_music_type()
476 {
477   return currentmusic;
478 }
479
480 /* Break a brick: */
481 void
482 World::trybreakbrick(float x, float y, bool small)
483 {
484   Level* plevel = get_level();
485   
486   Tile* tile = gettile(x, y);
487   if (tile->brick)
488     {
489       if (tile->data > 0)
490         {
491           /* Get a distro from it: */
492           add_bouncy_distro(((int)(x + 1) / 32) * 32,
493                                   (int)(y / 32) * 32);
494
495           if (!counting_distros)
496             {
497               counting_distros = true;
498               distro_counter = 50;
499             }
500
501           if (distro_counter <= 0)
502             plevel->change(x, y, TM_IA, tile->next_tile);
503
504           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
505           player_status.score = player_status.score + SCORE_DISTRO;
506           player_status.distros++;
507         }
508       else if (!small)
509         {
510           /* Get rid of it: */
511           plevel->change(x, y, TM_IA, tile->next_tile);
512           
513           /* Replace it with broken bits: */
514           add_broken_brick(tile, 
515                                  ((int)(x + 1) / 32) * 32,
516                                  (int)(y / 32) * 32);
517           
518           /* Get some score: */
519           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
520           player_status.score = player_status.score + SCORE_BRICK;
521         }
522     }
523 }
524
525 /* Empty a box: */
526 void
527 World::tryemptybox(float x, float y, Direction col_side)
528 {
529   Tile* tile = gettile(x,y);
530   if (!tile->fullbox)
531     return;
532
533   // according to the collision side, set the upgrade direction
534   if(col_side == LEFT)
535     col_side = RIGHT;
536   else
537     col_side = LEFT;
538
539   int posx = ((int)(x+1) / 32) * 32;
540   int posy = (int)(y/32) * 32 - 32;
541   switch(tile->data)
542     {
543     case 1: // Box with a distro!
544       add_bouncy_distro(posx, posy);
545       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
546       player_status.score = player_status.score + SCORE_DISTRO;
547       player_status.distros++;
548       break;
549
550     case 2: // Add an upgrade!
551       if (tux.size == SMALL)     /* Tux is small, add mints! */
552         add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
553       else     /* Tux is big, add an iceflower: */
554         add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
555       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
556       break;
557
558     case 3: // Add a golden herring
559       add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
560       break;
561
562     case 4: // Add a 1up extra
563       add_upgrade(posx, posy, col_side, UPGRADE_1UP);
564       break;
565     default:
566       break;
567     }
568
569   /* Empty the box: */
570   level->change(x, y, TM_IA, tile->next_tile);
571 }
572
573 /* Try to grab a distro: */
574 void
575 World::trygrabdistro(float x, float y, int bounciness)
576 {
577   Tile* tile = gettile(x, y);
578   if (tile && tile->distro)
579     {
580       level->change(x, y, TM_IA, tile->next_tile);
581       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
582
583       if (bounciness == BOUNCE)
584         {
585           add_bouncy_distro(((int)(x + 1) / 32) * 32,
586                                   (int)(y / 32) * 32);
587         }
588
589       player_status.score = player_status.score + SCORE_DISTRO;
590       player_status.distros++;
591     }
592 }
593
594 /* Try to bump a bad guy from below: */
595 void
596 World::trybumpbadguy(float x, float y)
597 {
598   // Bad guys: 
599   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
600     {
601       if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
602           (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
603         {
604           (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
605         }
606     }
607
608   // Upgrades:
609   for (unsigned int i = 0; i < upgrades.size(); i++)
610     {
611       if (upgrades[i].base.height == 32 &&
612           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
613           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
614         {
615           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
616         }
617     }
618 }
619
620 /* EOF */
621