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();
53 activate_particle_systems();
54 get_level()->load_song();
58 scrolling_timer.init(true);
61 World::World(const std::string& subset, int level_nr)
63 // FIXME: Move this to action and draw and everywhere else where the
64 // world calls child functions
67 level = new Level(subset, level_nr);
72 get_level()->load_gfx();
74 activate_particle_systems();
75 get_level()->load_song();
79 scrolling_timer.init(true);
83 World::apply_bonuses()
85 // Apply bonuses from former levels
86 switch (player_status.bonus)
88 case PlayerStatus::NO_BONUS:
91 case PlayerStatus::FLOWER_BONUS:
92 tux.got_power = tux.FIRE_POWER; // FIXME: add ice power to here
95 case PlayerStatus::GROWUP_BONUS:
96 // FIXME: Move this to Player class
106 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
109 for (ParticleSystems::iterator i = particle_systems.begin();
110 i != particle_systems.end(); ++i)
113 for (std::vector<BouncyDistro*>::iterator i = bouncy_distros.begin();
114 i != bouncy_distros.end(); ++i)
117 for (std::vector<BrokenBrick*>::iterator i = broken_bricks.begin();
118 i != broken_bricks.end(); ++i)
121 for (std::vector<BouncyBrick*>::iterator i = bouncy_bricks.begin();
122 i != bouncy_bricks.end(); ++i)
125 for (std::vector<FloatingScore*>::iterator i = floating_scores.begin();
126 i != floating_scores.end(); ++i)
133 World::set_defaults()
138 player_status.score_multiplier = 1;
140 counting_distros = false;
143 /* set current song/music */
144 currentmusic = LEVEL_MUSIC;
148 World::activate_bad_guys()
150 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
151 i != level->badguy_data.end();
154 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
159 World::activate_objects()
161 for (std::vector< ObjectData<TrampolineData> >::iterator i = level->trampoline_data.begin();
162 i != level->trampoline_data.end();
165 add_object<Trampoline, ObjectData<TrampolineData> >(*i);
170 World::activate_particle_systems()
172 if (level->particle_system == "clouds")
174 particle_systems.push_back(new CloudParticleSystem);
176 else if (level->particle_system == "snow")
178 particle_systems.push_back(new SnowParticleSystem);
180 else if (level->particle_system != "")
182 st_abort("unknown particle system specified in level", "");
191 /* Draw the real background */
192 drawgradient(level->bkgd_top, level->bkgd_bottom);
197 /* Draw particle systems (background) */
198 std::vector<ParticleSystem*>::iterator p;
199 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
201 (*p)->draw(scroll_x, 0, 0);
204 /* Draw background: */
205 for (y = 0; y < VISIBLE_TILES_Y && y < level->height; ++y)
207 for (x = 0; x < VISIBLE_TILES_X; ++x)
209 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
210 level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
214 /* Draw interactive tiles: */
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->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
224 /* (Bouncy bricks): */
225 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
226 bouncy_bricks[i]->draw();
228 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
233 for (unsigned int i = 0; i < bullets.size(); ++i)
236 for (unsigned int i = 0; i < floating_scores.size(); ++i)
237 floating_scores[i]->draw();
239 for (unsigned int i = 0; i < upgrades.size(); ++i)
242 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
243 bouncy_distros[i]->draw();
245 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
246 broken_bricks[i]->draw();
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)]);
258 /* Draw particle systems (foreground) */
259 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
261 (*p)->draw(scroll_x, 0, 1);
266 World::action(double frame_ratio)
268 tux.action(frame_ratio);
269 tux.check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
270 scrolling(frame_ratio);
272 /* Handle bouncy distros: */
273 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
274 bouncy_distros[i]->action(frame_ratio);
276 /* Handle broken bricks: */
277 for (unsigned int i = 0; i < broken_bricks.size(); i++)
278 broken_bricks[i]->action(frame_ratio);
280 // Handle all kinds of game objects
281 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
282 bouncy_bricks[i]->action(frame_ratio);
284 for (unsigned int i = 0; i < floating_scores.size(); i++)
285 floating_scores[i]->action(frame_ratio);
287 for (unsigned int i = 0; i < bullets.size(); ++i)
288 bullets[i].action(frame_ratio);
290 for (unsigned int i = 0; i < upgrades.size(); i++)
291 upgrades[i].action(frame_ratio);
293 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
294 (*i)->action(frame_ratio);
296 /* update particle systems */
297 std::vector<ParticleSystem*>::iterator p;
298 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
300 (*p)->simulate(frame_ratio);
303 /* Handle all possible collisions. */
306 // Cleanup marked badguys
307 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
308 /* ++i handled at end of the loop */) {
309 if ((*i)->is_removable()) {
311 i = bad_guys.erase(i);
318 /* the space that it takes for the screen to start scrolling, regarding */
319 /* screen bounds (in pixels) */
320 // should be higher than screen->w/2 (400)
321 #define X_SPACE (500-16)
322 // should be less than screen->h/2 (300)
325 // the time it takes to move the camera (in ms)
326 #define CHANGE_DIR_SCROLL_SPEED 2000
328 /* This functions takes cares of the scrolling */
329 void World::scrolling(double frame_ratio)
331 /* Y-axis scrolling */
333 float tux_pos_y = tux.base.y + (tux.base.height/2);
335 if(level->height > VISIBLE_TILES_Y-1)
337 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
338 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
339 else if (scroll_y > tux_pos_y - Y_SPACE)
340 scroll_y = tux_pos_y - Y_SPACE;
343 // this code prevent the screen to scroll before the start or after the level's end
344 if(scroll_y > level->height * 32 - screen->h)
345 scroll_y = level->height * 32 - screen->h;
349 /* X-axis scrolling */
352 if(level->hor_autoscroll_speed)
354 scroll_x += level->hor_autoscroll_speed * frame_ratio;
359 /* Horizontal backscrolling */
360 float tux_pos_x = tux.base.x + (tux.base.width/2);
362 if(tux.old_dir != tux.dir && level->back_scrolling)
363 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
367 if (tux.physic.get_velocity_x() > 0)
369 else if (tux.physic.get_velocity_x() < 0)
373 if (tux.dir == RIGHT)
379 if(scrolling_timer.check())
381 float final_scroll_x;
383 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
385 final_scroll_x = tux_pos_x - X_SPACE;
387 scroll_x += (final_scroll_x - scroll_x)
388 / (frame_ratio * (CHANGE_DIR_SCROLL_SPEED / 100))
389 + (tux.physic.get_velocity_x() * frame_ratio + tux.physic.get_acceleration_x() * frame_ratio * frame_ratio);
393 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
394 scroll_x = tux_pos_x - (screen->w - X_SPACE);
395 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
396 scroll_x = tux_pos_x - X_SPACE;
399 // this code prevent the screen to scroll before the start or after the level's end
400 if(scroll_x > level->width * 32 - screen->w)
401 scroll_x = level->width * 32 - screen->w;
407 World::collision_handler()
409 // CO_BULLET & CO_BADGUY check
410 for(unsigned int i = 0; i < bullets.size(); ++i)
412 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
414 if((*j)->dying != DYING_NOT)
417 if(rectcollision(bullets[i].base, (*j)->base))
419 // We have detected a collision and now call the
420 // collision functions of the collided objects.
421 // collide with bad_guy first, since bullet_collision will
423 (*j)->collision(&bullets[i], CO_BULLET);
424 bullets[i].collision(CO_BADGUY);
425 break; // bullet is invalid now, so break
430 /* CO_BADGUY & CO_BADGUY check */
431 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
433 if((*i)->dying != DYING_NOT)
436 BadGuys::iterator j = i;
438 for (; j != bad_guys.end(); ++j)
440 if(j == i || (*j)->dying != DYING_NOT)
443 if(rectcollision((*i)->base, (*j)->base))
445 // We have detected a collision and now call the
446 // collision functions of the collided objects.
447 (*j)->collision(*i, CO_BADGUY);
448 (*i)->collision(*j, CO_BADGUY);
453 if(tux.dying != DYING_NOT) return;
455 // CO_BADGUY & CO_PLAYER check
456 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
458 if((*i)->dying != DYING_NOT)
461 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
463 // We have detected a collision and now call the collision
464 // functions of the collided objects.
465 if (tux.previous_base.y < tux.base.y &&
466 tux.previous_base.y + tux.previous_base.height
467 < (*i)->base.y + (*i)->base.height/2
468 && !tux.invincible_timer.started())
470 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
474 tux.collision(*i, CO_BADGUY);
475 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
480 // CO_UPGRADE & CO_PLAYER check
481 for(unsigned int i = 0; i < upgrades.size(); ++i)
483 if(rectcollision(upgrades[i].base, tux.base))
485 // We have detected a collision and now call the collision
486 // functions of the collided objects.
487 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
493 World::add_score(float x, float y, int s)
495 player_status.score += s;
497 FloatingScore* new_floating_score = new FloatingScore();
498 new_floating_score->init(x-scroll_x, y-scroll_y, s);
499 floating_scores.push_back(new_floating_score);
503 World::add_bouncy_distro(float x, float y)
505 BouncyDistro* new_bouncy_distro = new BouncyDistro();
506 new_bouncy_distro->init(x, y);
507 bouncy_distros.push_back(new_bouncy_distro);
511 World::add_broken_brick(Tile* tile, float x, float y)
513 add_broken_brick_piece(tile, x, y, -1, -4);
514 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
516 add_broken_brick_piece(tile, x + 16, y, 1, -4);
517 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
521 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
523 BrokenBrick* new_broken_brick = new BrokenBrick();
524 new_broken_brick->init(tile, x, y, xm, ym);
525 broken_bricks.push_back(new_broken_brick);
529 World::add_bouncy_brick(float x, float y)
531 BouncyBrick* new_bouncy_brick = new BouncyBrick();
532 new_bouncy_brick->init(x,y);
533 bouncy_bricks.push_back(new_bouncy_brick);
537 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
539 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
540 bad_guys.push_back(badguy);
544 template<class T, class U>
546 World::add_object(U data)
548 T* tobject = new T(data);
549 trampolines.push_back(tobject);
555 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
558 new_upgrade.init(x,y,dir,kind);
559 upgrades.push_back(new_upgrade);
563 World::add_bullet(float x, float y, float xm, Direction dir)
565 if(tux.got_power == tux.FIRE_POWER)
567 if(bullets.size() > MAX_FIRE_BULLETS-1)
570 else if(tux.got_power == tux.ICE_POWER)
572 if(bullets.size() > MAX_ICE_BULLETS-1)
577 if(tux.got_power == tux.FIRE_POWER)
578 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
579 else if(tux.got_power == tux.ICE_POWER)
580 new_bullet.init(x,y,xm,dir, ICE_BULLET);
581 bullets.push_back(new_bullet);
583 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
587 World::play_music(int musictype)
589 currentmusic = musictype;
590 switch(currentmusic) {
592 music_manager->play_music(get_level()->get_level_music_fast());
595 music_manager->play_music(get_level()->get_level_music());
598 music_manager->play_music(herring_song);
601 music_manager->halt_music();
607 World::get_music_type()
614 World::trybreakbrick(float x, float y, bool small)
616 Level* plevel = get_level();
618 Tile* tile = gettile(x, y);
623 /* Get a distro from it: */
624 add_bouncy_distro(((int)(x + 1) / 32) * 32,
627 // TODO: don't handle this in a global way but per-tile...
628 if (!counting_distros)
630 counting_distros = true;
638 if (distro_counter <= 0)
640 counting_distros = false;
641 plevel->change(x, y, TM_IA, tile->next_tile);
644 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
645 player_status.score = player_status.score + SCORE_DISTRO;
646 player_status.distros++;
651 plevel->change(x, y, TM_IA, tile->next_tile);
653 /* Replace it with broken bits: */
654 add_broken_brick(tile,
655 ((int)(x + 1) / 32) * 32,
658 /* Get some score: */
659 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
660 player_status.score = player_status.score + SCORE_BRICK;
667 World::tryemptybox(float x, float y, Direction col_side)
669 Tile* tile = gettile(x,y);
673 // according to the collision side, set the upgrade direction
679 int posx = ((int)(x+1) / 32) * 32;
680 int posy = (int)(y/32) * 32 - 32;
683 case 1: // Box with a distro!
684 add_bouncy_distro(posx, posy);
685 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
686 player_status.score = player_status.score + SCORE_DISTRO;
687 player_status.distros++;
690 case 2: // Add a fire flower upgrade!
691 if (tux.size == SMALL) /* Tux is small, add mints! */
692 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
693 else /* Tux is big, add a fireflower: */
694 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
695 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
698 case 5: // Add an ice 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 an iceflower: */
702 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
703 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
706 case 3: // Add a golden herring
707 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
710 case 4: // Add a 1up extra
711 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
718 level->change(x, y, TM_IA, tile->next_tile);
721 /* Try to grab a distro: */
723 World::trygrabdistro(float x, float y, int bounciness)
725 Tile* tile = gettile(x, y);
726 if (tile && tile->distro)
728 level->change(x, y, TM_IA, tile->next_tile);
729 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
731 if (bounciness == BOUNCE)
733 add_bouncy_distro(((int)(x + 1) / 32) * 32,
737 player_status.score = player_status.score + SCORE_DISTRO;
738 player_status.distros++;
742 /* Try to bump a bad guy from below: */
744 World::trybumpbadguy(float x, float y)
747 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
749 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
750 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
752 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
757 for (unsigned int i = 0; i < upgrades.size(); i++)
759 if (upgrades[i].base.height == 32 &&
760 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
761 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
763 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);