- moved some activate_bad_guy stuff into the world, where it belongs
[supertux.git] / src / world.cpp
1 //
2 // C Implementation: world
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "globals.h"
17 #include "scene.h"
18 #include "screen.h"
19 #include "defines.h"
20 #include "world.h"
21 #include "level.h"
22 #include "tile.h"
23
24 texture_type img_distro[4];
25
26 World world;
27
28 World::World()
29 {
30   level = new Level;
31 }
32
33 World::~World()
34 {
35   delete level;
36 }
37
38 int
39 World::load(const char* subset, int level_nr)
40 {
41   return level->load(subset, level_nr);
42 }
43
44 int
45 World::load(const std::string& filename)
46 {
47   return level->load(filename);
48 }
49
50 void
51 World::arrays_free(void)
52 {
53   bad_guys.clear();
54   bouncy_distros.clear();
55   broken_bricks.clear();
56   bouncy_bricks.clear();
57   floating_scores.clear();
58   upgrades.clear();
59   bullets.clear();
60   std::vector<ParticleSystem*>::iterator i;
61   for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
62     delete *i;
63   }
64   particle_systems.clear();
65 }
66
67 void
68 World::activate_bad_guys()
69 {
70   for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
71        i != level->badguy_data.end();
72        ++i)
73     {
74       add_bad_guy(i->x, i->y, i->kind);
75     }
76 }
77
78 void
79 World::activate_particle_systems()
80 {
81   if (level->particle_system == "clouds")
82     {
83       particle_systems.push_back(new CloudParticleSystem);
84     }
85   else if (level->particle_system == "snow")
86     {
87       particle_systems.push_back(new SnowParticleSystem);
88     }
89   else if (level->particle_system != "")
90     {
91       st_abort("unknown particle system specified in level", "");
92     }
93 }
94
95 void
96 World::draw()
97 {
98   int y,x;
99
100   /* Draw screen: */
101   if(timer_check(&super_bkgd_timer))
102     texture_draw(&img_super_bkgd, 0, 0);
103   else
104     {
105       /* Draw the real background */
106       if(get_level()->bkgd_image[0] != '\0')
107         {
108           int s = (int)scroll_x / 30;
109           texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
110           texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
111         }
112       else
113         {
114           clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
115         }
116     }
117
118   /* Draw particle systems (background) */
119   std::vector<ParticleSystem*>::iterator p;
120   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
121     {
122       (*p)->draw(scroll_x, 0, 0);
123     }
124
125   /* Draw background: */
126   for (y = 0; y < 15; ++y)
127     {
128       for (x = 0; x < 21; ++x)
129         {
130           drawshape(32*x - fmodf(scroll_x, 32), y * 32,
131                     level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
132         }
133     }
134
135   /* Draw interactive tiles: */
136   for (y = 0; y < 15; ++y)
137     {
138       for (x = 0; x < 21; ++x)
139         {
140           drawshape(32*x - fmodf(scroll_x, 32), y * 32,
141                     level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
142         }
143     }
144
145   /* (Bouncy bricks): */
146   for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
147     bouncy_brick_draw(&bouncy_bricks[i]);
148
149   for (unsigned int i = 0; i < bad_guys.size(); ++i)
150     bad_guys[i].draw();
151
152   tux.draw();
153
154   for (unsigned int i = 0; i < bullets.size(); ++i)
155     bullet_draw(&bullets[i]);
156
157   for (unsigned int i = 0; i < floating_scores.size(); ++i)
158     floating_score_draw(&floating_scores[i]);
159
160   for (unsigned int i = 0; i < upgrades.size(); ++i)
161     upgrade_draw(&upgrades[i]);
162
163   for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
164     bouncy_distro_draw(&bouncy_distros[i]);
165
166   for (unsigned int i = 0; i < broken_bricks.size(); ++i)
167     broken_brick_draw(&broken_bricks[i]);
168
169   /* Draw foreground: */
170   for (y = 0; y < 15; ++y)
171     {
172       for (x = 0; x < 21; ++x)
173         {
174           drawshape(32*x - fmodf(scroll_x, 32), y * 32,
175                     level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
176         }
177     }
178
179   /* Draw particle systems (foreground) */
180   for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
181     {
182       (*p)->draw(scroll_x, 0, 1);
183     }
184 }
185
186 void
187 World::action()
188 {
189   /* Handle bouncy distros: */
190   for (unsigned int i = 0; i < bouncy_distros.size(); i++)
191     bouncy_distro_action(&bouncy_distros[i]);
192
193   /* Handle broken bricks: */
194   for (unsigned int i = 0; i < broken_bricks.size(); i++)
195     broken_brick_action(&broken_bricks[i]);
196
197   /* Handle distro counting: */
198   if (counting_distros)
199     {
200       distro_counter--;
201
202       if (distro_counter <= 0)
203         counting_distros = -1;
204     }
205
206   // Handle all kinds of game objects
207   for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
208     bouncy_brick_action(&bouncy_bricks[i]);
209   
210   for (unsigned int i = 0; i < floating_scores.size(); i++)
211     floating_score_action(&floating_scores[i]);
212
213   for (unsigned int i = 0; i < bullets.size(); ++i)
214     bullet_action(&bullets[i]);
215   
216   for (unsigned int i = 0; i < upgrades.size(); i++)
217     upgrade_action(&upgrades[i]);
218
219   for (unsigned int i = 0; i < bad_guys.size(); i++)
220     bad_guys[i].action();
221 }
222
223 void
224 World::add_score(float x, float y, int s)
225 {
226   score += s;
227
228   floating_score_type new_floating_score;
229   floating_score_init(&new_floating_score,x,y,s);
230   floating_scores.push_back(new_floating_score);
231 }
232
233 void
234 World::add_bouncy_distro(float x, float y)
235 {
236   bouncy_distro_type new_bouncy_distro;
237   bouncy_distro_init(&new_bouncy_distro,x,y);
238   bouncy_distros.push_back(new_bouncy_distro);
239 }
240
241 void
242 World::add_broken_brick(Tile* tile, float x, float y)
243 {
244   add_broken_brick_piece(tile, x, y, -1, -4);
245   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
246
247   add_broken_brick_piece(tile, x + 16, y, 1, -4);
248   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
249 }
250
251 void
252 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
253 {
254   broken_brick_type new_broken_brick;
255   broken_brick_init(&new_broken_brick, tile, x, y, xm, ym);
256   broken_bricks.push_back(new_broken_brick);
257 }
258
259 void
260 World::add_bouncy_brick(float x, float y)
261 {
262   bouncy_brick_type new_bouncy_brick;
263   bouncy_brick_init(&new_bouncy_brick,x,y);
264   bouncy_bricks.push_back(new_bouncy_brick);
265 }
266
267 void
268 World::add_bad_guy(float x, float y, BadGuyKind kind)
269 {
270   bad_guys.push_back(BadGuy());
271   BadGuy& new_bad_guy = bad_guys.back();
272   
273   new_bad_guy.init(x,y,kind);
274 }
275
276 void
277 World::add_upgrade(float x, float y, int dir, int kind)
278 {
279   upgrade_type new_upgrade;
280   upgrade_init(&new_upgrade,x,y,dir,kind);
281   upgrades.push_back(new_upgrade);
282 }
283
284 void 
285 World::add_bullet(float x, float y, float xm, int dir)
286 {
287   bullet_type new_bullet;
288   bullet_init(&new_bullet,x,y,xm,dir);
289   bullets.push_back(new_bullet);
290   
291   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
292 }
293
294
295
296 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
297 {
298   pbouncy_distro->base.x = x;
299   pbouncy_distro->base.y = y;
300   pbouncy_distro->base.ym = -2;
301 }
302
303 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
304 {
305   pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym * frame_ratio;
306
307   pbouncy_distro->base.ym += 0.1 * frame_ratio;
308
309   if (pbouncy_distro->base.ym >= 0)
310     world.bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
311 }
312
313 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
314 {
315   texture_draw(&img_distro[0],
316                pbouncy_distro->base.x - scroll_x,
317                pbouncy_distro->base.y);
318 }
319
320 void broken_brick_init(broken_brick_type* pbroken_brick, Tile* tile, 
321                        float x, float y, float xm, float ym)
322 {
323   pbroken_brick->tile   = tile;
324   pbroken_brick->base.x = x;
325   pbroken_brick->base.y = y;
326   pbroken_brick->base.xm = xm;
327   pbroken_brick->base.ym = ym;
328
329   timer_init(&pbroken_brick->timer, true);
330   timer_start(&pbroken_brick->timer,200);
331 }
332
333 void broken_brick_action(broken_brick_type* pbroken_brick)
334 {
335   pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
336   pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
337
338   if (!timer_check(&pbroken_brick->timer))
339     world.broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
340 }
341
342 void broken_brick_draw(broken_brick_type* pbroken_brick)
343 {
344   SDL_Rect src, dest;
345   src.x = rand() % 16;
346   src.y = rand() % 16;
347   src.w = 16;
348   src.h = 16;
349
350   dest.x = (int)(pbroken_brick->base.x - scroll_x);
351   dest.y = (int)pbroken_brick->base.y;
352   dest.w = 16;
353   dest.h = 16;
354   
355   if (pbroken_brick->tile->images.size() > 0)
356     texture_draw_part(&pbroken_brick->tile->images[0],
357                       src.x,src.y,dest.x,dest.y,dest.w,dest.h);
358 }
359
360 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
361 {
362   pbouncy_brick->base.x   = x;
363   pbouncy_brick->base.y   = y;
364   pbouncy_brick->offset   = 0;
365   pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
366   pbouncy_brick->shape    = GameSession::current()->get_level()->gettileid(x, y);
367 }
368
369 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
370 {
371
372   pbouncy_brick->offset = (pbouncy_brick->offset +
373                            pbouncy_brick->offset_m * frame_ratio);
374
375   /* Go back down? */
376
377   if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
378     pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
379
380
381   /* Stop bouncing? */
382
383   if (pbouncy_brick->offset >= 0)
384     world.bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
385 }
386
387 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
388 {
389   int s;
390   SDL_Rect dest;
391   
392   if (pbouncy_brick->base.x >= scroll_x - 32 &&
393       pbouncy_brick->base.x <= scroll_x + screen->w)
394     {
395       dest.x = (int)(pbouncy_brick->base.x - scroll_x);
396       dest.y = (int)pbouncy_brick->base.y;
397       dest.w = 32;
398       dest.h = 32;
399
400       Level* plevel = GameSession::current()->get_level();
401
402       // FIXME: overdrawing hack to clean the tile from the screen to
403       // paint it later at on offseted position
404       if(plevel->bkgd_image[0] == '\0')
405         {
406           fillrect(pbouncy_brick->base.x - scroll_x, pbouncy_brick->base.y,
407                    32,32, 
408                    plevel->bkgd_red, plevel->bkgd_green, plevel->bkgd_blue, 0);
409         }
410       else
411         {
412           s = (int)scroll_x / 30;
413           texture_draw_part(&plevel->img_bkgd, dest.x + s, dest.y, 
414                             dest.x, dest.y,dest.w,dest.h);
415         }
416
417       drawshape(pbouncy_brick->base.x - scroll_x,
418                 pbouncy_brick->base.y + pbouncy_brick->offset,
419                 pbouncy_brick->shape);
420     }
421 }
422
423 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
424 {
425   pfloating_score->base.x = x;
426   pfloating_score->base.y = y - 16;
427   timer_init(&pfloating_score->timer,true);
428   timer_start(&pfloating_score->timer,1000);
429   pfloating_score->value = s;
430 }
431
432 void floating_score_action(floating_score_type* pfloating_score)
433 {
434   pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
435
436   if(!timer_check(&pfloating_score->timer))
437     world.floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
438 }
439
440 void floating_score_draw(floating_score_type* pfloating_score)
441 {
442   char str[10];
443   sprintf(str, "%d", pfloating_score->value);
444   text_draw(&gold_text, str, (int)pfloating_score->base.x + 16 - strlen(str) * 8, (int)pfloating_score->base.y, 1);
445 }
446
447 /* Break a brick: */
448 void trybreakbrick(float x, float y, bool small)
449 {
450   Level* plevel = GameSession::current()->get_level();
451   
452   Tile* tile = gettile(x, y);
453   if (tile->brick)
454     {
455       if (tile->data > 0)
456         {
457           /* Get a distro from it: */
458           world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
459                                   (int)(y / 32) * 32);
460
461           if (!counting_distros)
462             {
463               counting_distros = true;
464               distro_counter = 50;
465             }
466
467           if (distro_counter <= 0)
468             plevel->change(x, y, TM_IA, tile->next_tile);
469
470           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
471           score = score + SCORE_DISTRO;
472           distros++;
473         }
474       else if (!small)
475         {
476           /* Get rid of it: */
477           plevel->change(x, y, TM_IA, tile->next_tile);
478           
479           /* Replace it with broken bits: */
480           world.add_broken_brick(tile, 
481                                  ((int)(x + 1) / 32) * 32,
482                                  (int)(y / 32) * 32);
483           
484           /* Get some score: */
485           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
486           score = score + SCORE_BRICK;
487         }
488     }
489 }
490
491 /* Empty a box: */
492 void tryemptybox(float x, float y, int col_side)
493 {
494   Level* plevel = GameSession::current()->get_level();
495
496   Tile* tile = gettile(x,y);
497   if (!tile->fullbox)
498     return;
499
500   // according to the collision side, set the upgrade direction
501   if(col_side == LEFT)
502     col_side = RIGHT;
503   else
504     col_side = LEFT;
505
506   switch(tile->data)
507     {
508     case 1: //'A':      /* Box with a distro! */
509       world.add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
510       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
511       score = score + SCORE_DISTRO;
512       distros++;
513       break;
514
515     case 2: // 'B':      /* Add an upgrade! */
516       if (tux.size == SMALL)     /* Tux is small, add mints! */
517         world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
518       else     /* Tux is big, add coffee: */
519         world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
520       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
521       break;
522
523     case 3:// '!':     /* Add a golden herring */
524       world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
525       break;
526     default:
527       break;
528     }
529
530   /* Empty the box: */
531   plevel->change(x, y, TM_IA, tile->next_tile);
532 }
533
534 /* Try to grab a distro: */
535 void trygrabdistro(float x, float y, int bounciness)
536 {
537   Level* plevel = GameSession::current()->get_level();
538   Tile* tile = gettile(x, y);
539   if (tile && tile->distro)
540     {
541       plevel->change(x, y, TM_IA, tile->next_tile);
542       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
543
544       if (bounciness == BOUNCE)
545         {
546           world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
547                                   (int)(y / 32) * 32);
548         }
549
550       score = score + SCORE_DISTRO;
551       distros++;
552     }
553 }
554
555 /* Try to bump a bad guy from below: */
556 void trybumpbadguy(float x, float y)
557 {
558   /* Bad guys: */
559   for (unsigned int i = 0; i < world.bad_guys.size(); i++)
560     {
561       if (world.bad_guys[i].base.x >= x - 32 && world.bad_guys[i].base.x <= x + 32 &&
562           world.bad_guys[i].base.y >= y - 16 && world.bad_guys[i].base.y <= y + 16)
563         {
564           world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
565         }
566     }
567
568
569   /* Upgrades: */
570   for (unsigned int i = 0; i < world.upgrades.size(); i++)
571     {
572       if (world.upgrades[i].base.height == 32 &&
573           world.upgrades[i].base.x >= x - 32 && world.upgrades[i].base.x <= x + 32 &&
574           world.upgrades[i].base.y >= y - 16 && world.upgrades[i].base.y <= y + 16)
575         {
576           world.upgrades[i].base.xm = -world.upgrades[i].base.xm;
577           world.upgrades[i].base.ym = -8;
578           play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
579         }
580     }
581 }
582
583 /* EOF */
584