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();
57 World::World(const std::string& subset, int level_nr)
59 // FIXME: Move this to action and draw and everywhere else where the
60 // world calls child functions
63 level = new Level(subset, level_nr);
68 get_level()->load_gfx();
70 activate_particle_systems();
71 get_level()->load_song();
76 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
79 halt_music(); // just to be sure (because levelmusic is freed now)
89 player_status.score_multiplier = 1;
91 counting_distros = false;
94 /* set current song/music */
95 currentmusic = LEVEL_MUSIC;
99 World::activate_bad_guys()
101 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
102 i != level->badguy_data.end();
105 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
110 World::activate_particle_systems()
112 if (level->particle_system == "clouds")
114 particle_systems.push_back(new CloudParticleSystem);
116 else if (level->particle_system == "snow")
118 particle_systems.push_back(new SnowParticleSystem);
120 else if (level->particle_system != "")
122 st_abort("unknown particle system specified in level", "");
131 /* Draw the real background */
132 if(get_level()->bkgd_image[0] != '\0')
134 int s = ((int)scroll_x / 2)%640;
135 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
136 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
140 drawgradient(level->bkgd_top, level->bkgd_bottom);
143 /* Draw particle systems (background) */
144 std::vector<ParticleSystem*>::iterator p;
145 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
147 (*p)->draw(scroll_x, 0, 0);
150 /* Draw background: */
151 for (y = 0; y < 15; ++y)
153 for (x = 0; x < 21; ++x)
155 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
156 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
160 /* Draw interactive tiles: */
161 for (y = 0; y < 15; ++y)
163 for (x = 0; x < 21; ++x)
165 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
166 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
170 /* (Bouncy bricks): */
171 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
172 bouncy_bricks[i].draw();
174 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
179 for (unsigned int i = 0; i < bullets.size(); ++i)
182 for (unsigned int i = 0; i < floating_scores.size(); ++i)
183 floating_scores[i].draw();
185 for (unsigned int i = 0; i < upgrades.size(); ++i)
188 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
189 bouncy_distros[i].draw();
191 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
192 broken_bricks[i].draw();
194 /* Draw foreground: */
195 for (y = 0; y < 15; ++y)
197 for (x = 0; x < 21; ++x)
199 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
200 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
204 /* Draw particle systems (foreground) */
205 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
207 (*p)->draw(scroll_x, 0, 1);
212 World::action(double frame_ratio)
214 tux.action(frame_ratio);
216 /* Handle bouncy distros: */
217 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
218 bouncy_distros[i].action(frame_ratio);
220 /* Handle broken bricks: */
221 for (unsigned int i = 0; i < broken_bricks.size(); i++)
222 broken_bricks[i].action(frame_ratio);
224 /* Handle distro counting: */
225 if (counting_distros)
229 if (distro_counter <= 0)
230 counting_distros = -1;
233 // Handle all kinds of game objects
234 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
235 bouncy_bricks[i].action(frame_ratio);
237 for (unsigned int i = 0; i < floating_scores.size(); i++)
238 floating_scores[i].action(frame_ratio);
240 for (unsigned int i = 0; i < bullets.size(); ++i)
241 bullets[i].action(frame_ratio);
243 for (unsigned int i = 0; i < upgrades.size(); i++)
244 upgrades[i].action(frame_ratio);
246 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
247 (*i)->action(frame_ratio);
249 /* update particle systems */
250 std::vector<ParticleSystem*>::iterator p;
251 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
253 (*p)->simulate(frame_ratio);
256 /* Handle all possible collisions. */
259 { // Cleanup marked badguys
260 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
261 if ((*i)->is_removable())
263 bad_guys.remove_if(std::mem_fun(&BadGuy::is_removable));
268 World::collision_handler()
270 // CO_BULLET & CO_BADGUY check
271 for(unsigned int i = 0; i < bullets.size(); ++i)
273 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
275 if((*j)->dying != DYING_NOT)
278 if(rectcollision(bullets[i].base, (*j)->base))
280 // We have detected a collision and now call the
281 // collision functions of the collided objects.
282 // collide with bad_guy first, since bullet_collision will
284 (*j)->collision(0, CO_BULLET);
285 bullets[i].collision(CO_BADGUY);
286 break; // bullet is invalid now, so break
291 /* CO_BADGUY & CO_BADGUY check */
292 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
294 if((*i)->dying != DYING_NOT)
297 BadGuys::iterator j = i;
299 for (; j != bad_guys.end(); ++j)
301 if(j == i || (*j)->dying != DYING_NOT)
304 if(rectcollision((*i)->base, (*j)->base))
306 // We have detected a collision and now call the
307 // collision functions of the collided objects.
308 (*j)->collision(*i, CO_BADGUY);
309 (*i)->collision(*j, CO_BADGUY);
314 if(tux.dying != DYING_NOT) return;
316 // CO_BADGUY & CO_PLAYER check
317 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
319 if((*i)->dying != DYING_NOT)
322 if(rectcollision_offset((*i)->base, tux.base, 0, 0))
324 // We have detected a collision and now call the collision
325 // functions of the collided objects.
326 if (tux.previous_base.y < tux.base.y &&
327 tux.previous_base.y + tux.previous_base.height
328 < (*i)->base.y + (*i)->base.height/2)
330 (*i)->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
334 tux.collision(*i, CO_BADGUY);
335 (*i)->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
340 // CO_UPGRADE & CO_PLAYER check
341 for(unsigned int i = 0; i < upgrades.size(); ++i)
343 if(rectcollision(upgrades[i].base, tux.base))
345 // We have detected a collision and now call the collision
346 // functions of the collided objects.
347 upgrades[i].collision(&tux, CO_PLAYER);
353 World::add_score(float x, float y, int s)
355 player_status.score += s;
357 FloatingScore new_floating_score;
358 new_floating_score.init(x,y,s);
359 floating_scores.push_back(new_floating_score);
363 World::add_bouncy_distro(float x, float y)
365 BouncyDistro new_bouncy_distro;
366 new_bouncy_distro.init(x,y);
367 bouncy_distros.push_back(new_bouncy_distro);
371 World::add_broken_brick(Tile* tile, float x, float y)
373 add_broken_brick_piece(tile, x, y, -1, -4);
374 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
376 add_broken_brick_piece(tile, x + 16, y, 1, -4);
377 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
381 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
383 BrokenBrick new_broken_brick;
384 new_broken_brick.init(tile, x, y, xm, ym);
385 broken_bricks.push_back(new_broken_brick);
389 World::add_bouncy_brick(float x, float y)
391 BouncyBrick new_bouncy_brick;
392 new_bouncy_brick.init(x,y);
393 bouncy_bricks.push_back(new_bouncy_brick);
397 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
399 BadGuy* badguy = new BadGuy(x,y,kind, stay_on_platform);
400 bad_guys.push_back(badguy);
405 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
408 new_upgrade.init(x,y,dir,kind);
409 upgrades.push_back(new_upgrade);
413 World::add_bullet(float x, float y, float xm, Direction dir)
415 if(bullets.size() > MAX_BULLETS-1)
419 new_bullet.init(x,y,xm,dir);
420 bullets.push_back(new_bullet);
422 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
426 World::play_music(int musictype)
428 currentmusic = musictype;
429 switch(currentmusic) {
431 ::play_music(get_level()->get_level_music_fast());
434 ::play_music(get_level()->get_level_music());
437 ::play_music(herring_song);
446 World::get_music_type()
453 World::trybreakbrick(float x, float y, bool small)
455 Level* plevel = get_level();
457 Tile* tile = gettile(x, y);
462 /* Get a distro from it: */
463 add_bouncy_distro(((int)(x + 1) / 32) * 32,
466 if (!counting_distros)
468 counting_distros = true;
472 if (distro_counter <= 0)
473 plevel->change(x, y, TM_IA, tile->next_tile);
475 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
476 player_status.score = player_status.score + SCORE_DISTRO;
477 player_status.distros++;
482 plevel->change(x, y, TM_IA, tile->next_tile);
484 /* Replace it with broken bits: */
485 add_broken_brick(tile,
486 ((int)(x + 1) / 32) * 32,
489 /* Get some score: */
490 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
491 player_status.score = player_status.score + SCORE_BRICK;
498 World::tryemptybox(float x, float y, Direction col_side)
500 Tile* tile = gettile(x,y);
504 // according to the collision side, set the upgrade direction
510 int posx = ((int)(x+1) / 32) * 32;
511 int posy = (int)(y/32) * 32 - 32;
514 case 1: // Box with a distro!
515 add_bouncy_distro(posx, posy);
516 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
517 player_status.score = player_status.score + SCORE_DISTRO;
518 player_status.distros++;
521 case 2: // Add an upgrade!
522 if (tux.size == SMALL) /* Tux is small, add mints! */
523 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
524 else /* Tux is big, add an iceflower: */
525 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
526 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
529 case 3: // Add a golden herring
530 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
533 case 4: // Add a 1up extra
534 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
541 level->change(x, y, TM_IA, tile->next_tile);
544 /* Try to grab a distro: */
546 World::trygrabdistro(float x, float y, int bounciness)
548 Tile* tile = gettile(x, y);
549 if (tile && tile->distro)
551 level->change(x, y, TM_IA, tile->next_tile);
552 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
554 if (bounciness == BOUNCE)
556 add_bouncy_distro(((int)(x + 1) / 32) * 32,
560 player_status.score = player_status.score + SCORE_DISTRO;
561 player_status.distros++;
565 /* Try to bump a bad guy from below: */
567 World::trybumpbadguy(float x, float y)
570 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
572 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
573 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
575 (*i)->collision(&tux, CO_PLAYER, COLLISION_BUMP);
580 for (unsigned int i = 0; i < upgrades.size(); i++)
582 if (upgrades[i].base.height == 32 &&
583 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
584 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
586 upgrades[i].base.xm = -upgrades[i].base.xm;
587 upgrades[i].base.ym = -8;
588 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);