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