4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
34 #include "resources.h"
36 Surface* img_distro[4];
38 World* World::current_ = 0;
40 World::World(const std::string& filename)
42 // FIXME: Move this to action and draw and everywhere else where the
43 // world calls child functions
46 level = new Level(filename);
51 get_level()->load_gfx();
53 activate_particle_systems();
54 get_level()->load_song();
59 World::World(const std::string& subset, int level_nr)
61 // FIXME: Move this to action and draw and everywhere else where the
62 // world calls child functions
65 level = new Level(subset, level_nr);
70 get_level()->load_gfx();
72 activate_particle_systems();
73 get_level()->load_song();
79 World::apply_bonuses()
81 // Apply bonuses from former levels
82 switch (player_status.bonus)
84 case PlayerStatus::NO_BONUS:
87 case PlayerStatus::FLOWER_BONUS:
88 tux.got_coffee = true;
91 case PlayerStatus::GROWUP_BONUS:
92 // FIXME: Move this to Player class
102 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
105 for (ParticleSystems::iterator i = particle_systems.begin();
106 i != particle_systems.end(); ++i)
109 for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
110 i != bouncy_distros.end(); ++i)
113 for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
114 i != broken_bricks.end(); ++i)
117 for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
118 i != bouncy_bricks.end(); ++i)
121 for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
122 i != floating_scores.end(); ++i)
129 World::set_defaults()
134 player_status.score_multiplier = 1;
136 counting_distros = false;
139 /* set current song/music */
140 currentmusic = LEVEL_MUSIC;
144 World::activate_bad_guys()
146 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
147 i != level->badguy_data.end();
150 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
155 World::activate_particle_systems()
157 if (level->particle_system == "clouds")
159 particle_systems.push_back(new CloudParticleSystem);
161 else if (level->particle_system == "snow")
163 particle_systems.push_back(new SnowParticleSystem);
165 else if (level->particle_system != "")
167 st_abort("unknown particle system specified in level", "");
176 /* Draw the real background */
177 if(get_level()->bkgd_image[0] != '\0')
179 int s = ((int)scroll_x / 2)%640;
180 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
181 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
185 drawgradient(level->bkgd_top, level->bkgd_bottom);
188 /* Draw particle systems (background) */
189 std::vector<ParticleSystem*>::iterator p;
190 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
192 (*p)->draw(scroll_x, 0, 0);
195 /* Draw background: */
196 for (y = 0; y < 15; ++y)
198 for (x = 0; x < 21; ++x)
200 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
201 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
205 /* Draw interactive tiles: */
206 for (y = 0; y < 15; ++y)
208 for (x = 0; x < 21; ++x)
210 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
211 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
215 /* (Bouncy bricks): */
216 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
217 bouncy_bricks[i]->draw();
219 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
224 for (unsigned int i = 0; i < bullets.size(); ++i)
227 for (unsigned int i = 0; i < floating_scores.size(); ++i)
228 floating_scores[i]->draw();
230 for (unsigned int i = 0; i < upgrades.size(); ++i)
233 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
234 bouncy_distros[i]->draw();
236 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
237 broken_bricks[i]->draw();
239 /* Draw foreground: */
240 for (y = 0; y < 15; ++y)
242 for (x = 0; x < 21; ++x)
244 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
245 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
249 /* Draw particle systems (foreground) */
250 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
252 (*p)->draw(scroll_x, 0, 1);
257 World::action(double frame_ratio)
259 tux.action(frame_ratio);
262 /* Handle bouncy distros: */
263 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
264 bouncy_distros[i]->action(frame_ratio);
266 /* Handle broken bricks: */
267 for (unsigned int i = 0; i < broken_bricks.size(); i++)
268 broken_bricks[i]->action(frame_ratio);
270 // Handle all kinds of game objects
271 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
272 bouncy_bricks[i]->action(frame_ratio);
274 for (unsigned int i = 0; i < floating_scores.size(); i++)
275 floating_scores[i]->action(frame_ratio);
277 for (unsigned int i = 0; i < bullets.size(); ++i)
278 bullets[i].action(frame_ratio);
280 for (unsigned int i = 0; i < upgrades.size(); i++)
281 upgrades[i].action(frame_ratio);
283 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
284 (*i)->action(frame_ratio);
286 /* update particle systems */
287 std::vector<ParticleSystem*>::iterator p;
288 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
290 (*p)->simulate(frame_ratio);
293 /* Handle all possible collisions. */
296 // Cleanup marked badguys
297 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
298 /* ++i handled at end of the loop */) {
299 if ((*i)->is_removable()) {
301 i = bad_guys.erase(i);
308 // the space that it takes for the screen to start scrolling
311 /* This functions takes cares of the scrolling */
312 void World::keep_in_bounds()
314 int tux_pos_x = (int)(tux.base.x - (tux.base.width/2));
316 scroll_x += screen->w/2;
318 if (scroll_x < tux_pos_x - X_SPACE)
319 scroll_x = tux_pos_x - X_SPACE;
320 else if (scroll_x > tux_pos_x + X_SPACE && level->back_scrolling)
321 scroll_x = tux_pos_x + X_SPACE;
323 scroll_x -= screen->w/2;
331 World::collision_handler()
333 // CO_BULLET & CO_BADGUY check
334 for(unsigned int i = 0; i < bullets.size(); ++i)
336 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
338 if((*j)->dying != DYING_NOT)
341 if(rectcollision(bullets[i].base, (*j)->base))
343 // We have detected a collision and now call the
344 // collision functions of the collided objects.
345 // collide with bad_guy first, since bullet_collision will
347 (*j)->collision(0, CO_BULLET);
348 bullets[i].collision(CO_BADGUY);
349 break; // bullet is invalid now, so break
354 /* CO_BADGUY & CO_BADGUY check */
355 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
357 if((*i)->dying != DYING_NOT)
360 BadGuys::iterator j = i;
362 for (; j != bad_guys.end(); ++j)
364 if(j == i || (*j)->dying != DYING_NOT)
367 if(rectcollision((*i)->base, (*j)->base))
369 // We have detected a collision and now call the
370 // collision functions of the collided objects.
371 (*j)->collision(*i, CO_BADGUY);
372 (*i)->collision(*j, CO_BADGUY);
377 if(tux.dying != DYING_NOT) return;
379 // CO_BADGUY & CO_PLAYER check
380 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
382 if((*i)->dying != DYING_NOT)
385 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
387 // We have detected a collision and now call the collision
388 // functions of the collided objects.
389 if (tux.previous_base.y < tux.base.y &&
390 tux.previous_base.y + tux.previous_base.height
391 < (*i)->base.y + (*i)->base.height/2
392 && !tux.invincible_timer.started())
394 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
398 tux.collision(*i, CO_BADGUY);
399 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
404 // CO_UPGRADE & CO_PLAYER check
405 for(unsigned int i = 0; i < upgrades.size(); ++i)
407 if(rectcollision(upgrades[i].base, tux.base))
409 // We have detected a collision and now call the collision
410 // functions of the collided objects.
411 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
417 World::add_score(float x, float y, int s)
419 player_status.score += s;
421 FloatingScore* new_floating_score = new FloatingScore();
422 new_floating_score->init(x,y,s);
423 floating_scores.push_back(new_floating_score);
427 World::add_bouncy_distro(float x, float y)
429 BouncyDistro* new_bouncy_distro = new BouncyDistro();
430 new_bouncy_distro->init(x,y);
431 bouncy_distros.push_back(new_bouncy_distro);
435 World::add_broken_brick(Tile* tile, float x, float y)
437 add_broken_brick_piece(tile, x, y, -1, -4);
438 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
440 add_broken_brick_piece(tile, x + 16, y, 1, -4);
441 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
445 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
447 BrokenBrick* new_broken_brick = new BrokenBrick();
448 new_broken_brick->init(tile, x, y, xm, ym);
449 broken_bricks.push_back(new_broken_brick);
453 World::add_bouncy_brick(float x, float y)
455 BouncyBrick* new_bouncy_brick = new BouncyBrick();
456 new_bouncy_brick->init(x,y);
457 bouncy_bricks.push_back(new_bouncy_brick);
461 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
463 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
464 bad_guys.push_back(badguy);
469 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
472 new_upgrade.init(x,y,dir,kind);
473 upgrades.push_back(new_upgrade);
477 World::add_bullet(float x, float y, float xm, Direction dir)
479 if(bullets.size() > MAX_BULLETS-1)
483 new_bullet.init(x,y,xm,dir);
484 bullets.push_back(new_bullet);
486 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
490 World::play_music(int musictype)
492 currentmusic = musictype;
493 switch(currentmusic) {
495 music_manager->play_music(get_level()->get_level_music_fast());
498 music_manager->play_music(get_level()->get_level_music());
501 music_manager->play_music(herring_song);
504 music_manager->halt_music();
510 World::get_music_type()
517 World::trybreakbrick(float x, float y, bool small)
519 Level* plevel = get_level();
521 Tile* tile = gettile(x, y);
526 /* Get a distro from it: */
527 add_bouncy_distro(((int)(x + 1) / 32) * 32,
530 // TODO: don't handle this in a global way but per-tile...
531 if (!counting_distros)
533 counting_distros = true;
541 if (distro_counter <= 0)
543 counting_distros = false;
544 plevel->change(x, y, TM_IA, tile->next_tile);
547 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
548 player_status.score = player_status.score + SCORE_DISTRO;
549 player_status.distros++;
554 plevel->change(x, y, TM_IA, tile->next_tile);
556 /* Replace it with broken bits: */
557 add_broken_brick(tile,
558 ((int)(x + 1) / 32) * 32,
561 /* Get some score: */
562 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
563 player_status.score = player_status.score + SCORE_BRICK;
570 World::tryemptybox(float x, float y, Direction col_side)
572 Tile* tile = gettile(x,y);
576 // according to the collision side, set the upgrade direction
582 int posx = ((int)(x+1) / 32) * 32;
583 int posy = (int)(y/32) * 32 - 32;
586 case 1: // Box with a distro!
587 add_bouncy_distro(posx, posy);
588 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
589 player_status.score = player_status.score + SCORE_DISTRO;
590 player_status.distros++;
593 case 2: // Add an upgrade!
594 if (tux.size == SMALL) /* Tux is small, add mints! */
595 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
596 else /* Tux is big, add an iceflower: */
597 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
598 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
601 case 3: // Add a golden herring
602 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
605 case 4: // Add a 1up extra
606 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
613 level->change(x, y, TM_IA, tile->next_tile);
616 /* Try to grab a distro: */
618 World::trygrabdistro(float x, float y, int bounciness)
620 Tile* tile = gettile(x, y);
621 if (tile && tile->distro)
623 level->change(x, y, TM_IA, tile->next_tile);
624 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
626 if (bounciness == BOUNCE)
628 add_bouncy_distro(((int)(x + 1) / 32) * 32,
632 player_status.score = player_status.score + SCORE_DISTRO;
633 player_status.distros++;
637 /* Try to bump a bad guy from below: */
639 World::trybumpbadguy(float x, float y)
642 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
644 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
645 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
647 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
652 for (unsigned int i = 0; i < upgrades.size(); i++)
654 if (upgrades[i].base.height == 32 &&
655 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
656 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
658 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);