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();
59 World::World(const std::string& subset, int level_nr)
61 // FIXME: Move this to action and draw and everywhere else where the
62 // world calls child functions
65 level = new Level(subset, level_nr);
70 get_level()->load_gfx();
72 activate_particle_systems();
73 get_level()->load_song();
79 World::apply_bonuses()
81 // Apply bonuses from former levels
82 switch (player_status.bonus)
84 case PlayerStatus::NO_BONUS:
87 case PlayerStatus::FLOWER_BONUS:
88 tux.got_coffee = true;
91 case PlayerStatus::GROWUP_BONUS:
92 // FIXME: Move this to Player class
102 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
109 World::set_defaults()
114 player_status.score_multiplier = 1;
116 counting_distros = false;
119 /* set current song/music */
120 currentmusic = LEVEL_MUSIC;
124 World::activate_bad_guys()
126 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
127 i != level->badguy_data.end();
130 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
135 World::activate_particle_systems()
137 if (level->particle_system == "clouds")
139 particle_systems.push_back(new CloudParticleSystem);
141 else if (level->particle_system == "snow")
143 particle_systems.push_back(new SnowParticleSystem);
145 else if (level->particle_system != "")
147 st_abort("unknown particle system specified in level", "");
156 /* Draw the real background */
157 if(get_level()->bkgd_image[0] != '\0')
159 int s = ((int)scroll_x / 2)%640;
160 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
161 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
165 drawgradient(level->bkgd_top, level->bkgd_bottom);
168 /* Draw particle systems (background) */
169 std::vector<ParticleSystem*>::iterator p;
170 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
172 (*p)->draw(scroll_x, 0, 0);
175 /* Draw background: */
176 for (y = 0; y < 15; ++y)
178 for (x = 0; x < 21; ++x)
180 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
181 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
185 /* Draw interactive tiles: */
186 for (y = 0; y < 15; ++y)
188 for (x = 0; x < 21; ++x)
190 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
191 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
195 /* (Bouncy bricks): */
196 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
197 bouncy_bricks[i].draw();
199 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
204 for (unsigned int i = 0; i < bullets.size(); ++i)
207 for (unsigned int i = 0; i < floating_scores.size(); ++i)
208 floating_scores[i].draw();
210 for (unsigned int i = 0; i < upgrades.size(); ++i)
213 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
214 bouncy_distros[i].draw();
216 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
217 broken_bricks[i].draw();
219 /* Draw foreground: */
220 for (y = 0; y < 15; ++y)
222 for (x = 0; x < 21; ++x)
224 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
225 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
229 /* Draw particle systems (foreground) */
230 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
232 (*p)->draw(scroll_x, 0, 1);
237 World::action(double frame_ratio)
239 tux.action(frame_ratio);
241 /* Handle bouncy distros: */
242 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
243 bouncy_distros[i].action(frame_ratio);
245 /* Handle broken bricks: */
246 for (unsigned int i = 0; i < broken_bricks.size(); i++)
247 broken_bricks[i].action(frame_ratio);
249 /* Handle distro counting: */
250 if (counting_distros)
254 if (distro_counter <= 0)
255 counting_distros = -1;
258 // Handle all kinds of game objects
259 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
260 bouncy_bricks[i].action(frame_ratio);
262 for (unsigned int i = 0; i < floating_scores.size(); i++)
263 floating_scores[i].action(frame_ratio);
265 for (unsigned int i = 0; i < bullets.size(); ++i)
266 bullets[i].action(frame_ratio);
268 for (unsigned int i = 0; i < upgrades.size(); i++)
269 upgrades[i].action(frame_ratio);
271 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
272 (*i)->action(frame_ratio);
274 /* update particle systems */
275 std::vector<ParticleSystem*>::iterator p;
276 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
278 (*p)->simulate(frame_ratio);
281 /* Handle all possible collisions. */
284 // Cleanup marked badguys
285 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end();
286 /* ++i handled at end of the loop */) {
287 if ((*i)->is_removable()) {
289 i = bad_guys.erase(i);
297 World::collision_handler()
299 // CO_BULLET & CO_BADGUY check
300 for(unsigned int i = 0; i < bullets.size(); ++i)
302 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
304 if((*j)->dying != DYING_NOT)
307 if(rectcollision(bullets[i].base, (*j)->base))
309 // We have detected a collision and now call the
310 // collision functions of the collided objects.
311 // collide with bad_guy first, since bullet_collision will
313 (*j)->collision(0, CO_BULLET);
314 bullets[i].collision(CO_BADGUY);
315 break; // bullet is invalid now, so break
320 /* CO_BADGUY & CO_BADGUY check */
321 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
323 if((*i)->dying != DYING_NOT)
326 BadGuys::iterator j = i;
328 for (; j != bad_guys.end(); ++j)
330 if(j == i || (*j)->dying != DYING_NOT)
333 if(rectcollision((*i)->base, (*j)->base))
335 // We have detected a collision and now call the
336 // collision functions of the collided objects.
337 (*j)->collision(*i, CO_BADGUY);
338 (*i)->collision(*j, CO_BADGUY);
343 if(tux.dying != DYING_NOT) return;
345 // CO_BADGUY & CO_PLAYER check
346 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
348 if((*i)->dying != DYING_NOT)
351 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
353 // We have detected a collision and now call the collision
354 // functions of the collided objects.
355 if (tux.previous_base.y < tux.base.y &&
356 tux.previous_base.y + tux.previous_base.height
357 < (*i)->base.y + (*i)->base.height/2)
359 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
363 tux.collision(*i, CO_BADGUY);
364 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
369 // CO_UPGRADE & CO_PLAYER check
370 for(unsigned int i = 0; i < upgrades.size(); ++i)
372 if(rectcollision(upgrades[i].base, tux.base))
374 // We have detected a collision and now call the collision
375 // functions of the collided objects.
376 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
382 World::add_score(float x, float y, int s)
384 player_status.score += s;
386 FloatingScore new_floating_score;
387 new_floating_score.init(x,y,s);
388 floating_scores.push_back(new_floating_score);
392 World::add_bouncy_distro(float x, float y)
394 BouncyDistro new_bouncy_distro;
395 new_bouncy_distro.init(x,y);
396 bouncy_distros.push_back(new_bouncy_distro);
400 World::add_broken_brick(Tile* tile, float x, float y)
402 add_broken_brick_piece(tile, x, y, -1, -4);
403 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
405 add_broken_brick_piece(tile, x + 16, y, 1, -4);
406 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
410 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
412 BrokenBrick new_broken_brick;
413 new_broken_brick.init(tile, x, y, xm, ym);
414 broken_bricks.push_back(new_broken_brick);
418 World::add_bouncy_brick(float x, float y)
420 BouncyBrick new_bouncy_brick;
421 new_bouncy_brick.init(x,y);
422 bouncy_bricks.push_back(new_bouncy_brick);
426 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
428 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
429 bad_guys.push_back(badguy);
434 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
437 new_upgrade.init(x,y,dir,kind);
438 upgrades.push_back(new_upgrade);
442 World::add_bullet(float x, float y, float xm, Direction dir)
444 if(bullets.size() > MAX_BULLETS-1)
448 new_bullet.init(x,y,xm,dir);
449 bullets.push_back(new_bullet);
451 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
455 World::play_music(int musictype)
457 currentmusic = musictype;
458 switch(currentmusic) {
460 music_manager->play_music(get_level()->get_level_music_fast());
463 music_manager->play_music(get_level()->get_level_music());
466 music_manager->play_music(herring_song);
469 music_manager->halt_music();
475 World::get_music_type()
482 World::trybreakbrick(float x, float y, bool small)
484 Level* plevel = get_level();
486 Tile* tile = gettile(x, y);
491 /* Get a distro from it: */
492 add_bouncy_distro(((int)(x + 1) / 32) * 32,
495 if (!counting_distros)
497 counting_distros = true;
501 if (distro_counter <= 0)
502 plevel->change(x, y, TM_IA, tile->next_tile);
504 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
505 player_status.score = player_status.score + SCORE_DISTRO;
506 player_status.distros++;
511 plevel->change(x, y, TM_IA, tile->next_tile);
513 /* Replace it with broken bits: */
514 add_broken_brick(tile,
515 ((int)(x + 1) / 32) * 32,
518 /* Get some score: */
519 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
520 player_status.score = player_status.score + SCORE_BRICK;
527 World::tryemptybox(float x, float y, Direction col_side)
529 Tile* tile = gettile(x,y);
533 // according to the collision side, set the upgrade direction
539 int posx = ((int)(x+1) / 32) * 32;
540 int posy = (int)(y/32) * 32 - 32;
543 case 1: // Box with a distro!
544 add_bouncy_distro(posx, posy);
545 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
546 player_status.score = player_status.score + SCORE_DISTRO;
547 player_status.distros++;
550 case 2: // Add an upgrade!
551 if (tux.size == SMALL) /* Tux is small, add mints! */
552 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
553 else /* Tux is big, add an iceflower: */
554 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
555 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
558 case 3: // Add a golden herring
559 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
562 case 4: // Add a 1up extra
563 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
570 level->change(x, y, TM_IA, tile->next_tile);
573 /* Try to grab a distro: */
575 World::trygrabdistro(float x, float y, int bounciness)
577 Tile* tile = gettile(x, y);
578 if (tile && tile->distro)
580 level->change(x, y, TM_IA, tile->next_tile);
581 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
583 if (bounciness == BOUNCE)
585 add_bouncy_distro(((int)(x + 1) / 32) * 32,
589 player_status.score = player_status.score + SCORE_DISTRO;
590 player_status.distros++;
594 /* Try to bump a bad guy from below: */
596 World::trybumpbadguy(float x, float y)
599 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
601 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
602 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
604 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
609 for (unsigned int i = 0; i < upgrades.size(); i++)
611 if (upgrades[i].base.height == 32 &&
612 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
613 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
615 upgrades[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);