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_particle_systems()
161 if (level->particle_system == "clouds")
163 particle_systems.push_back(new CloudParticleSystem);
165 else if (level->particle_system == "snow")
167 particle_systems.push_back(new SnowParticleSystem);
169 else if (level->particle_system != "")
171 st_abort("unknown particle system specified in level", "");
180 /* Draw the real background */
183 int s = (int)((float)scroll_x * ((float)level->bkgd_speed/100.0f)) % screen->w;
184 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
185 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
189 drawgradient(level->bkgd_top, level->bkgd_bottom);
192 /* Draw particle systems (background) */
193 std::vector<ParticleSystem*>::iterator p;
194 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
196 (*p)->draw(scroll_x, 0, 0);
199 /* Draw background: */
200 for (y = 0; y < 16 && y < level->height; ++y)
202 for (x = 0; x < 21; ++x)
204 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
205 level->bg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
209 /* Draw interactive tiles: */
210 for (y = 0; y < 16 && y < level->height; ++y)
212 for (x = 0; x < 21; ++x)
214 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
215 level->ia_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
219 /* (Bouncy bricks): */
220 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
221 bouncy_bricks[i]->draw();
223 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
228 for (unsigned int i = 0; i < bullets.size(); ++i)
231 for (unsigned int i = 0; i < floating_scores.size(); ++i)
232 floating_scores[i]->draw();
234 for (unsigned int i = 0; i < upgrades.size(); ++i)
237 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
238 bouncy_distros[i]->draw();
240 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
241 broken_bricks[i]->draw();
243 /* Draw foreground: */
244 for (y = 0; y < 16 && y < level->height; ++y)
246 for (x = 0; x < 21; ++x)
248 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32 - fmodf(scroll_y, 32),
249 level->fg_tiles[(int)y + (int)(scroll_y / 32)][(int)x + (int)(scroll_x / 32)]);
253 /* Draw particle systems (foreground) */
254 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
256 (*p)->draw(scroll_x, 0, 1);
261 World::action(double frame_ratio)
263 tux.action(frame_ratio);
264 tux.check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
265 scrolling(frame_ratio);
267 /* Handle bouncy distros: */
268 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
269 bouncy_distros[i]->action(frame_ratio);
271 /* Handle broken bricks: */
272 for (unsigned int i = 0; i < broken_bricks.size(); i++)
273 broken_bricks[i]->action(frame_ratio);
275 // Handle all kinds of game objects
276 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
277 bouncy_bricks[i]->action(frame_ratio);
279 for (unsigned int i = 0; i < floating_scores.size(); i++)
280 floating_scores[i]->action(frame_ratio);
282 for (unsigned int i = 0; i < bullets.size(); ++i)
283 bullets[i].action(frame_ratio);
285 for (unsigned int i = 0; i < upgrades.size(); i++)
286 upgrades[i].action(frame_ratio);
288 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
289 (*i)->action(frame_ratio);
291 /* update particle systems */
292 std::vector<ParticleSystem*>::iterator p;
293 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
295 (*p)->simulate(frame_ratio);
298 /* Handle all possible collisions. */
301 // Cleanup marked badguys
302 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
303 /* ++i handled at end of the loop */) {
304 if ((*i)->is_removable()) {
306 i = bad_guys.erase(i);
313 /* the space that it takes for the screen to start scrolling, regarding */
314 /* screen bounds (in pixels) */
315 // should be higher than screen->w/2 (320)
316 #define X_SPACE (400-16)
317 // should be less than screen->h/2 (240)
320 // the time it takes to move the camera (in ms)
321 #define CHANGE_DIR_SCROLL_SPEED 2000
323 /* This functions takes cares of the scrolling */
324 void World::scrolling(double frame_ratio)
326 /* Y-axis scrolling */
328 float tux_pos_y = tux.base.y + (tux.base.height/2);
330 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
331 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
332 else if (scroll_y > tux_pos_y - Y_SPACE)
333 scroll_y = tux_pos_y - Y_SPACE;
335 // 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;
341 /* X-axis scrolling */
344 if(level->hor_autoscroll_speed)
346 scroll_x += level->hor_autoscroll_speed * frame_ratio;
351 /* Horizontal backscrolling */
352 float tux_pos_x = tux.base.x + (tux.base.width/2);
354 if(tux.old_dir != tux.dir && level->back_scrolling)
355 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
357 if(scrolling_timer.check())
359 float final_scroll_x;
360 if (tux.physic.get_velocity_x() > 0)
361 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
362 else if (tux.physic.get_velocity_x() < 0)
363 final_scroll_x = tux_pos_x - X_SPACE;
366 if (tux.dir == RIGHT)
367 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
368 else if (tux.dir == LEFT && level->back_scrolling)
369 final_scroll_x = tux_pos_x - X_SPACE;
372 scroll_x += (final_scroll_x - scroll_x)
373 / (frame_ratio * (CHANGE_DIR_SCROLL_SPEED / 100))
374 + (tux.physic.get_velocity_x() * frame_ratio + tux.physic.get_acceleration_x() * frame_ratio * frame_ratio);
375 // std::cerr << tux_pos_x << " " << final_scroll_x << " " << scroll_x << std::endl;
380 if (tux.physic.get_velocity_x() > 0 && scroll_x < tux_pos_x - (screen->w - X_SPACE))
381 scroll_x = tux_pos_x - (screen->w - X_SPACE);
382 else if (tux.physic.get_velocity_x() < 0 && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
383 scroll_x = tux_pos_x - X_SPACE;
386 if (tux.dir == RIGHT && scroll_x < tux_pos_x - (screen->w - X_SPACE))
387 scroll_x = tux_pos_x - (screen->w - X_SPACE);
388 else if (tux.dir == LEFT && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
389 scroll_x = tux_pos_x - X_SPACE;
393 // this code prevent the screen to scroll before the start or after the level's end
396 if(scroll_x > level->width * 32 - screen->w)
397 scroll_x = level->width * 32 - screen->w;
401 World::collision_handler()
403 // CO_BULLET & CO_BADGUY check
404 for(unsigned int i = 0; i < bullets.size(); ++i)
406 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
408 if((*j)->dying != DYING_NOT)
411 if(rectcollision(bullets[i].base, (*j)->base))
413 // We have detected a collision and now call the
414 // collision functions of the collided objects.
415 // collide with bad_guy first, since bullet_collision will
417 (*j)->collision(&bullets[i], CO_BULLET);
418 bullets[i].collision(CO_BADGUY);
419 break; // bullet is invalid now, so break
424 /* CO_BADGUY & CO_BADGUY check */
425 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
427 if((*i)->dying != DYING_NOT)
430 BadGuys::iterator j = i;
432 for (; j != bad_guys.end(); ++j)
434 if(j == i || (*j)->dying != DYING_NOT)
437 if(rectcollision((*i)->base, (*j)->base))
439 // We have detected a collision and now call the
440 // collision functions of the collided objects.
441 (*j)->collision(*i, CO_BADGUY);
442 (*i)->collision(*j, CO_BADGUY);
447 if(tux.dying != DYING_NOT) return;
449 // CO_BADGUY & CO_PLAYER check
450 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
452 if((*i)->dying != DYING_NOT)
455 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
457 // We have detected a collision and now call the collision
458 // functions of the collided objects.
459 if (tux.previous_base.y < tux.base.y &&
460 tux.previous_base.y + tux.previous_base.height
461 < (*i)->base.y + (*i)->base.height/2
462 && !tux.invincible_timer.started())
464 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
468 tux.collision(*i, CO_BADGUY);
469 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
474 // CO_UPGRADE & CO_PLAYER check
475 for(unsigned int i = 0; i < upgrades.size(); ++i)
477 if(rectcollision(upgrades[i].base, tux.base))
479 // We have detected a collision and now call the collision
480 // functions of the collided objects.
481 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
487 World::add_score(float x, float y, int s)
489 player_status.score += s;
491 FloatingScore* new_floating_score = new FloatingScore();
492 new_floating_score->init(x-scroll_x, y-scroll_y, s);
493 floating_scores.push_back(new_floating_score);
497 World::add_bouncy_distro(float x, float y)
499 BouncyDistro* new_bouncy_distro = new BouncyDistro();
500 new_bouncy_distro->init(x, y);
501 bouncy_distros.push_back(new_bouncy_distro);
505 World::add_broken_brick(Tile* tile, float x, float y)
507 add_broken_brick_piece(tile, x, y, -1, -4);
508 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
510 add_broken_brick_piece(tile, x + 16, y, 1, -4);
511 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
515 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
517 BrokenBrick* new_broken_brick = new BrokenBrick();
518 new_broken_brick->init(tile, x, y, xm, ym);
519 broken_bricks.push_back(new_broken_brick);
523 World::add_bouncy_brick(float x, float y)
525 BouncyBrick* new_bouncy_brick = new BouncyBrick();
526 new_bouncy_brick->init(x,y);
527 bouncy_bricks.push_back(new_bouncy_brick);
531 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
533 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
534 bad_guys.push_back(badguy);
539 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
542 new_upgrade.init(x,y,dir,kind);
543 upgrades.push_back(new_upgrade);
547 World::add_bullet(float x, float y, float xm, Direction dir)
549 if(tux.got_power == tux.FIRE_POWER)
551 if(bullets.size() > MAX_FIRE_BULLETS-1)
554 else if(tux.got_power == tux.ICE_POWER)
556 if(bullets.size() > MAX_ICE_BULLETS-1)
561 if(tux.got_power == tux.FIRE_POWER)
562 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
563 else if(tux.got_power == tux.ICE_POWER)
564 new_bullet.init(x,y,xm,dir, ICE_BULLET);
565 bullets.push_back(new_bullet);
567 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
571 World::play_music(int musictype)
573 currentmusic = musictype;
574 switch(currentmusic) {
576 music_manager->play_music(get_level()->get_level_music_fast());
579 music_manager->play_music(get_level()->get_level_music());
582 music_manager->play_music(herring_song);
585 music_manager->halt_music();
591 World::get_music_type()
598 World::trybreakbrick(float x, float y, bool small)
600 Level* plevel = get_level();
602 Tile* tile = gettile(x, y);
607 /* Get a distro from it: */
608 add_bouncy_distro(((int)(x + 1) / 32) * 32,
611 // TODO: don't handle this in a global way but per-tile...
612 if (!counting_distros)
614 counting_distros = true;
622 if (distro_counter <= 0)
624 counting_distros = false;
625 plevel->change(x, y, TM_IA, tile->next_tile);
628 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
629 player_status.score = player_status.score + SCORE_DISTRO;
630 player_status.distros++;
635 plevel->change(x, y, TM_IA, tile->next_tile);
637 /* Replace it with broken bits: */
638 add_broken_brick(tile,
639 ((int)(x + 1) / 32) * 32,
642 /* Get some score: */
643 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
644 player_status.score = player_status.score + SCORE_BRICK;
651 World::tryemptybox(float x, float y, Direction col_side)
653 Tile* tile = gettile(x,y);
657 // according to the collision side, set the upgrade direction
663 int posx = ((int)(x+1) / 32) * 32;
664 int posy = (int)(y/32) * 32 - 32;
667 case 1: // Box with a distro!
668 add_bouncy_distro(posx, posy);
669 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
670 player_status.score = player_status.score + SCORE_DISTRO;
671 player_status.distros++;
674 case 2: // Add a fire flower upgrade!
675 if (tux.size == SMALL) /* Tux is small, add mints! */
676 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
677 else /* Tux is big, add a fireflower: */
678 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
679 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
682 case 5: // Add an ice flower upgrade!
683 if (tux.size == SMALL) /* Tux is small, add mints! */
684 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
685 else /* Tux is big, add an iceflower: */
686 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
687 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
690 case 3: // Add a golden herring
691 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
694 case 4: // Add a 1up extra
695 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
702 level->change(x, y, TM_IA, tile->next_tile);
705 /* Try to grab a distro: */
707 World::trygrabdistro(float x, float y, int bounciness)
709 Tile* tile = gettile(x, y);
710 if (tile && tile->distro)
712 level->change(x, y, TM_IA, tile->next_tile);
713 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
715 if (bounciness == BOUNCE)
717 add_bouncy_distro(((int)(x + 1) / 32) * 32,
721 player_status.score = player_status.score + SCORE_DISTRO;
722 player_status.distros++;
726 /* Try to bump a bad guy from below: */
728 World::trybumpbadguy(float x, float y)
731 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
733 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
734 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
736 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
741 for (unsigned int i = 0; i < upgrades.size(); i++)
743 if (upgrades[i].base.height == 32 &&
744 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
745 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
747 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);