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