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"
41 Surface* img_distro[4];
43 World* World::current_ = 0;
45 World::World(const std::string& filename)
47 // FIXME: Move this to action and draw and everywhere else where the
48 // world calls child functions
51 level = new Level(filename);
53 tux = new Player(displaymanager);
54 gameobjects.push_back(tux);
58 get_level()->load_gfx();
61 activate_particle_systems();
62 Background* bg = new Background(displaymanager);
64 bg->set_image(level->img_bkgd, level->bkgd_speed);
66 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
68 gameobjects.push_back(bg);
71 gameobjects.push_back(new TileMap(displaymanager, get_level()));
73 get_level()->load_song();
77 scrolling_timer.init(true);
80 World::World(const std::string& subset, int level_nr)
82 // FIXME: Move this to action and draw and everywhere else where the
83 // world calls child functions
86 level = new Level(subset, level_nr);
91 get_level()->load_gfx();
94 activate_particle_systems();
95 Background* bg = new Background(displaymanager);
97 bg->set_image(level->img_bkgd, level->bkgd_speed);
99 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
101 gameobjects.push_back(bg);
103 gameobjects.push_back(new TileMap(displaymanager, get_level()));
104 get_level()->load_song();
108 scrolling_timer.init(true);
112 World::apply_bonuses()
115 // Apply bonuses from former levels
116 switch (player_status.bonus)
118 case PlayerStatus::NO_BONUS:
121 case PlayerStatus::FLOWER_BONUS:
122 tux->got_power = tux.FIRE_POWER; // FIXME: add ice power to here
125 case PlayerStatus::GROWUP_BONUS:
126 // FIXME: Move this to Player class
128 tux->base.height = 64;
137 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
140 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
143 for (std::vector<_GameObject*>::iterator i = gameobjects.begin();
144 i != gameobjects.end(); ++i) {
145 Drawable* drawable = dynamic_cast<Drawable*> (*i);
147 displaymanager.remove_drawable(drawable);
155 World::set_defaults()
160 player_status.score_multiplier = 1;
162 counting_distros = false;
165 /* set current song/music */
166 currentmusic = LEVEL_MUSIC;
170 World::activate_bad_guys()
172 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
173 i != level->badguy_data.end();
176 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
181 World::activate_objects()
183 for (std::vector< ObjectData<TrampolineData> >::iterator i = level->trampoline_data.begin();
184 i != level->trampoline_data.end();
187 add_object<Trampoline, ObjectData<TrampolineData> >(*i);
192 World::activate_particle_systems()
194 if (level->particle_system == "clouds")
196 gameobjects.push_back(new CloudParticleSystem(displaymanager));
198 else if (level->particle_system == "snow")
200 gameobjects.push_back(new SnowParticleSystem(displaymanager));
202 else if (level->particle_system != "")
204 st_abort("unknown particle system specified in level", "");
212 displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y));
213 displaymanager.draw();
215 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
218 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
221 for (unsigned int i = 0; i < bullets.size(); ++i)
224 for (unsigned int i = 0; i < upgrades.size(); ++i)
229 World::action(double frame_ratio)
231 tux->check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
232 scrolling(frame_ratio);
234 for (unsigned int i = 0; i < bullets.size(); ++i)
235 bullets[i].action(frame_ratio);
237 for (unsigned int i = 0; i < upgrades.size(); i++)
238 upgrades[i].action(frame_ratio);
240 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
241 (*i)->action(frame_ratio);
243 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
244 (*i)->action(frame_ratio);
246 /* update objects (don't use iterators here, because the list might change
247 * during the iteration)
249 for(size_t i = 0; i < gameobjects.size(); ++i)
250 gameobjects[i]->action(frame_ratio);
252 /* Handle all possible collisions. */
255 // Cleanup marked badguys
256 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
257 /* ++i handled at end of the loop */) {
258 if ((*i)->is_removable()) {
260 i = bad_guys.erase(i);
266 for(std::vector<_GameObject*>::iterator i = gameobjects.begin();
267 i != gameobjects.end(); /* nothing */) {
268 if((*i)->is_valid() == false) {
269 Drawable* drawable = dynamic_cast<Drawable*> (*i);
271 displaymanager.remove_drawable(drawable);
274 i = gameobjects.erase(i);
281 /* the space that it takes for the screen to start scrolling, regarding */
282 /* screen bounds (in pixels) */
283 // should be higher than screen->w/2 (400)
284 #define X_SPACE (500-16)
285 // should be less than screen->h/2 (300)
288 // the time it takes to move the camera (in ms)
289 #define CHANGE_DIR_SCROLL_SPEED 2000
291 /* This functions takes cares of the scrolling */
292 void World::scrolling(double frame_ratio)
294 /* Y-axis scrolling */
296 float tux_pos_y = tux->base.y + (tux->base.height/2);
298 if(level->height > VISIBLE_TILES_Y-1 && !tux->dying)
300 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
301 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
302 else if (scroll_y > tux_pos_y - Y_SPACE)
303 scroll_y = tux_pos_y - Y_SPACE;
306 // this code prevent the screen to scroll before the start or after the level's end
307 if(scroll_y > level->height * 32 - screen->h)
308 scroll_y = level->height * 32 - screen->h;
312 /* X-axis scrolling */
315 if(level->hor_autoscroll_speed)
317 scroll_x += level->hor_autoscroll_speed * frame_ratio;
322 /* Horizontal backscrolling */
323 float tux_pos_x = tux->base.x + (tux->base.width/2);
325 if(tux->old_dir != tux->dir && level->back_scrolling)
326 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
330 if (tux->physic.get_velocity_x() > 0)
332 else if (tux->physic.get_velocity_x() < 0)
336 if (tux->dir == RIGHT)
342 if(scrolling_timer.check())
344 float final_scroll_x;
348 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
350 final_scroll_x = tux_pos_x - X_SPACE;
352 if((tux->physic.get_velocity_x() > 0 && tux->dir == RIGHT)
353 || (tux->physic.get_velocity_x() < 0 && tux->dir == LEFT))
364 float number = 2.5/(frame_ratio * CHANGE_DIR_SCROLL_SPEED/1000)*exp((CHANGE_DIR_SCROLL_SPEED-scrolling_timer.get_left())/1400.);
365 if(left) number *= -1.;
368 + constant1 * tux->physic.get_velocity_x() * frame_ratio
369 + constant2 * tux->physic.get_acceleration_x() * frame_ratio * frame_ratio;
371 if ((right && final_scroll_x - scroll_x < 0) || (left && final_scroll_x - scroll_x > 0))
372 scroll_x = final_scroll_x;
377 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
378 scroll_x = tux_pos_x - (screen->w - X_SPACE);
379 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
380 scroll_x = tux_pos_x - X_SPACE;
383 // this code prevent the screen to scroll before the start or after the level's end
384 if(scroll_x > level->width * 32 - screen->w)
385 scroll_x = level->width * 32 - screen->w;
391 World::collision_handler()
393 // CO_BULLET & CO_BADGUY check
394 for(unsigned int i = 0; i < bullets.size(); ++i)
396 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
398 if((*j)->dying != DYING_NOT)
401 if(rectcollision(bullets[i].base, (*j)->base))
403 // We have detected a collision and now call the
404 // collision functions of the collided objects.
405 // collide with bad_guy first, since bullet_collision will
407 (*j)->collision(&bullets[i], CO_BULLET);
408 bullets[i].collision(CO_BADGUY);
409 break; // bullet is invalid now, so break
414 /* CO_BADGUY & CO_BADGUY check */
415 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
417 if((*i)->dying != DYING_NOT)
420 BadGuys::iterator j = i;
422 for (; j != bad_guys.end(); ++j)
424 if(j == i || (*j)->dying != DYING_NOT)
427 if(rectcollision((*i)->base, (*j)->base))
429 // We have detected a collision and now call the
430 // collision functions of the collided objects.
431 (*j)->collision(*i, CO_BADGUY);
432 (*i)->collision(*j, CO_BADGUY);
437 if(tux->dying != DYING_NOT) return;
439 // CO_BADGUY & CO_PLAYER check
440 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
442 if((*i)->dying != DYING_NOT)
445 if(rectcollision_offset((*i)->base, tux->base, 0, 0))
447 // We have detected a collision and now call the collision
448 // functions of the collided objects.
449 if (tux->previous_base.y < tux->base.y &&
450 tux->previous_base.y + tux->previous_base.height
451 < (*i)->base.y + (*i)->base.height/2
452 && !tux->invincible_timer.started())
454 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
458 tux->collision(*i, CO_BADGUY);
459 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
464 // CO_UPGRADE & CO_PLAYER check
465 for(unsigned int i = 0; i < upgrades.size(); ++i)
467 if(rectcollision(upgrades[i].base, tux->base))
469 // We have detected a collision and now call the collision
470 // functions of the collided objects.
471 upgrades[i].collision(tux, CO_PLAYER, COLLISION_NORMAL);
475 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
476 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
478 if (rectcollision((*i)->base, tux->base))
480 if (tux->previous_base.y < tux->base.y &&
481 tux->previous_base.y + tux->previous_base.height
482 < (*i)->base.y + (*i)->base.height/2)
484 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
486 else if (tux->previous_base.y <= tux->base.y)
488 tux->collision(*i, CO_TRAMPOLINE);
489 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
496 World::add_score(const Vector& pos, int s)
498 player_status.score += s;
500 gameobjects.push_back(new FloatingScore(displaymanager, pos, s));
504 World::add_bouncy_distro(const Vector& pos)
506 gameobjects.push_back(new BouncyDistro(displaymanager, pos));
510 World::add_broken_brick(const Vector& pos, Tile* tile)
512 add_broken_brick_piece(pos, Vector(-1, -4), tile);
513 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
515 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
516 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
520 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
523 gameobjects.push_back(new BrokenBrick(displaymanager, tile, pos, movement));
527 World::add_bouncy_brick(const Vector& pos)
529 gameobjects.push_back(new BouncyBrick(displaymanager, pos));
533 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
535 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
536 bad_guys.push_back(badguy);
540 template<class T, class U>
542 World::add_object(U data)
544 T* tobject = new T(data);
546 if (data.type == OBJ_TRAMPOLINE)
547 trampolines.push_back(tobject);
553 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
556 new_upgrade.init(x,y,dir,kind);
557 upgrades.push_back(new_upgrade);
561 World::add_bullet(float x, float y, float xm, Direction dir)
563 if(tux->got_power == Player::FIRE_POWER)
565 if(bullets.size() > MAX_FIRE_BULLETS-1)
568 else if(tux->got_power == Player::ICE_POWER)
570 if(bullets.size() > MAX_ICE_BULLETS-1)
575 if(tux->got_power == Player::FIRE_POWER)
576 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
577 else if(tux->got_power == Player::ICE_POWER)
578 new_bullet.init(x,y,xm,dir, ICE_BULLET);
579 bullets.push_back(new_bullet);
581 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
585 World::play_music(int musictype)
587 currentmusic = musictype;
588 switch(currentmusic) {
590 music_manager->play_music(get_level()->get_level_music_fast());
593 music_manager->play_music(get_level()->get_level_music());
596 music_manager->play_music(herring_song);
599 music_manager->halt_music();
605 World::get_music_type()
612 World::trybreakbrick(float x, float y, bool small)
614 Level* plevel = get_level();
616 Tile* tile = gettile(x, y);
621 /* Get a distro from it: */
623 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
625 // TODO: don't handle this in a global way but per-tile...
626 if (!counting_distros)
628 counting_distros = true;
636 if (distro_counter <= 0)
638 counting_distros = false;
639 plevel->change(x, y, TM_IA, tile->next_tile);
642 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
643 player_status.score = player_status.score + SCORE_DISTRO;
644 player_status.distros++;
650 plevel->change(x, y, TM_IA, tile->next_tile);
652 /* Replace it with broken bits: */
653 add_broken_brick(Vector(
654 ((int)(x + 1) / 32) * 32,
655 (int)(y / 32) * 32), tile);
657 /* Get some score: */
658 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
659 player_status.score = player_status.score + SCORE_BRICK;
670 World::tryemptybox(float x, float y, Direction col_side)
672 Tile* tile = gettile(x,y);
676 // according to the collision side, set the upgrade direction
682 int posx = ((int)(x+1) / 32) * 32;
683 int posy = (int)(y/32) * 32 - 32;
686 case 1: // Box with a distro!
687 add_bouncy_distro(Vector(posx, posy));
688 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
689 player_status.score = player_status.score + SCORE_DISTRO;
690 player_status.distros++;
693 case 2: // Add a fire flower upgrade!
694 if (tux->size == SMALL) /* Tux is small, add mints! */
695 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
696 else /* Tux is big, add a fireflower: */
697 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
698 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
701 case 5: // Add an ice flower upgrade!
702 if (tux->size == SMALL) /* Tux is small, add mints! */
703 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
704 else /* Tux is big, add an iceflower: */
705 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
706 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
709 case 3: // Add a golden herring
710 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
713 case 4: // Add a 1up extra
714 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
721 level->change(x, y, TM_IA, tile->next_tile);
724 /* Try to grab a distro: */
726 World::trygrabdistro(float x, float y, int bounciness)
728 Tile* tile = gettile(x, y);
729 if (tile && tile->distro)
731 level->change(x, y, TM_IA, tile->next_tile);
732 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
734 if (bounciness == BOUNCE)
736 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
737 (int)(y / 32) * 32));
740 player_status.score = player_status.score + SCORE_DISTRO;
741 player_status.distros++;
745 /* Try to bump a bad guy from below: */
747 World::trybumpbadguy(float x, float y)
750 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
752 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
753 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
755 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
760 for (unsigned int i = 0; i < upgrades.size(); i++)
762 if (upgrades[i].base.height == 32 &&
763 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
764 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
766 upgrades[i].collision(tux, CO_PLAYER, COLLISION_BUMP);