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_coffee = true;
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 */
181 if(get_level()->bkgd_image[0] != '\0')
183 int s = (int)((float)scroll_x * ((float)level->bkgd_speed/60.)) % 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 < 15; ++y)
202 for (x = 0; x < 21; ++x)
204 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
205 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
209 /* Draw interactive tiles: */
210 for (y = 0; y < 15; ++y)
212 for (x = 0; x < 21; ++x)
214 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
215 level->ia_tiles[(int)y][(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 < 15; ++y)
246 for (x = 0; x < 21; ++x)
248 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
249 level->fg_tiles[(int)y][(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 scrolling(frame_ratio);
266 /* Handle bouncy distros: */
267 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
268 bouncy_distros[i]->action(frame_ratio);
270 /* Handle broken bricks: */
271 for (unsigned int i = 0; i < broken_bricks.size(); i++)
272 broken_bricks[i]->action(frame_ratio);
274 // Handle all kinds of game objects
275 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
276 bouncy_bricks[i]->action(frame_ratio);
278 for (unsigned int i = 0; i < floating_scores.size(); i++)
279 floating_scores[i]->action(frame_ratio);
281 for (unsigned int i = 0; i < bullets.size(); ++i)
282 bullets[i].action(frame_ratio);
284 for (unsigned int i = 0; i < upgrades.size(); i++)
285 upgrades[i].action(frame_ratio);
287 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
288 (*i)->action(frame_ratio);
290 /* update particle systems */
291 std::vector<ParticleSystem*>::iterator p;
292 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
294 (*p)->simulate(frame_ratio);
297 /* Handle all possible collisions. */
300 // Cleanup marked badguys
301 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
302 /* ++i handled at end of the loop */) {
303 if ((*i)->is_removable()) {
305 i = bad_guys.erase(i);
312 // the space that it takes for the screen to start scrolling, regarding
313 // screen bounds (in pixels)
315 // the time it takes to move the camera (in ms)
316 #define CHANGE_DIR_SCROLL_SPEED 2000
318 /* This functions takes cares of the scrolling */
319 void World::scrolling(double frame_ratio)
321 int tux_pos_x = (int)(tux.base.x + (tux.base.width/2));
323 if(tux.old_dir != tux.dir && (level->back_scrolling || debug_mode))
324 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
326 if(scrolling_timer.check())
328 float final_scroll_x;
329 if (tux.dir == RIGHT)
330 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
331 else// if (tux.dir == LEFT)// && )
332 final_scroll_x = tux_pos_x - X_SPACE;
334 scroll_x += ((final_scroll_x - scroll_x) / (CHANGE_DIR_SCROLL_SPEED)) * frame_ratio;
339 if (tux.dir == RIGHT && scroll_x < tux_pos_x - (screen->w - X_SPACE))
340 scroll_x = tux_pos_x - (screen->w - X_SPACE);
341 else if (tux.dir == LEFT && scroll_x > tux_pos_x - X_SPACE && (level->back_scrolling || debug_mode))
342 scroll_x = tux_pos_x - X_SPACE;
345 // this code prevent the screen to scroll before the start or after the level's end
348 if(scroll_x > (level->width-1) * 32)
349 scroll_x = (level->width-1) * 32;
353 World::collision_handler()
355 // CO_BULLET & CO_BADGUY check
356 for(unsigned int i = 0; i < bullets.size(); ++i)
358 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
360 if((*j)->dying != DYING_NOT)
363 if(rectcollision(bullets[i].base, (*j)->base))
365 // We have detected a collision and now call the
366 // collision functions of the collided objects.
367 // collide with bad_guy first, since bullet_collision will
369 (*j)->collision(0, CO_BULLET);
370 bullets[i].collision(CO_BADGUY);
371 break; // bullet is invalid now, so break
376 /* CO_BADGUY & CO_BADGUY check */
377 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
379 if((*i)->dying != DYING_NOT)
382 BadGuys::iterator j = i;
384 for (; j != bad_guys.end(); ++j)
386 if(j == i || (*j)->dying != DYING_NOT)
389 if(rectcollision((*i)->base, (*j)->base))
391 // We have detected a collision and now call the
392 // collision functions of the collided objects.
393 (*j)->collision(*i, CO_BADGUY);
394 (*i)->collision(*j, CO_BADGUY);
399 if(tux.dying != DYING_NOT) return;
401 // CO_BADGUY & CO_PLAYER check
402 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
404 if((*i)->dying != DYING_NOT)
407 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
409 // We have detected a collision and now call the collision
410 // functions of the collided objects.
411 if (tux.previous_base.y < tux.base.y &&
412 tux.previous_base.y + tux.previous_base.height
413 < (*i)->base.y + (*i)->base.height/2
414 && !tux.invincible_timer.started())
416 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
420 tux.collision(*i, CO_BADGUY);
421 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
426 // CO_UPGRADE & CO_PLAYER check
427 for(unsigned int i = 0; i < upgrades.size(); ++i)
429 if(rectcollision(upgrades[i].base, tux.base))
431 // We have detected a collision and now call the collision
432 // functions of the collided objects.
433 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
439 World::add_score(float x, float y, int s)
441 player_status.score += s;
443 FloatingScore* new_floating_score = new FloatingScore();
444 new_floating_score->init(x,y,s);
445 floating_scores.push_back(new_floating_score);
449 World::add_bouncy_distro(float x, float y)
451 BouncyDistro* new_bouncy_distro = new BouncyDistro();
452 new_bouncy_distro->init(x,y);
453 bouncy_distros.push_back(new_bouncy_distro);
457 World::add_broken_brick(Tile* tile, float x, float y)
459 add_broken_brick_piece(tile, x, y, -1, -4);
460 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
462 add_broken_brick_piece(tile, x + 16, y, 1, -4);
463 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
467 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
469 BrokenBrick* new_broken_brick = new BrokenBrick();
470 new_broken_brick->init(tile, x, y, xm, ym);
471 broken_bricks.push_back(new_broken_brick);
475 World::add_bouncy_brick(float x, float y)
477 BouncyBrick* new_bouncy_brick = new BouncyBrick();
478 new_bouncy_brick->init(x,y);
479 bouncy_bricks.push_back(new_bouncy_brick);
483 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
485 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
486 bad_guys.push_back(badguy);
491 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
494 new_upgrade.init(x,y,dir,kind);
495 upgrades.push_back(new_upgrade);
499 World::add_bullet(float x, float y, float xm, Direction dir)
501 if(bullets.size() > MAX_BULLETS-1)
505 new_bullet.init(x,y,xm,dir);
506 bullets.push_back(new_bullet);
508 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
512 World::play_music(int musictype)
514 currentmusic = musictype;
515 switch(currentmusic) {
517 music_manager->play_music(get_level()->get_level_music_fast());
520 music_manager->play_music(get_level()->get_level_music());
523 music_manager->play_music(herring_song);
526 music_manager->halt_music();
532 World::get_music_type()
539 World::trybreakbrick(float x, float y, bool small)
541 Level* plevel = get_level();
543 Tile* tile = gettile(x, y);
548 /* Get a distro from it: */
549 add_bouncy_distro(((int)(x + 1) / 32) * 32,
552 // TODO: don't handle this in a global way but per-tile...
553 if (!counting_distros)
555 counting_distros = true;
563 if (distro_counter <= 0)
565 counting_distros = false;
566 plevel->change(x, y, TM_IA, tile->next_tile);
569 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
570 player_status.score = player_status.score + SCORE_DISTRO;
571 player_status.distros++;
576 plevel->change(x, y, TM_IA, tile->next_tile);
578 /* Replace it with broken bits: */
579 add_broken_brick(tile,
580 ((int)(x + 1) / 32) * 32,
583 /* Get some score: */
584 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
585 player_status.score = player_status.score + SCORE_BRICK;
592 World::tryemptybox(float x, float y, Direction col_side)
594 Tile* tile = gettile(x,y);
598 // according to the collision side, set the upgrade direction
604 int posx = ((int)(x+1) / 32) * 32;
605 int posy = (int)(y/32) * 32 - 32;
608 case 1: // Box with a distro!
609 add_bouncy_distro(posx, posy);
610 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
611 player_status.score = player_status.score + SCORE_DISTRO;
612 player_status.distros++;
615 case 2: // Add an upgrade!
616 if (tux.size == SMALL) /* Tux is small, add mints! */
617 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
618 else /* Tux is big, add an iceflower: */
619 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
620 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
623 case 3: // Add a golden herring
624 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
627 case 4: // Add a 1up extra
628 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
635 level->change(x, y, TM_IA, tile->next_tile);
638 /* Try to grab a distro: */
640 World::trygrabdistro(float x, float y, int bounciness)
642 Tile* tile = gettile(x, y);
643 if (tile && tile->distro)
645 level->change(x, y, TM_IA, tile->next_tile);
646 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
648 if (bounciness == BOUNCE)
650 add_bouncy_distro(((int)(x + 1) / 32) * 32,
654 player_status.score = player_status.score + SCORE_DISTRO;
655 player_status.distros++;
659 /* Try to bump a bad guy from below: */
661 World::trybumpbadguy(float x, float y)
664 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
666 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
667 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
669 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
674 for (unsigned int i = 0; i < upgrades.size(); i++)
676 if (upgrades[i].base.height == 32 &&
677 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
678 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
680 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);