- change gameobjs names from lower case to upper case
[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 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
254 void
255 World::add_score(float x, float y, int s)
256 {
257   score += s;
258
259   FloatingScore new_floating_score;
260   new_floating_score.init(x,y,s);
261   floating_scores.push_back(new_floating_score);
262 }
263
264 void
265 World::add_bouncy_distro(float x, float y)
266 {
267   BouncyDistro new_bouncy_distro;
268   new_bouncy_distro.init(x,y);
269   bouncy_distros.push_back(new_bouncy_distro);
270 }
271
272 void
273 World::add_broken_brick(Tile* tile, float x, float y)
274 {
275   add_broken_brick_piece(tile, x, y, -1, -4);
276   add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
277
278   add_broken_brick_piece(tile, x + 16, y, 1, -4);
279   add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
280 }
281
282 void
283 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
284 {
285   BrokenBrick new_broken_brick;
286   new_broken_brick.init(tile, x, y, xm, ym);
287   broken_bricks.push_back(new_broken_brick);
288 }
289
290 void
291 World::add_bouncy_brick(float x, float y)
292 {
293   BouncyBrick new_bouncy_brick;
294   new_bouncy_brick.init(x,y);
295   bouncy_bricks.push_back(new_bouncy_brick);
296 }
297
298 void
299 World::add_bad_guy(float x, float y, BadGuyKind kind)
300 {
301   bad_guys.push_back(BadGuy());
302   BadGuy& new_bad_guy = bad_guys.back();
303   
304   new_bad_guy.init(x,y,kind);
305 }
306
307 void
308 World::add_upgrade(float x, float y, int dir, int kind)
309 {
310   upgrade_type new_upgrade;
311   upgrade_init(&new_upgrade,x,y,dir,kind);
312   upgrades.push_back(new_upgrade);
313 }
314
315 void 
316 World::add_bullet(float x, float y, float xm, int dir)
317 {
318   bullet_type new_bullet;
319   bullet_init(&new_bullet,x,y,xm,dir);
320   bullets.push_back(new_bullet);
321   
322   play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
323 }
324
325 /* Break a brick: */
326 void
327 World::trybreakbrick(float x, float y, bool small)
328 {
329   Level* plevel = get_level();
330   
331   Tile* tile = gettile(x, y);
332   if (tile->brick)
333     {
334       if (tile->data > 0)
335         {
336           /* Get a distro from it: */
337           add_bouncy_distro(((int)(x + 1) / 32) * 32,
338                                   (int)(y / 32) * 32);
339
340           if (!counting_distros)
341             {
342               counting_distros = true;
343               distro_counter = 50;
344             }
345
346           if (distro_counter <= 0)
347             plevel->change(x, y, TM_IA, tile->next_tile);
348
349           play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
350           score = score + SCORE_DISTRO;
351           distros++;
352         }
353       else if (!small)
354         {
355           /* Get rid of it: */
356           plevel->change(x, y, TM_IA, tile->next_tile);
357           
358           /* Replace it with broken bits: */
359           add_broken_brick(tile, 
360                                  ((int)(x + 1) / 32) * 32,
361                                  (int)(y / 32) * 32);
362           
363           /* Get some score: */
364           play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
365           score = score + SCORE_BRICK;
366         }
367     }
368 }
369
370 /* Empty a box: */
371 void
372 World::tryemptybox(float x, float y, int col_side)
373 {
374   Tile* tile = gettile(x,y);
375   if (!tile->fullbox)
376     return;
377
378   // according to the collision side, set the upgrade direction
379   if(col_side == LEFT)
380     col_side = RIGHT;
381   else
382     col_side = LEFT;
383
384   switch(tile->data)
385     {
386     case 1: // Box with a distro!
387       add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
388       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
389       score = score + SCORE_DISTRO;
390       distros++;
391       break;
392
393     case 2: // Add an upgrade!
394       if (tux.size == SMALL)     /* Tux is small, add mints! */
395         add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
396       else     /* Tux is big, add coffee: */
397         add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
398       play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
399       break;
400
401     case 3: // Add a golden herring
402       add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
403       break;
404     default:
405       break;
406     }
407
408   /* Empty the box: */
409   level->change(x, y, TM_IA, tile->next_tile);
410 }
411
412 /* Try to grab a distro: */
413 void
414 World::trygrabdistro(float x, float y, int bounciness)
415 {
416   Tile* tile = gettile(x, y);
417   if (tile && tile->distro)
418     {
419       level->change(x, y, TM_IA, tile->next_tile);
420       play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
421
422       if (bounciness == BOUNCE)
423         {
424           add_bouncy_distro(((int)(x + 1) / 32) * 32,
425                                   (int)(y / 32) * 32);
426         }
427
428       score = score + SCORE_DISTRO;
429       distros++;
430     }
431 }
432
433 /* Try to bump a bad guy from below: */
434 void
435 World::trybumpbadguy(float x, float y)
436 {
437   /* Bad guys: */
438   for (unsigned int i = 0; i < bad_guys.size(); i++)
439     {
440       if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
441           bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
442         {
443           bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
444         }
445     }
446
447
448   /* Upgrades: */
449   for (unsigned int i = 0; i < upgrades.size(); i++)
450     {
451       if (upgrades[i].base.height == 32 &&
452           upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
453           upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
454         {
455           upgrades[i].base.xm = -upgrades[i].base.xm;
456           upgrades[i].base.ym = -8;
457           play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
458         }
459     }
460 }
461
462 /* EOF */
463