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 std::cout << "Bonus: " << player_status.bonus << std::endl;
83 // Apply bonuses from former levels
84 switch (player_status.bonus)
86 case PlayerStatus::NO_BONUS:
89 case PlayerStatus::FLOWER_BONUS:
90 tux.got_coffee = true;
93 case PlayerStatus::GROWUP_BONUS:
94 // FIXME: Move this to Player class
104 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
111 World::set_defaults()
116 player_status.score_multiplier = 1;
118 counting_distros = false;
121 /* set current song/music */
122 currentmusic = LEVEL_MUSIC;
126 World::activate_bad_guys()
128 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
129 i != level->badguy_data.end();
132 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
137 World::activate_particle_systems()
139 if (level->particle_system == "clouds")
141 particle_systems.push_back(new CloudParticleSystem);
143 else if (level->particle_system == "snow")
145 particle_systems.push_back(new SnowParticleSystem);
147 else if (level->particle_system != "")
149 st_abort("unknown particle system specified in level", "");
158 /* Draw the real background */
159 if(get_level()->bkgd_image[0] != '\0')
161 int s = ((int)scroll_x / 2)%640;
162 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
163 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
167 drawgradient(level->bkgd_top, level->bkgd_bottom);
170 /* Draw particle systems (background) */
171 std::vector<ParticleSystem*>::iterator p;
172 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
174 (*p)->draw(scroll_x, 0, 0);
177 /* Draw background: */
178 for (y = 0; y < 15; ++y)
180 for (x = 0; x < 21; ++x)
182 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
183 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
187 /* Draw interactive tiles: */
188 for (y = 0; y < 15; ++y)
190 for (x = 0; x < 21; ++x)
192 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
193 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
197 /* (Bouncy bricks): */
198 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
199 bouncy_bricks[i].draw();
201 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
206 for (unsigned int i = 0; i < bullets.size(); ++i)
209 for (unsigned int i = 0; i < floating_scores.size(); ++i)
210 floating_scores[i].draw();
212 for (unsigned int i = 0; i < upgrades.size(); ++i)
215 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
216 bouncy_distros[i].draw();
218 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
219 broken_bricks[i].draw();
221 /* Draw foreground: */
222 for (y = 0; y < 15; ++y)
224 for (x = 0; x < 21; ++x)
226 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
227 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
231 /* Draw particle systems (foreground) */
232 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
234 (*p)->draw(scroll_x, 0, 1);
239 World::action(double frame_ratio)
241 tux.action(frame_ratio);
243 /* Handle bouncy distros: */
244 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
245 bouncy_distros[i].action(frame_ratio);
247 /* Handle broken bricks: */
248 for (unsigned int i = 0; i < broken_bricks.size(); i++)
249 broken_bricks[i].action(frame_ratio);
251 /* Handle distro counting: */
252 if (counting_distros)
256 if (distro_counter <= 0)
257 counting_distros = -1;
260 // Handle all kinds of game objects
261 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
262 bouncy_bricks[i].action(frame_ratio);
264 for (unsigned int i = 0; i < floating_scores.size(); i++)
265 floating_scores[i].action(frame_ratio);
267 for (unsigned int i = 0; i < bullets.size(); ++i)
268 bullets[i].action(frame_ratio);
270 for (unsigned int i = 0; i < upgrades.size(); i++)
271 upgrades[i].action(frame_ratio);
273 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
274 (*i)->action(frame_ratio);
276 /* update particle systems */
277 std::vector<ParticleSystem*>::iterator p;
278 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
280 (*p)->simulate(frame_ratio);
283 /* Handle all possible collisions. */
286 { // Cleanup marked badguys
287 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
288 if ((*i)->is_removable())
290 bad_guys.remove_if(std::mem_fun(&BadGuy::is_removable));
295 World::collision_handler()
297 // CO_BULLET & CO_BADGUY check
298 for(unsigned int i = 0; i < bullets.size(); ++i)
300 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
302 if((*j)->dying != DYING_NOT)
305 if(rectcollision(bullets[i].base, (*j)->base))
307 // We have detected a collision and now call the
308 // collision functions of the collided objects.
309 // collide with bad_guy first, since bullet_collision will
311 (*j)->collision(0, CO_BULLET);
312 bullets[i].collision(CO_BADGUY);
313 break; // bullet is invalid now, so break
318 /* CO_BADGUY & CO_BADGUY check */
319 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
321 if((*i)->dying != DYING_NOT)
324 BadGuys::iterator j = i;
326 for (; j != bad_guys.end(); ++j)
328 if(j == i || (*j)->dying != DYING_NOT)
331 if(rectcollision((*i)->base, (*j)->base))
333 // We have detected a collision and now call the
334 // collision functions of the collided objects.
335 (*j)->collision(*i, CO_BADGUY);
336 (*i)->collision(*j, CO_BADGUY);
341 if(tux.dying != DYING_NOT) return;
343 // CO_BADGUY & CO_PLAYER check
344 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
346 if((*i)->dying != DYING_NOT)
349 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
351 // We have detected a collision and now call the collision
352 // functions of the collided objects.
353 if (tux.previous_base.y < tux.base.y &&
354 tux.previous_base.y + tux.previous_base.height
355 < (*i)->base.y + (*i)->base.height/2)
357 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
361 tux.collision(*i, CO_BADGUY);
362 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
367 // CO_UPGRADE & CO_PLAYER check
368 for(unsigned int i = 0; i < upgrades.size(); ++i)
370 if(rectcollision(upgrades[i].base, tux.base))
372 // We have detected a collision and now call the collision
373 // functions of the collided objects.
374 upgrades[i].collision(&tux, CO_PLAYER);
380 World::add_score(float x, float y, int s)
382 player_status.score += s;
384 FloatingScore new_floating_score;
385 new_floating_score.init(x,y,s);
386 floating_scores.push_back(new_floating_score);
390 World::add_bouncy_distro(float x, float y)
392 BouncyDistro new_bouncy_distro;
393 new_bouncy_distro.init(x,y);
394 bouncy_distros.push_back(new_bouncy_distro);
398 World::add_broken_brick(Tile* tile, float x, float y)
400 add_broken_brick_piece(tile, x, y, -1, -4);
401 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
403 add_broken_brick_piece(tile, x + 16, y, 1, -4);
404 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
408 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
410 BrokenBrick new_broken_brick;
411 new_broken_brick.init(tile, x, y, xm, ym);
412 broken_bricks.push_back(new_broken_brick);
416 World::add_bouncy_brick(float x, float y)
418 BouncyBrick new_bouncy_brick;
419 new_bouncy_brick.init(x,y);
420 bouncy_bricks.push_back(new_bouncy_brick);
424 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
426 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
427 bad_guys.push_back(badguy);
432 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
435 new_upgrade.init(x,y,dir,kind);
436 upgrades.push_back(new_upgrade);
440 World::add_bullet(float x, float y, float xm, Direction dir)
442 if(bullets.size() > MAX_BULLETS-1)
446 new_bullet.init(x,y,xm,dir);
447 bullets.push_back(new_bullet);
449 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
453 World::play_music(int musictype)
455 currentmusic = musictype;
456 switch(currentmusic) {
458 music_manager->play_music(get_level()->get_level_music_fast());
461 music_manager->play_music(get_level()->get_level_music());
464 music_manager->play_music(herring_song);
467 music_manager->halt_music();
473 World::get_music_type()
480 World::trybreakbrick(float x, float y, bool small)
482 Level* plevel = get_level();
484 Tile* tile = gettile(x, y);
489 /* Get a distro from it: */
490 add_bouncy_distro(((int)(x + 1) / 32) * 32,
493 if (!counting_distros)
495 counting_distros = true;
499 if (distro_counter <= 0)
500 plevel->change(x, y, TM_IA, tile->next_tile);
502 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
503 player_status.score = player_status.score + SCORE_DISTRO;
504 player_status.distros++;
509 plevel->change(x, y, TM_IA, tile->next_tile);
511 /* Replace it with broken bits: */
512 add_broken_brick(tile,
513 ((int)(x + 1) / 32) * 32,
516 /* Get some score: */
517 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
518 player_status.score = player_status.score + SCORE_BRICK;
525 World::tryemptybox(float x, float y, Direction col_side)
527 Tile* tile = gettile(x,y);
531 // according to the collision side, set the upgrade direction
537 int posx = ((int)(x+1) / 32) * 32;
538 int posy = (int)(y/32) * 32 - 32;
541 case 1: // Box with a distro!
542 add_bouncy_distro(posx, posy);
543 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
544 player_status.score = player_status.score + SCORE_DISTRO;
545 player_status.distros++;
548 case 2: // Add an upgrade!
549 if (tux.size == SMALL) /* Tux is small, add mints! */
550 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
551 else /* Tux is big, add an iceflower: */
552 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
553 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
556 case 3: // Add a golden herring
557 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
560 case 4: // Add a 1up extra
561 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
568 level->change(x, y, TM_IA, tile->next_tile);
571 /* Try to grab a distro: */
573 World::trygrabdistro(float x, float y, int bounciness)
575 Tile* tile = gettile(x, y);
576 if (tile && tile->distro)
578 level->change(x, y, TM_IA, tile->next_tile);
579 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
581 if (bounciness == BOUNCE)
583 add_bouncy_distro(((int)(x + 1) / 32) * 32,
587 player_status.score = player_status.score + SCORE_DISTRO;
588 player_status.distros++;
592 /* Try to bump a bad guy from below: */
594 World::trybumpbadguy(float x, float y)
597 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
599 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
600 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
602 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
607 for (unsigned int i = 0; i < upgrades.size(); i++)
609 if (upgrades[i].base.height == 32 &&
610 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
611 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
613 upgrades[i].base.xm = -upgrades[i].base.xm;
614 upgrades[i].base.ym = -8;
615 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);