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
33 #include "resources.h"
35 Surface* img_distro[4];
37 World* World::current_ = 0;
39 World::World(const std::string& filename)
41 // FIXME: Move this to action and draw and everywhere else where the
42 // world calls child functions
45 level = new Level(filename);
50 get_level()->load_gfx();
52 activate_particle_systems();
53 get_level()->load_song();
56 World::World(const std::string& subset, int level_nr)
58 // FIXME: Move this to action and draw and everywhere else where the
59 // world calls child functions
62 level = new Level(subset, level_nr);
67 get_level()->load_gfx();
69 activate_particle_systems();
70 get_level()->load_song();
75 for (unsigned int i = 0; i < bad_guys.size(); ++i)
80 halt_music(); // just to be sure (because levelmusic is freed now)
90 player_status.score_multiplier = 1;
92 counting_distros = false;
95 /* set current song/music */
96 currentmusic = LEVEL_MUSIC;
100 World::activate_bad_guys()
102 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
103 i != level->badguy_data.end();
106 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
111 World::activate_particle_systems()
113 if (level->particle_system == "clouds")
115 particle_systems.push_back(new CloudParticleSystem);
117 else if (level->particle_system == "snow")
119 particle_systems.push_back(new SnowParticleSystem);
121 else if (level->particle_system != "")
123 st_abort("unknown particle system specified in level", "");
132 /* Draw the real background */
133 if(get_level()->bkgd_image[0] != '\0')
135 int s = ((int)scroll_x / 2)%640;
136 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
137 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
141 drawgradient(level->bkgd_top, level->bkgd_bottom);
144 /* Draw particle systems (background) */
145 std::vector<ParticleSystem*>::iterator p;
146 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
148 (*p)->draw(scroll_x, 0, 0);
151 /* Draw background: */
152 for (y = 0; y < 15; ++y)
154 for (x = 0; x < 21; ++x)
156 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
157 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
161 /* Draw interactive tiles: */
162 for (y = 0; y < 15; ++y)
164 for (x = 0; x < 21; ++x)
166 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
167 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
171 /* (Bouncy bricks): */
172 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
173 bouncy_bricks[i].draw();
175 for (unsigned int i = 0; i < bad_guys.size(); ++i)
180 for (unsigned int i = 0; i < bullets.size(); ++i)
183 for (unsigned int i = 0; i < floating_scores.size(); ++i)
184 floating_scores[i].draw();
186 for (unsigned int i = 0; i < upgrades.size(); ++i)
189 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
190 bouncy_distros[i].draw();
192 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
193 broken_bricks[i].draw();
195 /* Draw foreground: */
196 for (y = 0; y < 15; ++y)
198 for (x = 0; x < 21; ++x)
200 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
201 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
205 /* Draw particle systems (foreground) */
206 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
208 (*p)->draw(scroll_x, 0, 1);
213 World::action(double frame_ratio)
215 tux.action(frame_ratio);
217 /* Handle bouncy distros: */
218 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
219 bouncy_distros[i].action(frame_ratio);
221 /* Handle broken bricks: */
222 for (unsigned int i = 0; i < broken_bricks.size(); i++)
223 broken_bricks[i].action(frame_ratio);
225 /* Handle distro counting: */
226 if (counting_distros)
230 if (distro_counter <= 0)
231 counting_distros = -1;
234 // Handle all kinds of game objects
235 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
236 bouncy_bricks[i].action(frame_ratio);
238 for (unsigned int i = 0; i < floating_scores.size(); i++)
239 floating_scores[i].action(frame_ratio);
241 for (unsigned int i = 0; i < bullets.size(); ++i)
242 bullets[i].action(frame_ratio);
244 for (unsigned int i = 0; i < upgrades.size(); i++)
245 upgrades[i].action(frame_ratio);
247 for (unsigned int i = 0; i < bad_guys.size(); i++)
248 bad_guys[i]->action(frame_ratio);
250 /* update particle systems */
251 std::vector<ParticleSystem*>::iterator p;
252 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
254 (*p)->simulate(frame_ratio);
257 /* Handle all possible collisions. */
263 World::collision_handler()
265 // CO_BULLET & CO_BADGUY check
266 for(unsigned int i = 0; i < bullets.size(); ++i)
268 for(unsigned int j = 0; j < bad_guys.size(); ++j)
270 if(bad_guys[j]->dying != DYING_NOT)
272 if(rectcollision(&bullets[i].base, &bad_guys[j]->base))
274 // We have detected a collision and now call the
275 // collision functions of the collided objects.
276 // collide with bad_guy first, since bullet_collision will
278 bad_guys[j]->collision(0, CO_BULLET);
279 bullets[i].collision(CO_BADGUY);
280 break; // bullet is invalid now, so break
285 /* CO_BADGUY & CO_BADGUY check */
286 for(unsigned int i = 0; i < bad_guys.size(); ++i)
288 if(bad_guys[i]->dying != DYING_NOT)
291 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
293 if(j == i || bad_guys[j]->dying != DYING_NOT)
296 if(rectcollision(&bad_guys[i]->base, &bad_guys[j]->base))
298 // We have detected a collision and now call the
299 // collision functions of the collided objects.
300 bad_guys[j]->collision(&bad_guys[i], CO_BADGUY);
301 bad_guys[i]->collision(&bad_guys[j], CO_BADGUY);
306 if(tux.dying != DYING_NOT) return;
308 // CO_BADGUY & CO_PLAYER check
309 for(unsigned int i = 0; i < bad_guys.size(); ++i)
311 if(bad_guys[i]->dying != DYING_NOT)
314 if(rectcollision_offset(&bad_guys[i]->base,&tux.base,0,0))
316 // We have detected a collision and now call the collision
317 // functions of the collided objects.
318 if (tux.previous_base.y < tux.base.y &&
319 tux.previous_base.y + tux.previous_base.height
320 < bad_guys[i]->base.y + bad_guys[i]->base.height/2)
322 bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
326 tux.collision(&bad_guys[i], CO_BADGUY);
327 bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
332 // CO_UPGRADE & CO_PLAYER check
333 for(unsigned int i = 0; i < upgrades.size(); ++i)
335 if(rectcollision(&upgrades[i].base, &tux.base))
337 // We have detected a collision and now call the collision
338 // functions of the collided objects.
339 upgrades[i].collision(&tux, CO_PLAYER);
345 World::add_score(float x, float y, int s)
347 player_status.score += s;
349 FloatingScore new_floating_score;
350 new_floating_score.init(x,y,s);
351 floating_scores.push_back(new_floating_score);
355 World::add_bouncy_distro(float x, float y)
357 BouncyDistro new_bouncy_distro;
358 new_bouncy_distro.init(x,y);
359 bouncy_distros.push_back(new_bouncy_distro);
363 World::add_broken_brick(Tile* tile, float x, float y)
365 add_broken_brick_piece(tile, x, y, -1, -4);
366 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
368 add_broken_brick_piece(tile, x + 16, y, 1, -4);
369 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
373 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
375 BrokenBrick new_broken_brick;
376 new_broken_brick.init(tile, x, y, xm, ym);
377 broken_bricks.push_back(new_broken_brick);
381 World::add_bouncy_brick(float x, float y)
383 BouncyBrick new_bouncy_brick;
384 new_bouncy_brick.init(x,y);
385 bouncy_bricks.push_back(new_bouncy_brick);
389 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
391 bad_guys.push_back(new BadGuy());
392 BadGuy* new_bad_guy = bad_guys.back();
394 new_bad_guy->init(x,y,kind, stay_on_platform);
399 World::remove_bad_guy(BadGuy* badguy)
401 for(std::vector<BadGuy*>::iterator i = bad_guys.begin();
402 i != bad_guys.end(); ++i)
406 World::current()->bad_guys.erase(i);
414 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
417 new_upgrade.init(x,y,dir,kind);
418 upgrades.push_back(new_upgrade);
422 World::add_bullet(float x, float y, float xm, Direction dir)
424 if(bullets.size() > MAX_BULLETS-1)
428 new_bullet.init(x,y,xm,dir);
429 bullets.push_back(new_bullet);
431 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
435 World::play_music(int musictype)
437 currentmusic = musictype;
438 switch(currentmusic) {
440 ::play_music(get_level()->get_level_music_fast());
443 ::play_music(get_level()->get_level_music());
446 ::play_music(herring_song);
455 World::get_music_type()
462 World::trybreakbrick(float x, float y, bool small)
464 Level* plevel = get_level();
466 Tile* tile = gettile(x, y);
471 /* Get a distro from it: */
472 add_bouncy_distro(((int)(x + 1) / 32) * 32,
475 if (!counting_distros)
477 counting_distros = true;
481 if (distro_counter <= 0)
482 plevel->change(x, y, TM_IA, tile->next_tile);
484 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
485 player_status.score = player_status.score + SCORE_DISTRO;
486 player_status.distros++;
491 plevel->change(x, y, TM_IA, tile->next_tile);
493 /* Replace it with broken bits: */
494 add_broken_brick(tile,
495 ((int)(x + 1) / 32) * 32,
498 /* Get some score: */
499 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
500 player_status.score = player_status.score + SCORE_BRICK;
507 World::tryemptybox(float x, float y, Direction col_side)
509 Tile* tile = gettile(x,y);
513 // according to the collision side, set the upgrade direction
519 int posx = ((int)(x+1) / 32) * 32;
520 int posy = (int)(y/32) * 32 - 32;
523 case 1: // Box with a distro!
524 add_bouncy_distro(posx, posy);
525 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
526 player_status.score = player_status.score + SCORE_DISTRO;
527 player_status.distros++;
530 case 2: // Add an upgrade!
531 if (tux.size == SMALL) /* Tux is small, add mints! */
532 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
533 else /* Tux is big, add an iceflower: */
534 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
535 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
538 case 3: // Add a golden herring
539 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
542 case 4: // Add a 1up extra
543 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
550 level->change(x, y, TM_IA, tile->next_tile);
553 /* Try to grab a distro: */
555 World::trygrabdistro(float x, float y, int bounciness)
557 Tile* tile = gettile(x, y);
558 if (tile && tile->distro)
560 level->change(x, y, TM_IA, tile->next_tile);
561 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
563 if (bounciness == BOUNCE)
565 add_bouncy_distro(((int)(x + 1) / 32) * 32,
569 player_status.score = player_status.score + SCORE_DISTRO;
570 player_status.distros++;
574 /* Try to bump a bad guy from below: */
576 World::trybumpbadguy(float x, float y)
579 for (unsigned int i = 0; i < bad_guys.size(); i++)
581 if (bad_guys[i]->base.x >= x - 32 && bad_guys[i]->base.x <= x + 32 &&
582 bad_guys[i]->base.y >= y - 16 && bad_guys[i]->base.y <= y + 16)
584 bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_BUMP);
589 for (unsigned int i = 0; i < upgrades.size(); i++)
591 if (upgrades[i].base.height == 32 &&
592 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
593 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
595 upgrades[i].base.xm = -upgrades[i].base.xm;
596 upgrades[i].base.ym = -8;
597 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);