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 #include "display_manager.h"
38 #include "background.h"
40 Surface* img_distro[4];
42 World* World::current_ = 0;
44 World::World(const std::string& filename)
46 // FIXME: Move this to action and draw and everywhere else where the
47 // world calls child functions
50 level = new Level(filename);
55 get_level()->load_gfx();
58 activate_particle_systems();
59 Background* bg = new Background(displaymanager);
61 bg->set_image(level->img_bkgd, level->bkgd_speed);
63 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
65 gameobjects.push_back(bg);
67 get_level()->load_song();
71 scrolling_timer.init(true);
74 World::World(const std::string& subset, int level_nr)
76 // FIXME: Move this to action and draw and everywhere else where the
77 // world calls child functions
80 level = new Level(subset, level_nr);
85 get_level()->load_gfx();
88 activate_particle_systems();
89 Background* bg = new Background(displaymanager);
91 bg->set_image(level->img_bkgd, level->bkgd_speed);
93 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
95 gameobjects.push_back(bg);
96 get_level()->load_song();
100 scrolling_timer.init(true);
104 World::apply_bonuses()
106 // Apply bonuses from former levels
107 switch (player_status.bonus)
109 case PlayerStatus::NO_BONUS:
112 case PlayerStatus::FLOWER_BONUS:
113 tux.got_power = tux.FIRE_POWER; // FIXME: add ice power to here
116 case PlayerStatus::GROWUP_BONUS:
117 // FIXME: Move this to Player class
119 tux.base.height = 64;
127 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
130 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
133 for (std::vector<_GameObject*>::iterator i = gameobjects.begin();
134 i != gameobjects.end(); ++i) {
135 Drawable* drawable = dynamic_cast<Drawable*> (*i);
137 displaymanager.remove_drawable(drawable);
145 World::set_defaults()
150 player_status.score_multiplier = 1;
152 counting_distros = false;
155 /* set current song/music */
156 currentmusic = LEVEL_MUSIC;
160 World::activate_bad_guys()
162 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
163 i != level->badguy_data.end();
166 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
171 World::activate_objects()
173 for (std::vector< ObjectData<TrampolineData> >::iterator i = level->trampoline_data.begin();
174 i != level->trampoline_data.end();
177 add_object<Trampoline, ObjectData<TrampolineData> >(*i);
182 World::activate_particle_systems()
184 if (level->particle_system == "clouds")
186 gameobjects.push_back(new CloudParticleSystem(displaymanager));
188 else if (level->particle_system == "snow")
190 gameobjects.push_back(new SnowParticleSystem(displaymanager));
192 else if (level->particle_system != "")
194 st_abort("unknown particle system specified in level", "");
203 /* Draw the real background */
205 drawgradient(level->bkgd_top, level->bkgd_bottom);
210 /* Draw particle systems (background) */
211 displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y));
212 displaymanager.draw();
214 /* Draw background: */
215 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
217 for (x = 0; x < VISIBLE_TILES_X; ++x)
219 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
220 level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
224 /* Draw interactive tiles: */
225 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
227 for (x = 0; x < VISIBLE_TILES_X; ++x)
229 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
230 level->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
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 < upgrades.size(); ++i)
248 /* Draw foreground: */
249 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
251 for (x = 0; x < VISIBLE_TILES_X; ++x)
253 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
254 level->fg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
260 World::action(double frame_ratio)
262 tux.action(frame_ratio);
263 tux.check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
264 scrolling(frame_ratio);
266 for (unsigned int i = 0; i < bullets.size(); ++i)
267 bullets[i].action(frame_ratio);
269 for (unsigned int i = 0; i < upgrades.size(); i++)
270 upgrades[i].action(frame_ratio);
272 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
273 (*i)->action(frame_ratio);
275 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
276 (*i)->action(frame_ratio);
278 /* update particle systems */
279 for(std::vector<_GameObject*>::iterator i = gameobjects.begin();
280 i != gameobjects.end(); ++i)
281 (*i)->action(frame_ratio);
283 /* Handle all possible collisions. */
286 // Cleanup marked badguys
287 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
288 /* ++i handled at end of the loop */) {
289 if ((*i)->is_removable()) {
291 i = bad_guys.erase(i);
297 for(std::vector<_GameObject*>::iterator i = gameobjects.begin();
298 i != gameobjects.end(); /* nothing */) {
299 if((*i)->is_valid() == false) {
300 Drawable* drawable = dynamic_cast<Drawable*> (*i);
302 displaymanager.remove_drawable(drawable);
305 i = gameobjects.erase(i);
312 /* the space that it takes for the screen to start scrolling, regarding */
313 /* screen bounds (in pixels) */
314 // should be higher than screen->w/2 (400)
315 #define X_SPACE (500-16)
316 // should be less than screen->h/2 (300)
319 // the time it takes to move the camera (in ms)
320 #define CHANGE_DIR_SCROLL_SPEED 2000
322 /* This functions takes cares of the scrolling */
323 void World::scrolling(double frame_ratio)
325 /* Y-axis scrolling */
327 float tux_pos_y = tux.base.y + (tux.base.height/2);
329 if(level->height > VISIBLE_TILES_Y-1 && !tux.dying)
331 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
332 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
333 else if (scroll_y > tux_pos_y - Y_SPACE)
334 scroll_y = tux_pos_y - Y_SPACE;
337 // this code prevent the screen to scroll before the start or after the level's end
338 if(scroll_y > level->height * 32 - screen->h)
339 scroll_y = level->height * 32 - screen->h;
343 /* X-axis scrolling */
346 if(level->hor_autoscroll_speed)
348 scroll_x += level->hor_autoscroll_speed * frame_ratio;
353 /* Horizontal backscrolling */
354 float tux_pos_x = tux.base.x + (tux.base.width/2);
356 if(tux.old_dir != tux.dir && level->back_scrolling)
357 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
361 if (tux.physic.get_velocity_x() > 0)
363 else if (tux.physic.get_velocity_x() < 0)
367 if (tux.dir == RIGHT)
373 if(scrolling_timer.check())
375 float final_scroll_x;
379 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
381 final_scroll_x = tux_pos_x - X_SPACE;
383 if((tux.physic.get_velocity_x() > 0 && tux.dir == RIGHT) || (tux.physic.get_velocity_x() < 0 && tux.dir == LEFT))
394 float number = 2.5/(frame_ratio * CHANGE_DIR_SCROLL_SPEED/1000)*exp((CHANGE_DIR_SCROLL_SPEED-scrolling_timer.get_left())/1400.);
395 if(left) number *= -1.;
398 + constant1 * tux.physic.get_velocity_x() * frame_ratio
399 + constant2 * tux.physic.get_acceleration_x() * frame_ratio * frame_ratio;
401 if ((right && final_scroll_x - scroll_x < 0) || (left && final_scroll_x - scroll_x > 0))
402 scroll_x = final_scroll_x;
407 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
408 scroll_x = tux_pos_x - (screen->w - X_SPACE);
409 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
410 scroll_x = tux_pos_x - X_SPACE;
413 // this code prevent the screen to scroll before the start or after the level's end
414 if(scroll_x > level->width * 32 - screen->w)
415 scroll_x = level->width * 32 - screen->w;
421 World::collision_handler()
423 // CO_BULLET & CO_BADGUY check
424 for(unsigned int i = 0; i < bullets.size(); ++i)
426 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
428 if((*j)->dying != DYING_NOT)
431 if(rectcollision(bullets[i].base, (*j)->base))
433 // We have detected a collision and now call the
434 // collision functions of the collided objects.
435 // collide with bad_guy first, since bullet_collision will
437 (*j)->collision(&bullets[i], CO_BULLET);
438 bullets[i].collision(CO_BADGUY);
439 break; // bullet is invalid now, so break
444 /* CO_BADGUY & CO_BADGUY check */
445 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
447 if((*i)->dying != DYING_NOT)
450 BadGuys::iterator j = i;
452 for (; j != bad_guys.end(); ++j)
454 if(j == i || (*j)->dying != DYING_NOT)
457 if(rectcollision((*i)->base, (*j)->base))
459 // We have detected a collision and now call the
460 // collision functions of the collided objects.
461 (*j)->collision(*i, CO_BADGUY);
462 (*i)->collision(*j, CO_BADGUY);
467 if(tux.dying != DYING_NOT) return;
469 // CO_BADGUY & CO_PLAYER check
470 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
472 if((*i)->dying != DYING_NOT)
475 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
477 // We have detected a collision and now call the collision
478 // functions of the collided objects.
479 if (tux.previous_base.y < tux.base.y &&
480 tux.previous_base.y + tux.previous_base.height
481 < (*i)->base.y + (*i)->base.height/2
482 && !tux.invincible_timer.started())
484 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
488 tux.collision(*i, CO_BADGUY);
489 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
494 // CO_UPGRADE & CO_PLAYER check
495 for(unsigned int i = 0; i < upgrades.size(); ++i)
497 if(rectcollision(upgrades[i].base, tux.base))
499 // We have detected a collision and now call the collision
500 // functions of the collided objects.
501 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
505 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
506 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
508 if (rectcollision((*i)->base, tux.base))
510 if (tux.previous_base.y < tux.base.y &&
511 tux.previous_base.y + tux.previous_base.height
512 < (*i)->base.y + (*i)->base.height/2)
514 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
516 else if (tux.previous_base.y <= tux.base.y)
518 tux.collision(*i, CO_TRAMPOLINE);
519 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
526 World::add_score(const Vector& pos, int s)
528 player_status.score += s;
530 gameobjects.push_back(new FloatingScore(displaymanager, pos, s));
534 World::add_bouncy_distro(const Vector& pos)
536 gameobjects.push_back(new BouncyDistro(displaymanager, pos));
540 World::add_broken_brick(const Vector& pos, Tile* tile)
542 add_broken_brick_piece(pos, Vector(-1, -4), tile);
543 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
545 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
546 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
550 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
553 gameobjects.push_back(new BrokenBrick(displaymanager, tile, pos, movement));
557 World::add_bouncy_brick(const Vector& pos)
559 gameobjects.push_back(new BouncyBrick(displaymanager, pos));
563 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
565 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
566 bad_guys.push_back(badguy);
570 template<class T, class U>
572 World::add_object(U data)
574 T* tobject = new T(data);
576 if (data.type == OBJ_TRAMPOLINE)
577 trampolines.push_back(tobject);
583 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
586 new_upgrade.init(x,y,dir,kind);
587 upgrades.push_back(new_upgrade);
591 World::add_bullet(float x, float y, float xm, Direction dir)
593 if(tux.got_power == tux.FIRE_POWER)
595 if(bullets.size() > MAX_FIRE_BULLETS-1)
598 else if(tux.got_power == tux.ICE_POWER)
600 if(bullets.size() > MAX_ICE_BULLETS-1)
605 if(tux.got_power == tux.FIRE_POWER)
606 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
607 else if(tux.got_power == tux.ICE_POWER)
608 new_bullet.init(x,y,xm,dir, ICE_BULLET);
609 bullets.push_back(new_bullet);
611 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
615 World::play_music(int musictype)
617 currentmusic = musictype;
618 switch(currentmusic) {
620 music_manager->play_music(get_level()->get_level_music_fast());
623 music_manager->play_music(get_level()->get_level_music());
626 music_manager->play_music(herring_song);
629 music_manager->halt_music();
635 World::get_music_type()
642 World::trybreakbrick(float x, float y, bool small)
644 Level* plevel = get_level();
646 Tile* tile = gettile(x, y);
651 /* Get a distro from it: */
653 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
655 // TODO: don't handle this in a global way but per-tile...
656 if (!counting_distros)
658 counting_distros = true;
666 if (distro_counter <= 0)
668 counting_distros = false;
669 plevel->change(x, y, TM_IA, tile->next_tile);
672 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
673 player_status.score = player_status.score + SCORE_DISTRO;
674 player_status.distros++;
680 plevel->change(x, y, TM_IA, tile->next_tile);
682 /* Replace it with broken bits: */
683 add_broken_brick(Vector(
684 ((int)(x + 1) / 32) * 32,
685 (int)(y / 32) * 32), tile);
687 /* Get some score: */
688 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
689 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(Vector(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(Vector(((int)(x + 1) / 32) * 32,
767 (int)(y / 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);