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 && !tux.dying)
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;
397 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
399 final_scroll_x = tux_pos_x - X_SPACE;
401 if((tux.physic.get_velocity_x() > 0 && tux.dir == RIGHT) || (tux.physic.get_velocity_x() < 0 && tux.dir == LEFT))
412 float number = 2.5/(frame_ratio * CHANGE_DIR_SCROLL_SPEED/1000)*exp((CHANGE_DIR_SCROLL_SPEED-scrolling_timer.get_left())/1400.);
413 if(left) number *= -1.;
416 + constant1 * tux.physic.get_velocity_x() * frame_ratio
417 + constant2 * tux.physic.get_acceleration_x() * frame_ratio * frame_ratio;
419 if ((right && final_scroll_x - scroll_x < 0) || (left && final_scroll_x - scroll_x > 0))
420 scroll_x = final_scroll_x;
425 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
426 scroll_x = tux_pos_x - (screen->w - X_SPACE);
427 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
428 scroll_x = tux_pos_x - X_SPACE;
431 // this code prevent the screen to scroll before the start or after the level's end
432 if(scroll_x > level->width * 32 - screen->w)
433 scroll_x = level->width * 32 - screen->w;
439 World::collision_handler()
441 // CO_BULLET & CO_BADGUY check
442 for(unsigned int i = 0; i < bullets.size(); ++i)
444 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
446 if((*j)->dying != DYING_NOT)
449 if(rectcollision(bullets[i].base, (*j)->base))
451 // We have detected a collision and now call the
452 // collision functions of the collided objects.
453 // collide with bad_guy first, since bullet_collision will
455 (*j)->collision(&bullets[i], CO_BULLET);
456 bullets[i].collision(CO_BADGUY);
457 break; // bullet is invalid now, so break
462 /* CO_BADGUY & CO_BADGUY check */
463 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
465 if((*i)->dying != DYING_NOT)
468 BadGuys::iterator j = i;
470 for (; j != bad_guys.end(); ++j)
472 if(j == i || (*j)->dying != DYING_NOT)
475 if(rectcollision((*i)->base, (*j)->base))
477 // We have detected a collision and now call the
478 // collision functions of the collided objects.
479 (*j)->collision(*i, CO_BADGUY);
480 (*i)->collision(*j, CO_BADGUY);
485 if(tux.dying != DYING_NOT) return;
487 // CO_BADGUY & CO_PLAYER check
488 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
490 if((*i)->dying != DYING_NOT)
493 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
495 // We have detected a collision and now call the collision
496 // functions of the collided objects.
497 if (tux.previous_base.y < tux.base.y &&
498 tux.previous_base.y + tux.previous_base.height
499 < (*i)->base.y + (*i)->base.height/2
500 && !tux.invincible_timer.started())
502 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
506 tux.collision(*i, CO_BADGUY);
507 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
512 // CO_UPGRADE & CO_PLAYER check
513 for(unsigned int i = 0; i < upgrades.size(); ++i)
515 if(rectcollision(upgrades[i].base, tux.base))
517 // We have detected a collision and now call the collision
518 // functions of the collided objects.
519 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
523 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
524 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
526 if (rectcollision((*i)->base, tux.base))
528 if (tux.previous_base.y < tux.base.y &&
529 tux.previous_base.y + tux.previous_base.height
530 < (*i)->base.y + (*i)->base.height/2)
532 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
534 else if (tux.previous_base.y <= tux.base.y)
536 tux.collision(*i, CO_TRAMPOLINE);
537 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
544 World::add_score(float x, float y, int s)
546 player_status.score += s;
548 FloatingScore* new_floating_score = new FloatingScore();
549 new_floating_score->init(x-scroll_x, y-scroll_y, s);
550 floating_scores.push_back(new_floating_score);
554 World::add_bouncy_distro(float x, float y)
556 BouncyDistro* new_bouncy_distro = new BouncyDistro();
557 new_bouncy_distro->init(x, y);
558 bouncy_distros.push_back(new_bouncy_distro);
562 World::add_broken_brick(Tile* tile, float x, float y)
564 add_broken_brick_piece(tile, x, y, -1, -4);
565 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
567 add_broken_brick_piece(tile, x + 16, y, 1, -4);
568 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
572 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
574 BrokenBrick* new_broken_brick = new BrokenBrick();
575 new_broken_brick->init(tile, x, y, xm, ym);
576 broken_bricks.push_back(new_broken_brick);
580 World::add_bouncy_brick(float x, float y)
582 BouncyBrick* new_bouncy_brick = new BouncyBrick();
583 new_bouncy_brick->init(x,y);
584 bouncy_bricks.push_back(new_bouncy_brick);
588 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
590 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
591 bad_guys.push_back(badguy);
595 template<class T, class U>
597 World::add_object(U data)
599 T* tobject = new T(data);
601 if (data.type == OBJ_TRAMPOLINE)
602 trampolines.push_back(tobject);
608 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
611 new_upgrade.init(x,y,dir,kind);
612 upgrades.push_back(new_upgrade);
616 World::add_bullet(float x, float y, float xm, Direction dir)
618 if(tux.got_power == tux.FIRE_POWER)
620 if(bullets.size() > MAX_FIRE_BULLETS-1)
623 else if(tux.got_power == tux.ICE_POWER)
625 if(bullets.size() > MAX_ICE_BULLETS-1)
630 if(tux.got_power == tux.FIRE_POWER)
631 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
632 else if(tux.got_power == tux.ICE_POWER)
633 new_bullet.init(x,y,xm,dir, ICE_BULLET);
634 bullets.push_back(new_bullet);
636 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
640 World::play_music(int musictype)
642 currentmusic = musictype;
643 switch(currentmusic) {
645 music_manager->play_music(get_level()->get_level_music_fast());
648 music_manager->play_music(get_level()->get_level_music());
651 music_manager->play_music(herring_song);
654 music_manager->halt_music();
660 World::get_music_type()
667 World::trybreakbrick(float x, float y, bool small)
669 Level* plevel = get_level();
671 Tile* tile = gettile(x, y);
676 /* Get a distro from it: */
677 add_bouncy_distro(((int)(x + 1) / 32) * 32,
680 // TODO: don't handle this in a global way but per-tile...
681 if (!counting_distros)
683 counting_distros = true;
691 if (distro_counter <= 0)
693 counting_distros = false;
694 plevel->change(x, y, TM_IA, tile->next_tile);
697 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
698 player_status.score = player_status.score + SCORE_DISTRO;
699 player_status.distros++;
705 plevel->change(x, y, TM_IA, tile->next_tile);
707 /* Replace it with broken bits: */
708 add_broken_brick(tile,
709 ((int)(x + 1) / 32) * 32,
712 /* Get some score: */
713 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
714 player_status.score = player_status.score + SCORE_BRICK;
725 World::tryemptybox(float x, float y, Direction col_side)
727 Tile* tile = gettile(x,y);
731 // according to the collision side, set the upgrade direction
737 int posx = ((int)(x+1) / 32) * 32;
738 int posy = (int)(y/32) * 32 - 32;
741 case 1: // Box with a distro!
742 add_bouncy_distro(posx, posy);
743 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
744 player_status.score = player_status.score + SCORE_DISTRO;
745 player_status.distros++;
748 case 2: // Add a fire flower upgrade!
749 if (tux.size == SMALL) /* Tux is small, add mints! */
750 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
751 else /* Tux is big, add a fireflower: */
752 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
753 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
756 case 5: // Add an ice flower upgrade!
757 if (tux.size == SMALL) /* Tux is small, add mints! */
758 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
759 else /* Tux is big, add an iceflower: */
760 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
761 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
764 case 3: // Add a golden herring
765 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
768 case 4: // Add a 1up extra
769 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
776 level->change(x, y, TM_IA, tile->next_tile);
779 /* Try to grab a distro: */
781 World::trygrabdistro(float x, float y, int bounciness)
783 Tile* tile = gettile(x, y);
784 if (tile && tile->distro)
786 level->change(x, y, TM_IA, tile->next_tile);
787 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
789 if (bounciness == BOUNCE)
791 add_bouncy_distro(((int)(x + 1) / 32) * 32,
795 player_status.score = player_status.score + SCORE_DISTRO;
796 player_status.distros++;
800 /* Try to bump a bad guy from below: */
802 World::trybumpbadguy(float x, float y)
805 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
807 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
808 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
810 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
815 for (unsigned int i = 0; i < upgrades.size(); i++)
817 if (upgrades[i].base.height == 32 &&
818 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
819 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
821 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);