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();
54 activate_particle_systems();
55 get_level()->load_song();
59 scrolling_timer.init(true);
62 World::World(const std::string& subset, int level_nr)
64 // FIXME: Move this to action and draw and everywhere else where the
65 // world calls child functions
68 level = new Level(subset, level_nr);
73 get_level()->load_gfx();
76 activate_particle_systems();
77 get_level()->load_song();
81 scrolling_timer.init(true);
85 World::apply_bonuses()
87 // Apply bonuses from former levels
88 switch (player_status.bonus)
90 case PlayerStatus::NO_BONUS:
93 case PlayerStatus::FLOWER_BONUS:
94 tux.got_power = tux.FIRE_POWER; // FIXME: add ice power to here
97 case PlayerStatus::GROWUP_BONUS:
98 // FIXME: Move this to Player class
100 tux.base.height = 64;
108 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
111 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
114 for (ParticleSystems::iterator i = particle_systems.begin();
115 i != particle_systems.end(); ++i)
118 for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
119 i != bouncy_distros.end(); ++i)
122 for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
123 i != broken_bricks.end(); ++i)
126 for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
127 i != bouncy_bricks.end(); ++i)
130 for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
131 i != floating_scores.end(); ++i)
138 World::set_defaults()
143 player_status.score_multiplier = 1;
145 counting_distros = false;
148 /* set current song/music */
149 currentmusic = LEVEL_MUSIC;
153 World::activate_bad_guys()
155 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
156 i != level->badguy_data.end();
159 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
164 World::activate_objects()
166 for (std::vector< ObjectData<TrampolineData> >::iterator i = level->trampoline_data.begin();
167 i != level->trampoline_data.end();
170 add_object<Trampoline, ObjectData<TrampolineData> >(*i);
175 World::activate_particle_systems()
177 if (level->particle_system == "clouds")
179 particle_systems.push_back(new CloudParticleSystem);
181 else if (level->particle_system == "snow")
183 particle_systems.push_back(new SnowParticleSystem);
185 else if (level->particle_system != "")
187 st_abort("unknown particle system specified in level", "");
196 /* Draw the real background */
197 drawgradient(level->bkgd_top, level->bkgd_bottom);
202 /* Draw particle systems (background) */
203 std::vector<ParticleSystem*>::iterator p;
204 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
206 (*p)->draw(scroll_x, 0, 0);
209 /* Draw background: */
210 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
212 for (x = 0; x < VISIBLE_TILES_X; ++x)
214 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
215 level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
219 /* Draw interactive tiles: */
220 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
222 for (x = 0; x < VISIBLE_TILES_X; ++x)
224 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
225 level->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
229 /* (Bouncy bricks): */
230 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
231 bouncy_bricks[i]->draw();
233 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
236 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
241 for (unsigned int i = 0; i < bullets.size(); ++i)
244 for (unsigned int i = 0; i < floating_scores.size(); ++i)
245 floating_scores[i]->draw();
247 for (unsigned int i = 0; i < upgrades.size(); ++i)
250 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
251 bouncy_distros[i]->draw();
253 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
254 broken_bricks[i]->draw();
256 /* Draw foreground: */
257 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
259 for (x = 0; x < VISIBLE_TILES_X; ++x)
261 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
262 level->fg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
266 /* Draw particle systems (foreground) */
267 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
269 (*p)->draw(scroll_x, 0, 1);
274 World::action(double frame_ratio)
276 tux.action(frame_ratio);
277 tux.check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
278 scrolling(frame_ratio);
280 /* Handle bouncy distros: */
281 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
282 bouncy_distros[i]->action(frame_ratio);
284 /* Handle broken bricks: */
285 for (unsigned int i = 0; i < broken_bricks.size(); i++)
286 broken_bricks[i]->action(frame_ratio);
288 // Handle all kinds of game objects
289 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
290 bouncy_bricks[i]->action(frame_ratio);
292 for (unsigned int i = 0; i < floating_scores.size(); i++)
293 floating_scores[i]->action(frame_ratio);
295 for (unsigned int i = 0; i < bullets.size(); ++i)
296 bullets[i].action(frame_ratio);
298 for (unsigned int i = 0; i < upgrades.size(); i++)
299 upgrades[i].action(frame_ratio);
301 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
302 (*i)->action(frame_ratio);
304 /* update particle systems */
305 std::vector<ParticleSystem*>::iterator p;
306 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
308 (*p)->simulate(frame_ratio);
311 /* Handle all possible collisions. */
314 // Cleanup marked badguys
315 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
316 /* ++i handled at end of the loop */) {
317 if ((*i)->is_removable()) {
319 i = bad_guys.erase(i);
326 /* the space that it takes for the screen to start scrolling, regarding */
327 /* screen bounds (in pixels) */
328 // should be higher than screen->w/2 (400)
329 #define X_SPACE (500-16)
330 // should be less than screen->h/2 (300)
333 // the time it takes to move the camera (in ms)
334 #define CHANGE_DIR_SCROLL_SPEED 2000
336 /* This functions takes cares of the scrolling */
337 void World::scrolling(double frame_ratio)
339 /* Y-axis scrolling */
341 float tux_pos_y = tux.base.y + (tux.base.height/2);
343 if(level->height > VISIBLE_TILES_Y-1)
345 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
346 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
347 else if (scroll_y > tux_pos_y - Y_SPACE)
348 scroll_y = tux_pos_y - Y_SPACE;
351 // this code prevent the screen to scroll before the start or after the level's end
352 if(scroll_y > level->height * 32 - screen->h)
353 scroll_y = level->height * 32 - screen->h;
357 /* X-axis scrolling */
360 if(level->hor_autoscroll_speed)
362 scroll_x += level->hor_autoscroll_speed * frame_ratio;
367 /* Horizontal backscrolling */
368 float tux_pos_x = tux.base.x + (tux.base.width/2);
370 if(tux.old_dir != tux.dir && level->back_scrolling)
371 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
375 if (tux.physic.get_velocity_x() > 0)
377 else if (tux.physic.get_velocity_x() < 0)
381 if (tux.dir == RIGHT)
387 if(scrolling_timer.check())
389 float final_scroll_x;
391 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
393 final_scroll_x = tux_pos_x - X_SPACE;
395 scroll_x += (final_scroll_x - scroll_x)
396 / (frame_ratio * (CHANGE_DIR_SCROLL_SPEED / 100))
397 + (tux.physic.get_velocity_x() * frame_ratio + tux.physic.get_acceleration_x() * frame_ratio * frame_ratio);
401 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
402 scroll_x = tux_pos_x - (screen->w - X_SPACE);
403 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
404 scroll_x = tux_pos_x - X_SPACE;
407 // this code prevent the screen to scroll before the start or after the level's end
408 if(scroll_x > level->width * 32 - screen->w)
409 scroll_x = level->width * 32 - screen->w;
415 World::collision_handler()
417 // CO_BULLET & CO_BADGUY check
418 for(unsigned int i = 0; i < bullets.size(); ++i)
420 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
422 if((*j)->dying != DYING_NOT)
425 if(rectcollision(bullets[i].base, (*j)->base))
427 // We have detected a collision and now call the
428 // collision functions of the collided objects.
429 // collide with bad_guy first, since bullet_collision will
431 (*j)->collision(&bullets[i], CO_BULLET);
432 bullets[i].collision(CO_BADGUY);
433 break; // bullet is invalid now, so break
438 /* CO_BADGUY & CO_BADGUY check */
439 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
441 if((*i)->dying != DYING_NOT)
444 BadGuys::iterator j = i;
446 for (; j != bad_guys.end(); ++j)
448 if(j == i || (*j)->dying != DYING_NOT)
451 if(rectcollision((*i)->base, (*j)->base))
453 // We have detected a collision and now call the
454 // collision functions of the collided objects.
455 (*j)->collision(*i, CO_BADGUY);
456 (*i)->collision(*j, CO_BADGUY);
461 if(tux.dying != DYING_NOT) return;
463 // CO_BADGUY & CO_PLAYER check
464 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
466 if((*i)->dying != DYING_NOT)
469 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
471 // We have detected a collision and now call the collision
472 // functions of the collided objects.
473 if (tux.previous_base.y < tux.base.y &&
474 tux.previous_base.y + tux.previous_base.height
475 < (*i)->base.y + (*i)->base.height/2
476 && !tux.invincible_timer.started())
478 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
482 tux.collision(*i, CO_BADGUY);
483 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
488 // CO_UPGRADE & CO_PLAYER check
489 for(unsigned int i = 0; i < upgrades.size(); ++i)
491 if(rectcollision(upgrades[i].base, tux.base))
493 // We have detected a collision and now call the collision
494 // functions of the collided objects.
495 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
501 World::add_score(float x, float y, int s)
503 player_status.score += s;
505 FloatingScore* new_floating_score = new FloatingScore();
506 new_floating_score->init(x-scroll_x, y-scroll_y, s);
507 floating_scores.push_back(new_floating_score);
511 World::add_bouncy_distro(float x, float y)
513 BouncyDistro* new_bouncy_distro = new BouncyDistro();
514 new_bouncy_distro->init(x, y);
515 bouncy_distros.push_back(new_bouncy_distro);
519 World::add_broken_brick(Tile* tile, float x, float y)
521 add_broken_brick_piece(tile, x, y, -1, -4);
522 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
524 add_broken_brick_piece(tile, x + 16, y, 1, -4);
525 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
529 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
531 BrokenBrick* new_broken_brick = new BrokenBrick();
532 new_broken_brick->init(tile, x, y, xm, ym);
533 broken_bricks.push_back(new_broken_brick);
537 World::add_bouncy_brick(float x, float y)
539 BouncyBrick* new_bouncy_brick = new BouncyBrick();
540 new_bouncy_brick->init(x,y);
541 bouncy_bricks.push_back(new_bouncy_brick);
545 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
547 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
548 bad_guys.push_back(badguy);
552 template<class T, class U>
554 World::add_object(U data)
556 T* tobject = new T(data);
557 trampolines.push_back(tobject);
563 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
566 new_upgrade.init(x,y,dir,kind);
567 upgrades.push_back(new_upgrade);
571 World::add_bullet(float x, float y, float xm, Direction dir)
573 if(tux.got_power == tux.FIRE_POWER)
575 if(bullets.size() > MAX_FIRE_BULLETS-1)
578 else if(tux.got_power == tux.ICE_POWER)
580 if(bullets.size() > MAX_ICE_BULLETS-1)
585 if(tux.got_power == tux.FIRE_POWER)
586 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
587 else if(tux.got_power == tux.ICE_POWER)
588 new_bullet.init(x,y,xm,dir, ICE_BULLET);
589 bullets.push_back(new_bullet);
591 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
595 World::play_music(int musictype)
597 currentmusic = musictype;
598 switch(currentmusic) {
600 music_manager->play_music(get_level()->get_level_music_fast());
603 music_manager->play_music(get_level()->get_level_music());
606 music_manager->play_music(herring_song);
609 music_manager->halt_music();
615 World::get_music_type()
622 World::trybreakbrick(float x, float y, bool small)
624 Level* plevel = get_level();
626 Tile* tile = gettile(x, y);
631 /* Get a distro from it: */
632 add_bouncy_distro(((int)(x + 1) / 32) * 32,
635 // TODO: don't handle this in a global way but per-tile...
636 if (!counting_distros)
638 counting_distros = true;
646 if (distro_counter <= 0)
648 counting_distros = false;
649 plevel->change(x, y, TM_IA, tile->next_tile);
652 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
653 player_status.score = player_status.score + SCORE_DISTRO;
654 player_status.distros++;
659 plevel->change(x, y, TM_IA, tile->next_tile);
661 /* Replace it with broken bits: */
662 add_broken_brick(tile,
663 ((int)(x + 1) / 32) * 32,
666 /* Get some score: */
667 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
668 player_status.score = player_status.score + SCORE_BRICK;
675 World::tryemptybox(float x, float y, Direction col_side)
677 Tile* tile = gettile(x,y);
681 // according to the collision side, set the upgrade direction
687 int posx = ((int)(x+1) / 32) * 32;
688 int posy = (int)(y/32) * 32 - 32;
691 case 1: // Box with a distro!
692 add_bouncy_distro(posx, posy);
693 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
694 player_status.score = player_status.score + SCORE_DISTRO;
695 player_status.distros++;
698 case 2: // Add a fire flower upgrade!
699 if (tux.size == SMALL) /* Tux is small, add mints! */
700 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
701 else /* Tux is big, add a fireflower: */
702 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
703 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
706 case 5: // Add an ice flower upgrade!
707 if (tux.size == SMALL) /* Tux is small, add mints! */
708 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
709 else /* Tux is big, add an iceflower: */
710 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
711 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
714 case 3: // Add a golden herring
715 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
718 case 4: // Add a 1up extra
719 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
726 level->change(x, y, TM_IA, tile->next_tile);
729 /* Try to grab a distro: */
731 World::trygrabdistro(float x, float y, int bounciness)
733 Tile* tile = gettile(x, y);
734 if (tile && tile->distro)
736 level->change(x, y, TM_IA, tile->next_tile);
737 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
739 if (bounciness == BOUNCE)
741 add_bouncy_distro(((int)(x + 1) / 32) * 32,
745 player_status.score = player_status.score + SCORE_DISTRO;
746 player_status.distros++;
750 /* Try to bump a bad guy from below: */
752 World::trybumpbadguy(float x, float y)
755 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
757 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
758 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
760 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
765 for (unsigned int i = 0; i < upgrades.size(); i++)
767 if (upgrades[i].base.height == 32 &&
768 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
769 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
771 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);