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