4 // Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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.
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.
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.
30 #include "resources.h"
32 Surface* img_distro[4];
34 World* World::current_ = 0;
36 World::World(const std::string& filename)
38 // FIXME: Move this to action and draw and everywhere else where the
39 // world calls child functions
42 level = new Level(filename);
47 get_level()->load_gfx();
49 activate_particle_systems();
50 get_level()->load_song();
53 World::World(const std::string& subset, int level_nr)
55 // FIXME: Move this to action and draw and everywhere else where the
56 // world calls child functions
59 level = new Level(subset, level_nr);
64 get_level()->load_gfx();
66 activate_particle_systems();
67 get_level()->load_song();
81 player_status.score_multiplier = 1;
83 counting_distros = false;
86 /* set current song/music */
87 set_current_music(LEVEL_MUSIC);
91 World::activate_bad_guys()
93 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
94 i != level->badguy_data.end();
97 add_bad_guy(i->x, i->y, i->kind);
102 World::activate_particle_systems()
104 if (level->particle_system == "clouds")
106 particle_systems.push_back(new CloudParticleSystem);
108 else if (level->particle_system == "snow")
110 particle_systems.push_back(new SnowParticleSystem);
112 else if (level->particle_system != "")
114 st_abort("unknown particle system specified in level", "");
123 /* Draw the real background */
124 if(get_level()->bkgd_image[0] != '\0')
126 int s = ((int)scroll_x / 2)%640;
127 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
128 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
132 drawgradient(level->bkgd_top, level->bkgd_bottom);
135 /* Draw particle systems (background) */
136 std::vector<ParticleSystem*>::iterator p;
137 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
139 (*p)->draw(scroll_x, 0, 0);
142 /* Draw background: */
143 for (y = 0; y < 15; ++y)
145 for (x = 0; x < 21; ++x)
147 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
148 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
152 /* Draw interactive tiles: */
153 for (y = 0; y < 15; ++y)
155 for (x = 0; x < 21; ++x)
157 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
158 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
162 /* (Bouncy bricks): */
163 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
164 bouncy_bricks[i].draw();
166 for (unsigned int i = 0; i < bad_guys.size(); ++i)
171 for (unsigned int i = 0; i < bullets.size(); ++i)
174 for (unsigned int i = 0; i < floating_scores.size(); ++i)
175 floating_scores[i].draw();
177 for (unsigned int i = 0; i < upgrades.size(); ++i)
180 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
181 bouncy_distros[i].draw();
183 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
184 broken_bricks[i].draw();
186 /* Draw foreground: */
187 for (y = 0; y < 15; ++y)
189 for (x = 0; x < 21; ++x)
191 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
192 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
196 /* Draw particle systems (foreground) */
197 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
199 (*p)->draw(scroll_x, 0, 1);
204 World::action(double frame_ratio)
206 tux.action(frame_ratio);
208 /* Handle bouncy distros: */
209 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
210 bouncy_distros[i].action(frame_ratio);
212 /* Handle broken bricks: */
213 for (unsigned int i = 0; i < broken_bricks.size(); i++)
214 broken_bricks[i].action(frame_ratio);
216 /* Handle distro counting: */
217 if (counting_distros)
221 if (distro_counter <= 0)
222 counting_distros = -1;
225 // Handle all kinds of game objects
226 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
227 bouncy_bricks[i].action(frame_ratio);
229 for (unsigned int i = 0; i < floating_scores.size(); i++)
230 floating_scores[i].action(frame_ratio);
232 for (unsigned int i = 0; i < bullets.size(); ++i)
233 bullets[i].action(frame_ratio);
235 for (unsigned int i = 0; i < upgrades.size(); i++)
236 upgrades[i].action(frame_ratio);
238 for (unsigned int i = 0; i < bad_guys.size(); i++)
239 bad_guys[i].action(frame_ratio);
241 /* update particle systems */
242 std::vector<ParticleSystem*>::iterator p;
243 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
245 (*p)->simulate(frame_ratio);
248 /* Handle all possible collisions. */
254 World::collision_handler()
256 // CO_BULLET & CO_BADGUY check
257 for(unsigned int i = 0; i < bullets.size(); ++i)
259 for(unsigned int j = 0; j < bad_guys.size(); ++j)
261 if(bad_guys[j].dying != DYING_NOT)
263 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
265 // We have detected a collision and now call the
266 // collision functions of the collided objects.
267 // collide with bad_guy first, since bullet_collision will
269 bad_guys[j].collision(0, CO_BULLET);
270 bullets[i].collision(CO_BADGUY);
271 break; // bullet is invalid now, so break
276 /* CO_BADGUY & CO_BADGUY check */
277 for(unsigned int i = 0; i < bad_guys.size(); ++i)
279 if(bad_guys[i].dying != DYING_NOT)
282 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
284 if(j == i || bad_guys[j].dying != DYING_NOT)
287 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
289 // We have detected a collision and now call the
290 // collision functions of the collided objects.
291 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
292 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
297 if(tux.dying != DYING_NOT) return;
299 // CO_BADGUY & CO_PLAYER check
300 for(unsigned int i = 0; i < bad_guys.size(); ++i)
302 if(bad_guys[i].dying != DYING_NOT)
305 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
307 // We have detected a collision and now call the collision
308 // functions of the collided objects.
309 if (tux.previous_base.y < tux.base.y &&
310 tux.previous_base.y + tux.previous_base.height
311 < bad_guys[i].base.y + bad_guys[i].base.height/2)
313 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
317 tux.collision(&bad_guys[i], CO_BADGUY);
318 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
323 // CO_UPGRADE & CO_PLAYER check
324 for(unsigned int i = 0; i < upgrades.size(); ++i)
326 if(rectcollision(&upgrades[i].base, &tux.base))
328 // We have detected a collision and now call the collision
329 // functions of the collided objects.
330 upgrades[i].collision(&tux, CO_PLAYER);
336 World::add_score(float x, float y, int s)
338 player_status.score += s;
340 FloatingScore new_floating_score;
341 new_floating_score.init(x,y,s);
342 floating_scores.push_back(new_floating_score);
346 World::add_bouncy_distro(float x, float y)
348 BouncyDistro new_bouncy_distro;
349 new_bouncy_distro.init(x,y);
350 bouncy_distros.push_back(new_bouncy_distro);
354 World::add_broken_brick(Tile* tile, float x, float y)
356 add_broken_brick_piece(tile, x, y, -1, -4);
357 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
359 add_broken_brick_piece(tile, x + 16, y, 1, -4);
360 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
364 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
366 BrokenBrick new_broken_brick;
367 new_broken_brick.init(tile, x, y, xm, ym);
368 broken_bricks.push_back(new_broken_brick);
372 World::add_bouncy_brick(float x, float y)
374 BouncyBrick new_bouncy_brick;
375 new_bouncy_brick.init(x,y);
376 bouncy_bricks.push_back(new_bouncy_brick);
380 World::add_bad_guy(float x, float y, BadGuyKind kind)
382 bad_guys.push_back(BadGuy());
383 BadGuy& new_bad_guy = bad_guys.back();
385 new_bad_guy.init(x,y,kind);
389 World::add_upgrade(float x, float y, int dir, UpgradeKind kind)
392 new_upgrade.init(x,y,dir,kind);
393 upgrades.push_back(new_upgrade);
397 World::add_bullet(float x, float y, float xm, int dir)
400 new_bullet.init(x,y,xm,dir);
401 bullets.push_back(new_bullet);
403 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
408 World::trybreakbrick(float x, float y, bool small)
410 Level* plevel = get_level();
412 Tile* tile = gettile(x, y);
417 /* Get a distro from it: */
418 add_bouncy_distro(((int)(x + 1) / 32) * 32,
421 if (!counting_distros)
423 counting_distros = true;
427 if (distro_counter <= 0)
428 plevel->change(x, y, TM_IA, tile->next_tile);
430 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
431 player_status.score = player_status.score + SCORE_DISTRO;
432 player_status.distros++;
437 plevel->change(x, y, TM_IA, tile->next_tile);
439 /* Replace it with broken bits: */
440 add_broken_brick(tile,
441 ((int)(x + 1) / 32) * 32,
444 /* Get some score: */
445 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
446 player_status.score = player_status.score + SCORE_BRICK;
453 World::tryemptybox(float x, float y, int col_side)
455 Tile* tile = gettile(x,y);
459 // according to the collision side, set the upgrade direction
465 int posx = ((int)(x+1) / 32) * 32;
466 int posy = (int)(y/32) * 32 - 32;
469 case 1: // Box with a distro!
470 add_bouncy_distro(posx, posy);
471 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
472 player_status.score = player_status.score + SCORE_DISTRO;
473 player_status.distros++;
476 case 2: // Add an upgrade!
477 if (tux.size == SMALL) /* Tux is small, add mints! */
478 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
479 else /* Tux is big, add an iceflower: */
480 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
481 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
484 case 3: // Add a golden herring
485 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
488 case 4: // Add a 1up extra
489 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
496 level->change(x, y, TM_IA, tile->next_tile);
499 /* Try to grab a distro: */
501 World::trygrabdistro(float x, float y, int bounciness)
503 Tile* tile = gettile(x, y);
504 if (tile && tile->distro)
506 level->change(x, y, TM_IA, tile->next_tile);
507 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
509 if (bounciness == BOUNCE)
511 add_bouncy_distro(((int)(x + 1) / 32) * 32,
515 player_status.score = player_status.score + SCORE_DISTRO;
516 player_status.distros++;
520 /* Try to bump a bad guy from below: */
522 World::trybumpbadguy(float x, float y)
525 for (unsigned int i = 0; i < bad_guys.size(); i++)
527 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
528 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
530 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
536 for (unsigned int i = 0; i < upgrades.size(); i++)
538 if (upgrades[i].base.height == 32 &&
539 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
540 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
542 upgrades[i].base.xm = -upgrades[i].base.xm;
543 upgrades[i].base.ym = -8;
544 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);