Changed the way the scrolling was calculated. Instead of calculating it relatively...
[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   for (ParticleSystems::iterator i = particle_systems.begin();
106           i != particle_systems.end(); ++i)
107     delete *i;
108
109   for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
110        i != bouncy_distros.end(); ++i)
111     delete *i;
112   
113   for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
114        i != broken_bricks.end(); ++i)
115     delete *i;
116   
117   for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
118        i != bouncy_bricks.end(); ++i)
119     delete *i;
120
121   for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
122        i != floating_scores.end(); ++i)
123     delete *i;
124   
125   delete level;
126 }
127
128 void
129 World::set_defaults()
130 {
131   // Set defaults: 
132   scroll_x = 0;
133
134   player_status.score_multiplier = 1;
135
136   counting_distros = false;
137   distro_counter = 0;
138
139   /* set current song/music */
140   currentmusic = LEVEL_MUSIC;
141 }
142
143 void
144 World::activate_bad_guys()
145 {
146   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
147        i != level->badguy_data.end();
148        ++i)
149     {
150       add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
151     }
152 }
153
154 void
155 World::activate_particle_systems()
156 {
157   if (level->particle_system == "clouds")
158     {
159       particle_systems.push_back(new CloudParticleSystem);
160     }
161   else if (level->particle_system == "snow")
162     {
163       particle_systems.push_back(new SnowParticleSystem);
164     }
165   else if (level->particle_system != "")
166     {
167       st_abort("unknown particle system specified in level", "");
168     }
169 }
170
171 void
172 World::draw()
173 {
174   int y,x;
175
176   /* Draw the real background */
177   if(get_level()->bkgd_image[0] != '\0')
178     {
179       int s = (int)((float)scroll_x * ((float)level->bkgd_speed/60.)) % screen->w;
180       level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
181       level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
182     }
183   else
184     {
185       drawgradient(level->bkgd_top, level->bkgd_bottom);
186     }
187     
188   /* Draw particle systems (background) */
189   std::vector<ParticleSystem*>::iterator p;
190   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
191     {
192       (*p)->draw(scroll_x, 0, 0);
193     }
194
195   /* Draw background: */
196   for (y = 0; y < 15; ++y)
197     {
198       for (x = 0; x < 21; ++x)
199         {
200           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
201                      level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
202         }
203     }
204
205   /* Draw interactive tiles: */
206   for (y = 0; y < 15; ++y)
207     {
208       for (x = 0; x < 21; ++x)
209         {
210           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
211                      level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
212         }
213     }
214
215   /* (Bouncy bricks): */
216   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
217     bouncy_bricks[i]->draw();
218
219   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
220     (*i)->draw();
221
222   tux.draw();
223
224   for (unsigned int i = 0; i < bullets.size(); ++i)
225     bullets[i].draw();
226
227   for (unsigned int i = 0; i < floating_scores.size(); ++i)
228     floating_scores[i]->draw();
229
230   for (unsigned int i = 0; i < upgrades.size(); ++i)
231     upgrades[i].draw();
232
233   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
234     bouncy_distros[i]->draw();
235
236   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
237     broken_bricks[i]->draw();
238
239   /* Draw foreground: */
240   for (y = 0; y < 15; ++y)
241     {
242       for (x = 0; x < 21; ++x)
243         {
244           Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
245                      level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
246         }
247     }
248
249   /* Draw particle systems (foreground) */
250   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
251     {
252       (*p)->draw(scroll_x, 0, 1);
253     }
254 }
255
256 void
257 World::action(double frame_ratio)
258 {
259   tux.action(frame_ratio);
260   keep_in_bounds();
261
262   /* Handle bouncy distros: */
263   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
264     bouncy_distros[i]->action(frame_ratio);
265
266   /* Handle broken bricks: */
267   for (unsigned int i = 0; i < broken_bricks.size(); i++)
268     broken_bricks[i]->action(frame_ratio);
269
270   // Handle all kinds of game objects
271   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
272     bouncy_bricks[i]->action(frame_ratio);
273   
274   for (unsigned int i = 0; i < floating_scores.size(); i++)
275     floating_scores[i]->action(frame_ratio);
276
277   for (unsigned int i = 0; i < bullets.size(); ++i)
278     bullets[i].action(frame_ratio);
279   
280   for (unsigned int i = 0; i < upgrades.size(); i++)
281     upgrades[i].action(frame_ratio);
282
283   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
284     (*i)->action(frame_ratio);
285
286   /* update particle systems */
287   std::vector<ParticleSystem*>::iterator p;
288   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
289     {
290       (*p)->simulate(frame_ratio);
291     }
292
293   /* Handle all possible collisions. */
294   collision_handler();
295   
296   // Cleanup marked badguys
297   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
298       /* ++i handled at end of the loop */) {
299     if ((*i)->is_removable()) {
300       delete *i;
301       i =  bad_guys.erase(i);
302     } else {
303       ++i;
304     }
305   }
306 }
307
308 // the space that it takes for the screen to start scrolling, regarding
309 // screen bounds (in pixels)
310 #define X_SPACE 160
311
312 /* This functions takes cares of the scrolling */
313 void World::keep_in_bounds()
314 {
315   int tux_pos_x = (int)(tux.base.x + (tux.base.width/2));
316
317   if (scroll_x < tux_pos_x - (screen->w - X_SPACE))
318     scroll_x = tux_pos_x - (screen->w - X_SPACE);
319   else if (scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
320     scroll_x = tux_pos_x - X_SPACE;
321
322   if(scroll_x < 0)
323     scroll_x = 0;
324 }
325
326 void
327 World::collision_handler()
328 {
329   // CO_BULLET & CO_BADGUY check
330   for(unsigned int i = 0; i < bullets.size(); ++i)
331     {
332       for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
333         {
334           if((*j)->dying != DYING_NOT)
335             continue;
336           
337           if(rectcollision(bullets[i].base, (*j)->base))
338             {
339               // We have detected a collision and now call the
340               // collision functions of the collided objects.
341               // collide with bad_guy first, since bullet_collision will
342               // delete the bullet
343               (*j)->collision(0, CO_BULLET);
344               bullets[i].collision(CO_BADGUY);
345               break; // bullet is invalid now, so break
346             }
347         }
348     }
349
350   /* CO_BADGUY & CO_BADGUY check */
351   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
352     {
353       if((*i)->dying != DYING_NOT)
354         continue;
355       
356       BadGuys::iterator j = i;
357       ++j;
358       for (; j != bad_guys.end(); ++j)
359         {
360           if(j == i || (*j)->dying != DYING_NOT)
361             continue;
362
363           if(rectcollision((*i)->base, (*j)->base))
364             {
365               // We have detected a collision and now call the
366               // collision functions of the collided objects.
367               (*j)->collision(*i, CO_BADGUY);
368               (*i)->collision(*j, CO_BADGUY);
369             }
370         }
371     }
372
373   if(tux.dying != DYING_NOT) return;
374     
375   // CO_BADGUY & CO_PLAYER check 
376   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
377     {
378       if((*i)->dying != DYING_NOT)
379         continue;
380       
381       if(rectcollision_offset((*i)->base, tux.base, 0, 0))
382         {
383           // We have detected a collision and now call the collision
384           // functions of the collided objects.
385           if (tux.previous_base.y < tux.base.y &&
386               tux.previous_base.y + tux.previous_base.height 
387               < (*i)->base.y + (*i)->base.height/2
388               && !tux.invincible_timer.started())
389             {
390               (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
391             }
392           else
393             {
394               tux.collision(*i, CO_BADGUY);
395               (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
396             }
397         }
398     }
399
400   // CO_UPGRADE & CO_PLAYER check
401   for(unsigned int i = 0; i < upgrades.size(); ++i)
402     {
403       if(rectcollision(upgrades[i].base, tux.base))
404         {
405           // We have detected a collision and now call the collision
406           // functions of the collided objects.
407           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
408         }
409     }
410 }
411
412 void
413 World::add_score(float x, float y, int s)
414 {
415   player_status.score += s;
416
417   FloatingScore* new_floating_score = new FloatingScore();
418   new_floating_score->init(x,y,s);
419   floating_scores.push_back(new_floating_score);
420 }
421
422 void
423 World::add_bouncy_distro(float x, float y)
424 {
425   BouncyDistro* new_bouncy_distro = new BouncyDistro();
426   new_bouncy_distro->init(x,y);
427   bouncy_distros.push_back(new_bouncy_distro);
428 }
429
430 void
431 World::add_broken_brick(Tile* tile, float x, float y)
432 {
433   add_broken_brick_piece(tile, x, y, -1, -4);
434   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
435
436   add_broken_brick_piece(tile, x + 16, y, 1, -4);
437   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
438 }
439
440 void
441 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
442 {
443   BrokenBrick* new_broken_brick = new BrokenBrick();
444   new_broken_brick->init(tile, x, y, xm, ym);
445   broken_bricks.push_back(new_broken_brick);
446 }
447
448 void
449 World::add_bouncy_brick(float x, float y)
450 {
451   BouncyBrick* new_bouncy_brick = new BouncyBrick();
452   new_bouncy_brick->init(x,y);
453   bouncy_bricks.push_back(new_bouncy_brick);
454 }
455
456 BadGuy*
457 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
458 {
459   BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
460   bad_guys.push_back(badguy);
461   return badguy;
462 }
463
464 void
465 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
466 {
467   Upgrade new_upgrade;
468   new_upgrade.init(x,y,dir,kind);
469   upgrades.push_back(new_upgrade);
470 }
471
472 void 
473 World::add_bullet(float x, float y, float xm, Direction dir)
474 {
475   if(bullets.size() > MAX_BULLETS-1)
476     return;
477
478   Bullet new_bullet;
479   new_bullet.init(x,y,xm,dir);
480   bullets.push_back(new_bullet);
481   
482   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
483 }
484
485 void
486 World::play_music(int musictype)
487 {
488   currentmusic = musictype;
489   switch(currentmusic) {
490     case HURRYUP_MUSIC:
491       music_manager->play_music(get_level()->get_level_music_fast());
492       break;
493     case LEVEL_MUSIC:
494       music_manager->play_music(get_level()->get_level_music());
495       break;
496     case HERRING_MUSIC:
497       music_manager->play_music(herring_song);
498       break;
499     default:
500       music_manager->halt_music();
501       break;
502   }
503 }
504
505 int
506 World::get_music_type()
507 {
508   return currentmusic;
509 }
510
511 /* Break a brick: */
512 void
513 World::trybreakbrick(float x, float y, bool small)
514 {
515   Level* plevel = get_level();
516   
517   Tile* tile = gettile(x, y);
518   if (tile->brick)
519     {
520       if (tile->data > 0)
521         {
522           /* Get a distro from it: */
523           add_bouncy_distro(((int)(x + 1) / 32) * 32,
524                                   (int)(y / 32) * 32);
525
526           // TODO: don't handle this in a global way but per-tile...
527           if (!counting_distros)
528             {
529               counting_distros = true;
530               distro_counter = 5;
531             }
532           else
533             {
534               distro_counter--;
535             }
536
537           if (distro_counter <= 0)
538             {
539               counting_distros = false;
540               plevel->change(x, y, TM_IA, tile->next_tile);
541             }
542
543           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
544           player_status.score = player_status.score + SCORE_DISTRO;
545           player_status.distros++;
546         }
547       else if (!small)
548         {
549           /* Get rid of it: */
550           plevel->change(x, y, TM_IA, tile->next_tile);
551           
552           /* Replace it with broken bits: */
553           add_broken_brick(tile, 
554                                  ((int)(x + 1) / 32) * 32,
555                                  (int)(y / 32) * 32);
556           
557           /* Get some score: */
558           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
559           player_status.score = player_status.score + SCORE_BRICK;
560         }
561     }
562 }
563
564 /* Empty a box: */
565 void
566 World::tryemptybox(float x, float y, Direction col_side)
567 {
568   Tile* tile = gettile(x,y);
569   if (!tile->fullbox)
570     return;
571
572   // according to the collision side, set the upgrade direction
573   if(col_side == LEFT)
574     col_side = RIGHT;
575   else
576     col_side = LEFT;
577
578   int posx = ((int)(x+1) / 32) * 32;
579   int posy = (int)(y/32) * 32 - 32;
580   switch(tile->data)
581     {
582     case 1: // Box with a distro!
583       add_bouncy_distro(posx, posy);
584       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
585       player_status.score = player_status.score + SCORE_DISTRO;
586       player_status.distros++;
587       break;
588
589     case 2: // Add an upgrade!
590       if (tux.size == SMALL)     /* Tux is small, add mints! */
591         add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
592       else     /* Tux is big, add an iceflower: */
593         add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
594       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
595       break;
596
597     case 3: // Add a golden herring
598       add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
599       break;
600
601     case 4: // Add a 1up extra
602       add_upgrade(posx, posy, col_side, UPGRADE_1UP);
603       break;
604     default:
605       break;
606     }
607
608   /* Empty the box: */
609   level->change(x, y, TM_IA, tile->next_tile);
610 }
611
612 /* Try to grab a distro: */
613 void
614 World::trygrabdistro(float x, float y, int bounciness)
615 {
616   Tile* tile = gettile(x, y);
617   if (tile && tile->distro)
618     {
619       level->change(x, y, TM_IA, tile->next_tile);
620       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
621
622       if (bounciness == BOUNCE)
623         {
624           add_bouncy_distro(((int)(x + 1) / 32) * 32,
625                                   (int)(y / 32) * 32);
626         }
627
628       player_status.score = player_status.score + SCORE_DISTRO;
629       player_status.distros++;
630     }
631 }
632
633 /* Try to bump a bad guy from below: */
634 void
635 World::trybumpbadguy(float x, float y)
636 {
637   // Bad guys: 
638   for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
639     {
640       if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
641           (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
642         {
643           (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
644         }
645     }
646
647   // Upgrades:
648   for (unsigned int i = 0; i < upgrades.size(); i++)
649     {
650       if (upgrades[i].base.height == 32 &&
651           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
652           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
653         {
654           upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
655         }
656     }
657 }
658
659 /* EOF */
660