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"
37 Surface* img_distro[4];
39 World* World::current_ = 0;
41 World::World(const std::string& filename)
43 // FIXME: Move this to action and draw and everywhere else where the
44 // world calls child functions
47 level = new Level(filename);
52 get_level()->load_gfx();
55 activate_particle_systems();
56 get_level()->load_song();
60 scrolling_timer.init(true);
63 World::World(const std::string& subset, int level_nr)
65 // FIXME: Move this to action and draw and everywhere else where the
66 // world calls child functions
69 level = new Level(subset, level_nr);
74 get_level()->load_gfx();
77 activate_particle_systems();
78 get_level()->load_song();
82 scrolling_timer.init(true);
86 World::apply_bonuses()
88 // Apply bonuses from former levels
89 switch (player_status.bonus)
91 case PlayerStatus::NO_BONUS:
94 case PlayerStatus::FLOWER_BONUS:
95 tux.got_power = tux.FIRE_POWER; // FIXME: add ice power to here
98 case PlayerStatus::GROWUP_BONUS:
99 // FIXME: Move this to Player class
101 tux.base.height = 64;
109 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
112 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
115 for (ParticleSystems::iterator i = particle_systems.begin();
116 i != particle_systems.end(); ++i)
119 for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
120 i != bouncy_distros.end(); ++i)
123 for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
124 i != broken_bricks.end(); ++i)
127 for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
128 i != bouncy_bricks.end(); ++i)
131 for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
132 i != floating_scores.end(); ++i)
139 World::set_defaults()
144 player_status.score_multiplier = 1;
146 counting_distros = false;
149 /* set current song/music */
150 currentmusic = LEVEL_MUSIC;
154 World::activate_bad_guys()
156 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
157 i != level->badguy_data.end();
160 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
165 World::activate_objects()
167 for (std::vector< ObjectData<TrampolineData> >::iterator i = level->trampoline_data.begin();
168 i != level->trampoline_data.end();
171 add_object<Trampoline, ObjectData<TrampolineData> >(*i);
176 World::activate_particle_systems()
178 if (level->particle_system == "clouds")
180 particle_systems.push_back(new CloudParticleSystem);
182 else if (level->particle_system == "snow")
184 particle_systems.push_back(new SnowParticleSystem);
186 else if (level->particle_system != "")
188 st_abort("unknown particle system specified in level", "");
197 /* Draw the real background */
198 drawgradient(level->bkgd_top, level->bkgd_bottom);
203 /* Draw particle systems (background) */
204 std::vector<ParticleSystem*>::iterator p;
205 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
207 (*p)->draw(scroll_x, 0, 0);
210 /* Draw background: */
211 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
213 for (x = 0; x < VISIBLE_TILES_X; ++x)
215 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
216 level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
220 /* Draw interactive tiles: */
221 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
223 for (x = 0; x < VISIBLE_TILES_X; ++x)
225 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
226 level->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
230 /* (Bouncy bricks): */
231 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
232 bouncy_bricks[i]->draw();
234 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
237 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
242 for (unsigned int i = 0; i < bullets.size(); ++i)
245 for (unsigned int i = 0; i < floating_scores.size(); ++i)
246 floating_scores[i]->draw();
248 for (unsigned int i = 0; i < upgrades.size(); ++i)
251 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
252 bouncy_distros[i]->draw();
254 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
255 broken_bricks[i]->draw();
257 /* Draw foreground: */
258 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
260 for (x = 0; x < VISIBLE_TILES_X; ++x)
262 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
263 level->fg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
267 /* Draw particle systems (foreground) */
268 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
270 (*p)->draw(scroll_x, 0, 1);
275 World::action(double frame_ratio)
277 tux.action(frame_ratio);
278 tux.check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
279 scrolling(frame_ratio);
281 /* Handle bouncy distros: */
282 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
283 bouncy_distros[i]->action(frame_ratio);
285 /* Handle broken bricks: */
286 for (unsigned int i = 0; i < broken_bricks.size(); i++)
287 broken_bricks[i]->action(frame_ratio);
289 // Handle all kinds of game objects
290 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
291 bouncy_bricks[i]->action(frame_ratio);
293 for (unsigned int i = 0; i < floating_scores.size(); i++)
294 floating_scores[i]->action(frame_ratio);
296 for (unsigned int i = 0; i < bullets.size(); ++i)
297 bullets[i].action(frame_ratio);
299 for (unsigned int i = 0; i < upgrades.size(); i++)
300 upgrades[i].action(frame_ratio);
302 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
303 (*i)->action(frame_ratio);
305 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
306 (*i)->action(frame_ratio);
308 /* update particle systems */
309 std::vector<ParticleSystem*>::iterator p;
310 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
312 (*p)->simulate(frame_ratio);
315 /* Handle all possible collisions. */
318 // Cleanup marked badguys
319 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
320 /* ++i handled at end of the loop */) {
321 if ((*i)->is_removable()) {
323 i = bad_guys.erase(i);
330 /* the space that it takes for the screen to start scrolling, regarding */
331 /* screen bounds (in pixels) */
332 // should be higher than screen->w/2 (400)
333 #define X_SPACE (500-16)
334 // should be less than screen->h/2 (300)
337 // the time it takes to move the camera (in ms)
338 #define CHANGE_DIR_SCROLL_SPEED 2000
340 /* This functions takes cares of the scrolling */
341 void World::scrolling(double frame_ratio)
343 /* Y-axis scrolling */
345 float tux_pos_y = tux.base.y + (tux.base.height/2);
347 if(level->height > VISIBLE_TILES_Y-1)
349 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
350 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
351 else if (scroll_y > tux_pos_y - Y_SPACE)
352 scroll_y = tux_pos_y - Y_SPACE;
355 // this code prevent the screen to scroll before the start or after the level's end
356 if(scroll_y > level->height * 32 - screen->h)
357 scroll_y = level->height * 32 - screen->h;
361 /* X-axis scrolling */
364 if(level->hor_autoscroll_speed)
366 scroll_x += level->hor_autoscroll_speed * frame_ratio;
371 /* Horizontal backscrolling */
372 float tux_pos_x = tux.base.x + (tux.base.width/2);
374 if(tux.old_dir != tux.dir && level->back_scrolling)
375 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
379 if (tux.physic.get_velocity_x() > 0)
381 else if (tux.physic.get_velocity_x() < 0)
385 if (tux.dir == RIGHT)
391 if(scrolling_timer.check())
393 float final_scroll_x;
395 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
397 final_scroll_x = tux_pos_x - X_SPACE;
399 scroll_x += (final_scroll_x - scroll_x)
400 / (frame_ratio * (CHANGE_DIR_SCROLL_SPEED / 100))
401 + (tux.physic.get_velocity_x() * frame_ratio + tux.physic.get_acceleration_x() * frame_ratio * frame_ratio);
405 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
406 scroll_x = tux_pos_x - (screen->w - X_SPACE);
407 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
408 scroll_x = tux_pos_x - X_SPACE;
411 // this code prevent the screen to scroll before the start or after the level's end
412 if(scroll_x > level->width * 32 - screen->w)
413 scroll_x = level->width * 32 - screen->w;
419 World::collision_handler()
421 // CO_BULLET & CO_BADGUY check
422 for(unsigned int i = 0; i < bullets.size(); ++i)
424 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
426 if((*j)->dying != DYING_NOT)
429 if(rectcollision(bullets[i].base, (*j)->base))
431 // We have detected a collision and now call the
432 // collision functions of the collided objects.
433 // collide with bad_guy first, since bullet_collision will
435 (*j)->collision(&bullets[i], CO_BULLET);
436 bullets[i].collision(CO_BADGUY);
437 break; // bullet is invalid now, so break
442 /* CO_BADGUY & CO_BADGUY check */
443 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
445 if((*i)->dying != DYING_NOT)
448 BadGuys::iterator j = i;
450 for (; j != bad_guys.end(); ++j)
452 if(j == i || (*j)->dying != DYING_NOT)
455 if(rectcollision((*i)->base, (*j)->base))
457 // We have detected a collision and now call the
458 // collision functions of the collided objects.
459 (*j)->collision(*i, CO_BADGUY);
460 (*i)->collision(*j, CO_BADGUY);
465 if(tux.dying != DYING_NOT) return;
467 // CO_BADGUY & CO_PLAYER check
468 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
470 if((*i)->dying != DYING_NOT)
473 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
475 // We have detected a collision and now call the collision
476 // functions of the collided objects.
477 if (tux.previous_base.y < tux.base.y &&
478 tux.previous_base.y + tux.previous_base.height
479 < (*i)->base.y + (*i)->base.height/2
480 && !tux.invincible_timer.started())
482 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
486 tux.collision(*i, CO_BADGUY);
487 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
492 // CO_UPGRADE & CO_PLAYER check
493 for(unsigned int i = 0; i < upgrades.size(); ++i)
495 if(rectcollision(upgrades[i].base, tux.base))
497 // We have detected a collision and now call the collision
498 // functions of the collided objects.
499 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
503 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
504 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
506 if (rectcollision((*i)->base, tux.base))
508 if (tux.previous_base.y < tux.base.y &&
509 tux.previous_base.y + tux.previous_base.height
510 < (*i)->base.y + (*i)->base.height/2)
512 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
514 else if (tux.previous_base.y <= tux.base.y)
516 tux.collision(*i, CO_TRAMPOLINE);
517 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
524 World::add_score(float x, float y, int s)
526 player_status.score += s;
528 FloatingScore* new_floating_score = new FloatingScore();
529 new_floating_score->init(x-scroll_x, y-scroll_y, s);
530 floating_scores.push_back(new_floating_score);
534 World::add_bouncy_distro(float x, float y)
536 BouncyDistro* new_bouncy_distro = new BouncyDistro();
537 new_bouncy_distro->init(x, y);
538 bouncy_distros.push_back(new_bouncy_distro);
542 World::add_broken_brick(Tile* tile, float x, float y)
544 add_broken_brick_piece(tile, x, y, -1, -4);
545 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
547 add_broken_brick_piece(tile, x + 16, y, 1, -4);
548 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
552 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
554 BrokenBrick* new_broken_brick = new BrokenBrick();
555 new_broken_brick->init(tile, x, y, xm, ym);
556 broken_bricks.push_back(new_broken_brick);
560 World::add_bouncy_brick(float x, float y)
562 BouncyBrick* new_bouncy_brick = new BouncyBrick();
563 new_bouncy_brick->init(x,y);
564 bouncy_bricks.push_back(new_bouncy_brick);
568 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
570 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
571 bad_guys.push_back(badguy);
575 template<class T, class U>
577 World::add_object(U data)
579 T* tobject = new T(data);
581 if (data.type == OBJ_TRAMPOLINE)
582 trampolines.push_back(tobject);
588 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
591 new_upgrade.init(x,y,dir,kind);
592 upgrades.push_back(new_upgrade);
596 World::add_bullet(float x, float y, float xm, Direction dir)
598 if(tux.got_power == tux.FIRE_POWER)
600 if(bullets.size() > MAX_FIRE_BULLETS-1)
603 else if(tux.got_power == tux.ICE_POWER)
605 if(bullets.size() > MAX_ICE_BULLETS-1)
610 if(tux.got_power == tux.FIRE_POWER)
611 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
612 else if(tux.got_power == tux.ICE_POWER)
613 new_bullet.init(x,y,xm,dir, ICE_BULLET);
614 bullets.push_back(new_bullet);
616 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
620 World::play_music(int musictype)
622 currentmusic = musictype;
623 switch(currentmusic) {
625 music_manager->play_music(get_level()->get_level_music_fast());
628 music_manager->play_music(get_level()->get_level_music());
631 music_manager->play_music(herring_song);
634 music_manager->halt_music();
640 World::get_music_type()
647 World::trybreakbrick(float x, float y, bool small)
649 Level* plevel = get_level();
651 Tile* tile = gettile(x, y);
656 /* Get a distro from it: */
657 add_bouncy_distro(((int)(x + 1) / 32) * 32,
660 // TODO: don't handle this in a global way but per-tile...
661 if (!counting_distros)
663 counting_distros = true;
671 if (distro_counter <= 0)
673 counting_distros = false;
674 plevel->change(x, y, TM_IA, tile->next_tile);
677 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
678 player_status.score = player_status.score + SCORE_DISTRO;
679 player_status.distros++;
684 plevel->change(x, y, TM_IA, tile->next_tile);
686 /* Replace it with broken bits: */
687 add_broken_brick(tile,
688 ((int)(x + 1) / 32) * 32,
691 /* Get some score: */
692 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
693 player_status.score = player_status.score + SCORE_BRICK;
700 World::tryemptybox(float x, float y, Direction col_side)
702 Tile* tile = gettile(x,y);
706 // according to the collision side, set the upgrade direction
712 int posx = ((int)(x+1) / 32) * 32;
713 int posy = (int)(y/32) * 32 - 32;
716 case 1: // Box with a distro!
717 add_bouncy_distro(posx, posy);
718 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
719 player_status.score = player_status.score + SCORE_DISTRO;
720 player_status.distros++;
723 case 2: // Add a fire flower upgrade!
724 if (tux.size == SMALL) /* Tux is small, add mints! */
725 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
726 else /* Tux is big, add a fireflower: */
727 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
728 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
731 case 5: // Add an ice flower upgrade!
732 if (tux.size == SMALL) /* Tux is small, add mints! */
733 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
734 else /* Tux is big, add an iceflower: */
735 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
736 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
739 case 3: // Add a golden herring
740 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
743 case 4: // Add a 1up extra
744 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
751 level->change(x, y, TM_IA, tile->next_tile);
754 /* Try to grab a distro: */
756 World::trygrabdistro(float x, float y, int bounciness)
758 Tile* tile = gettile(x, y);
759 if (tile && tile->distro)
761 level->change(x, y, TM_IA, tile->next_tile);
762 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
764 if (bounciness == BOUNCE)
766 add_bouncy_distro(((int)(x + 1) / 32) * 32,
770 player_status.score = player_status.score + SCORE_DISTRO;
771 player_status.distros++;
775 /* Try to bump a bad guy from below: */
777 World::trybumpbadguy(float x, float y)
780 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
782 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
783 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
785 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
790 for (unsigned int i = 0; i < upgrades.size(); i++)
792 if (upgrades[i].base.height == 32 &&
793 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
794 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
796 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);