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