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();
58 scrolling_timer.init(true);
61 World::World(const std::string& subset, int level_nr)
63 // FIXME: Move this to action and draw and everywhere else where the
64 // world calls child functions
67 level = new Level(subset, level_nr);
72 get_level()->load_gfx();
74 activate_particle_systems();
75 get_level()->load_song();
79 scrolling_timer.init(true);
83 World::apply_bonuses()
85 // Apply bonuses from former levels
86 switch (player_status.bonus)
88 case PlayerStatus::NO_BONUS:
91 case PlayerStatus::FLOWER_BONUS:
92 tux.got_power = tux.FIRE_POWER; // FIXME: add ice power to here
95 case PlayerStatus::GROWUP_BONUS:
96 // FIXME: Move this to Player class
106 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
109 for (ParticleSystems::iterator i = particle_systems.begin();
110 i != particle_systems.end(); ++i)
113 for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
114 i != bouncy_distros.end(); ++i)
117 for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
118 i != broken_bricks.end(); ++i)
121 for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
122 i != bouncy_bricks.end(); ++i)
125 for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
126 i != floating_scores.end(); ++i)
133 World::set_defaults()
138 player_status.score_multiplier = 1;
140 counting_distros = false;
143 /* set current song/music */
144 currentmusic = LEVEL_MUSIC;
148 World::activate_bad_guys()
150 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
151 i != level->badguy_data.end();
154 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
159 World::activate_particle_systems()
161 if (level->particle_system == "clouds")
163 particle_systems.push_back(new CloudParticleSystem);
165 else if (level->particle_system == "snow")
167 particle_systems.push_back(new SnowParticleSystem);
169 else if (level->particle_system != "")
171 st_abort("unknown particle system specified in level", "");
180 /* Draw the real background */
181 drawgradient(level->bkgd_top, level->bkgd_bottom);
186 /* Draw particle systems (background) */
187 std::vector<ParticleSystem*>::iterator p;
188 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
190 (*p)->draw(scroll_x, 0, 0);
193 /* Draw background: */
194 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
196 for (x = 0; x < VISIBLE_TILES_X; ++x)
198 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
199 level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
203 /* Draw interactive tiles: */
204 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
206 for (x = 0; x < VISIBLE_TILES_X; ++x)
208 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
209 level->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
213 /* (Bouncy bricks): */
214 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
215 bouncy_bricks[i]->draw();
217 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
222 for (unsigned int i = 0; i < bullets.size(); ++i)
225 for (unsigned int i = 0; i < floating_scores.size(); ++i)
226 floating_scores[i]->draw();
228 for (unsigned int i = 0; i < upgrades.size(); ++i)
231 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
232 bouncy_distros[i]->draw();
234 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
235 broken_bricks[i]->draw();
237 /* Draw foreground: */
238 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
240 for (x = 0; x < VISIBLE_TILES_X; ++x)
242 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
243 level->fg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
247 /* Draw particle systems (foreground) */
248 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
250 (*p)->draw(scroll_x, 0, 1);
255 World::action(double frame_ratio)
257 tux.action(frame_ratio);
258 tux.check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
259 scrolling(frame_ratio);
261 /* Handle bouncy distros: */
262 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
263 bouncy_distros[i]->action(frame_ratio);
265 /* Handle broken bricks: */
266 for (unsigned int i = 0; i < broken_bricks.size(); i++)
267 broken_bricks[i]->action(frame_ratio);
269 // Handle all kinds of game objects
270 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
271 bouncy_bricks[i]->action(frame_ratio);
273 for (unsigned int i = 0; i < floating_scores.size(); i++)
274 floating_scores[i]->action(frame_ratio);
276 for (unsigned int i = 0; i < bullets.size(); ++i)
277 bullets[i].action(frame_ratio);
279 for (unsigned int i = 0; i < upgrades.size(); i++)
280 upgrades[i].action(frame_ratio);
282 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
283 (*i)->action(frame_ratio);
285 /* update particle systems */
286 std::vector<ParticleSystem*>::iterator p;
287 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
289 (*p)->simulate(frame_ratio);
292 /* Handle all possible collisions. */
295 // Cleanup marked badguys
296 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
297 /* ++i handled at end of the loop */) {
298 if ((*i)->is_removable()) {
300 i = bad_guys.erase(i);
307 /* the space that it takes for the screen to start scrolling, regarding */
308 /* screen bounds (in pixels) */
309 // should be higher than screen->w/2 (400)
310 #define X_SPACE (500-16)
311 // should be less than screen->h/2 (300)
314 // the time it takes to move the camera (in ms)
315 #define CHANGE_DIR_SCROLL_SPEED 2000
317 /* This functions takes cares of the scrolling */
318 void World::scrolling(double frame_ratio)
320 /* Y-axis scrolling */
322 float tux_pos_y = tux.base.y + (tux.base.height/2);
324 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
325 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
326 else if (scroll_y > tux_pos_y - Y_SPACE)
327 scroll_y = tux_pos_y - Y_SPACE;
329 // this code prevent the screen to scroll before the start or after the level's end
332 else if(scroll_y > level->height * 32 - screen->h)
333 scroll_y = level->height * 32 - screen->h;
337 //std::cerr << "Level too short!!" << std::endl;
341 /* X-axis scrolling */
344 if(level->hor_autoscroll_speed)
346 scroll_x += level->hor_autoscroll_speed * frame_ratio;
351 /* Horizontal backscrolling */
352 float tux_pos_x = tux.base.x + (tux.base.width/2);
354 if(tux.old_dir != tux.dir && level->back_scrolling)
355 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
359 if (tux.physic.get_velocity_x() > 0)
361 else if (tux.physic.get_velocity_x() < 0)
365 if (tux.dir == RIGHT)
371 if(scrolling_timer.check())
373 float final_scroll_x;
375 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
377 final_scroll_x = tux_pos_x - X_SPACE;
379 scroll_x += (final_scroll_x - scroll_x)
380 / (frame_ratio * (CHANGE_DIR_SCROLL_SPEED / 100))
381 + (tux.physic.get_velocity_x() * frame_ratio + tux.physic.get_acceleration_x() * frame_ratio * frame_ratio);
385 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
386 scroll_x = tux_pos_x - (screen->w - X_SPACE);
387 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
388 scroll_x = tux_pos_x - X_SPACE;
391 // this code prevent the screen to scroll before the start or after the level's end
394 else if(scroll_x > level->width * 32 - screen->w)
395 scroll_x = level->width * 32 - screen->w;
399 World::collision_handler()
401 // CO_BULLET & CO_BADGUY check
402 for(unsigned int i = 0; i < bullets.size(); ++i)
404 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
406 if((*j)->dying != DYING_NOT)
409 if(rectcollision(bullets[i].base, (*j)->base))
411 // We have detected a collision and now call the
412 // collision functions of the collided objects.
413 // collide with bad_guy first, since bullet_collision will
415 (*j)->collision(&bullets[i], CO_BULLET);
416 bullets[i].collision(CO_BADGUY);
417 break; // bullet is invalid now, so break
422 /* CO_BADGUY & CO_BADGUY check */
423 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
425 if((*i)->dying != DYING_NOT)
428 BadGuys::iterator j = i;
430 for (; j != bad_guys.end(); ++j)
432 if(j == i || (*j)->dying != DYING_NOT)
435 if(rectcollision((*i)->base, (*j)->base))
437 // We have detected a collision and now call the
438 // collision functions of the collided objects.
439 (*j)->collision(*i, CO_BADGUY);
440 (*i)->collision(*j, CO_BADGUY);
445 if(tux.dying != DYING_NOT) return;
447 // CO_BADGUY & CO_PLAYER check
448 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
450 if((*i)->dying != DYING_NOT)
453 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
455 // We have detected a collision and now call the collision
456 // functions of the collided objects.
457 if (tux.previous_base.y < tux.base.y &&
458 tux.previous_base.y + tux.previous_base.height
459 < (*i)->base.y + (*i)->base.height/2
460 && !tux.invincible_timer.started())
462 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
466 tux.collision(*i, CO_BADGUY);
467 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
472 // CO_UPGRADE & CO_PLAYER check
473 for(unsigned int i = 0; i < upgrades.size(); ++i)
475 if(rectcollision(upgrades[i].base, tux.base))
477 // We have detected a collision and now call the collision
478 // functions of the collided objects.
479 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
485 World::add_score(float x, float y, int s)
487 player_status.score += s;
489 FloatingScore* new_floating_score = new FloatingScore();
490 new_floating_score->init(x-scroll_x, y-scroll_y, s);
491 floating_scores.push_back(new_floating_score);
495 World::add_bouncy_distro(float x, float y)
497 BouncyDistro* new_bouncy_distro = new BouncyDistro();
498 new_bouncy_distro->init(x, y);
499 bouncy_distros.push_back(new_bouncy_distro);
503 World::add_broken_brick(Tile* tile, float x, float y)
505 add_broken_brick_piece(tile, x, y, -1, -4);
506 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
508 add_broken_brick_piece(tile, x + 16, y, 1, -4);
509 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
513 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
515 BrokenBrick* new_broken_brick = new BrokenBrick();
516 new_broken_brick->init(tile, x, y, xm, ym);
517 broken_bricks.push_back(new_broken_brick);
521 World::add_bouncy_brick(float x, float y)
523 BouncyBrick* new_bouncy_brick = new BouncyBrick();
524 new_bouncy_brick->init(x,y);
525 bouncy_bricks.push_back(new_bouncy_brick);
529 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
531 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
532 bad_guys.push_back(badguy);
537 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
540 new_upgrade.init(x,y,dir,kind);
541 upgrades.push_back(new_upgrade);
545 World::add_bullet(float x, float y, float xm, Direction dir)
547 if(tux.got_power == tux.FIRE_POWER)
549 if(bullets.size() > MAX_FIRE_BULLETS-1)
552 else if(tux.got_power == tux.ICE_POWER)
554 if(bullets.size() > MAX_ICE_BULLETS-1)
559 if(tux.got_power == tux.FIRE_POWER)
560 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
561 else if(tux.got_power == tux.ICE_POWER)
562 new_bullet.init(x,y,xm,dir, ICE_BULLET);
563 bullets.push_back(new_bullet);
565 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
569 World::play_music(int musictype)
571 currentmusic = musictype;
572 switch(currentmusic) {
574 music_manager->play_music(get_level()->get_level_music_fast());
577 music_manager->play_music(get_level()->get_level_music());
580 music_manager->play_music(herring_song);
583 music_manager->halt_music();
589 World::get_music_type()
596 World::trybreakbrick(float x, float y, bool small)
598 Level* plevel = get_level();
600 Tile* tile = gettile(x, y);
605 /* Get a distro from it: */
606 add_bouncy_distro(((int)(x + 1) / 32) * 32,
609 // TODO: don't handle this in a global way but per-tile...
610 if (!counting_distros)
612 counting_distros = true;
620 if (distro_counter <= 0)
622 counting_distros = false;
623 plevel->change(x, y, TM_IA, tile->next_tile);
626 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
627 player_status.score = player_status.score + SCORE_DISTRO;
628 player_status.distros++;
633 plevel->change(x, y, TM_IA, tile->next_tile);
635 /* Replace it with broken bits: */
636 add_broken_brick(tile,
637 ((int)(x + 1) / 32) * 32,
640 /* Get some score: */
641 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
642 player_status.score = player_status.score + SCORE_BRICK;
649 World::tryemptybox(float x, float y, Direction col_side)
651 Tile* tile = gettile(x,y);
655 // according to the collision side, set the upgrade direction
661 int posx = ((int)(x+1) / 32) * 32;
662 int posy = (int)(y/32) * 32 - 32;
665 case 1: // Box with a distro!
666 add_bouncy_distro(posx, posy);
667 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
668 player_status.score = player_status.score + SCORE_DISTRO;
669 player_status.distros++;
672 case 2: // Add a fire flower upgrade!
673 if (tux.size == SMALL) /* Tux is small, add mints! */
674 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
675 else /* Tux is big, add a fireflower: */
676 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
677 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
680 case 5: // Add an ice flower upgrade!
681 if (tux.size == SMALL) /* Tux is small, add mints! */
682 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
683 else /* Tux is big, add an iceflower: */
684 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
685 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
688 case 3: // Add a golden herring
689 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
692 case 4: // Add a 1up extra
693 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
700 level->change(x, y, TM_IA, tile->next_tile);
703 /* Try to grab a distro: */
705 World::trygrabdistro(float x, float y, int bounciness)
707 Tile* tile = gettile(x, y);
708 if (tile && tile->distro)
710 level->change(x, y, TM_IA, tile->next_tile);
711 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
713 if (bounciness == BOUNCE)
715 add_bouncy_distro(((int)(x + 1) / 32) * 32,
719 player_status.score = player_status.score + SCORE_DISTRO;
720 player_status.distros++;
724 /* Try to bump a bad guy from below: */
726 World::trybumpbadguy(float x, float y)
729 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
731 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
732 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
734 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
739 for (unsigned int i = 0; i < upgrades.size(); i++)
741 if (upgrades[i].base.height == 32 &&
742 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
743 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
745 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);