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 halt_music(); // just to be sure (because levelmusic is freed now)
85 player_status.score_multiplier = 1;
87 counting_distros = false;
90 /* set current song/music */
91 currentmusic = LEVEL_MUSIC;
95 World::activate_bad_guys()
97 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
98 i != level->badguy_data.end();
101 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
106 World::activate_particle_systems()
108 if (level->particle_system == "clouds")
110 particle_systems.push_back(new CloudParticleSystem);
112 else if (level->particle_system == "snow")
114 particle_systems.push_back(new SnowParticleSystem);
116 else if (level->particle_system != "")
118 st_abort("unknown particle system specified in level", "");
127 /* Draw the real background */
128 if(get_level()->bkgd_image[0] != '\0')
130 int s = ((int)scroll_x / 2)%640;
131 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
132 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
136 drawgradient(level->bkgd_top, level->bkgd_bottom);
139 /* Draw particle systems (background) */
140 std::vector<ParticleSystem*>::iterator p;
141 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
143 (*p)->draw(scroll_x, 0, 0);
146 /* Draw background: */
147 for (y = 0; y < 15; ++y)
149 for (x = 0; x < 21; ++x)
151 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
152 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
156 /* Draw interactive tiles: */
157 for (y = 0; y < 15; ++y)
159 for (x = 0; x < 21; ++x)
161 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
162 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
166 /* (Bouncy bricks): */
167 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
168 bouncy_bricks[i].draw();
170 for (unsigned int i = 0; i < bad_guys.size(); ++i)
175 for (unsigned int i = 0; i < bullets.size(); ++i)
178 for (unsigned int i = 0; i < floating_scores.size(); ++i)
179 floating_scores[i].draw();
181 for (unsigned int i = 0; i < upgrades.size(); ++i)
184 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
185 bouncy_distros[i].draw();
187 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
188 broken_bricks[i].draw();
190 /* Draw foreground: */
191 for (y = 0; y < 15; ++y)
193 for (x = 0; x < 21; ++x)
195 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
196 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
200 /* Draw particle systems (foreground) */
201 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
203 (*p)->draw(scroll_x, 0, 1);
208 World::action(double frame_ratio)
210 tux.action(frame_ratio);
212 /* Handle bouncy distros: */
213 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
214 bouncy_distros[i].action(frame_ratio);
216 /* Handle broken bricks: */
217 for (unsigned int i = 0; i < broken_bricks.size(); i++)
218 broken_bricks[i].action(frame_ratio);
220 /* Handle distro counting: */
221 if (counting_distros)
225 if (distro_counter <= 0)
226 counting_distros = -1;
229 // Handle all kinds of game objects
230 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
231 bouncy_bricks[i].action(frame_ratio);
233 for (unsigned int i = 0; i < floating_scores.size(); i++)
234 floating_scores[i].action(frame_ratio);
236 for (unsigned int i = 0; i < bullets.size(); ++i)
237 bullets[i].action(frame_ratio);
239 for (unsigned int i = 0; i < upgrades.size(); i++)
240 upgrades[i].action(frame_ratio);
242 for (unsigned int i = 0; i < bad_guys.size(); i++)
243 bad_guys[i].action(frame_ratio);
245 /* update particle systems */
246 std::vector<ParticleSystem*>::iterator p;
247 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
249 (*p)->simulate(frame_ratio);
252 /* Handle all possible collisions. */
258 World::collision_handler()
260 // CO_BULLET & CO_BADGUY check
261 for(unsigned int i = 0; i < bullets.size(); ++i)
263 for(unsigned int j = 0; j < bad_guys.size(); ++j)
265 if(bad_guys[j].dying != DYING_NOT)
267 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
269 // We have detected a collision and now call the
270 // collision functions of the collided objects.
271 // collide with bad_guy first, since bullet_collision will
273 bad_guys[j].collision(0, CO_BULLET);
274 bullets[i].collision(CO_BADGUY);
275 break; // bullet is invalid now, so break
280 /* CO_BADGUY & CO_BADGUY check */
281 for(unsigned int i = 0; i < bad_guys.size(); ++i)
283 if(bad_guys[i].dying != DYING_NOT)
286 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
288 if(j == i || bad_guys[j].dying != DYING_NOT)
291 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
293 // We have detected a collision and now call the
294 // collision functions of the collided objects.
295 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
296 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
301 if(tux.dying != DYING_NOT) return;
303 // CO_BADGUY & CO_PLAYER check
304 for(unsigned int i = 0; i < bad_guys.size(); ++i)
306 if(bad_guys[i].dying != DYING_NOT)
309 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
311 // We have detected a collision and now call the collision
312 // functions of the collided objects.
313 if (tux.previous_base.y < tux.base.y &&
314 tux.previous_base.y + tux.previous_base.height
315 < bad_guys[i].base.y + bad_guys[i].base.height/2)
317 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
321 tux.collision(&bad_guys[i], CO_BADGUY);
322 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
327 // CO_UPGRADE & CO_PLAYER check
328 for(unsigned int i = 0; i < upgrades.size(); ++i)
330 if(rectcollision(&upgrades[i].base, &tux.base))
332 // We have detected a collision and now call the collision
333 // functions of the collided objects.
334 upgrades[i].collision(&tux, CO_PLAYER);
340 World::add_score(float x, float y, int s)
342 player_status.score += s;
344 FloatingScore new_floating_score;
345 new_floating_score.init(x,y,s);
346 floating_scores.push_back(new_floating_score);
350 World::add_bouncy_distro(float x, float y)
352 BouncyDistro new_bouncy_distro;
353 new_bouncy_distro.init(x,y);
354 bouncy_distros.push_back(new_bouncy_distro);
358 World::add_broken_brick(Tile* tile, float x, float y)
360 add_broken_brick_piece(tile, x, y, -1, -4);
361 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
363 add_broken_brick_piece(tile, x + 16, y, 1, -4);
364 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
368 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
370 BrokenBrick new_broken_brick;
371 new_broken_brick.init(tile, x, y, xm, ym);
372 broken_bricks.push_back(new_broken_brick);
376 World::add_bouncy_brick(float x, float y)
378 BouncyBrick new_bouncy_brick;
379 new_bouncy_brick.init(x,y);
380 bouncy_bricks.push_back(new_bouncy_brick);
384 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
386 bad_guys.push_back(BadGuy());
387 BadGuy& new_bad_guy = bad_guys.back();
389 new_bad_guy.init(x,y,kind, stay_on_platform);
393 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
396 new_upgrade.init(x,y,dir,kind);
397 upgrades.push_back(new_upgrade);
401 World::add_bullet(float x, float y, float xm, Direction dir)
403 if(bullets.size() > MAX_BULLETS-1)
407 new_bullet.init(x,y,xm,dir);
408 bullets.push_back(new_bullet);
410 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
414 World::play_music(int musictype)
416 currentmusic = musictype;
417 switch(currentmusic) {
419 ::play_music(get_level()->get_level_music_fast());
422 ::play_music(get_level()->get_level_music());
425 ::play_music(herring_song);
434 World::get_music_type()
441 World::trybreakbrick(float x, float y, bool small)
443 Level* plevel = get_level();
445 Tile* tile = gettile(x, y);
450 /* Get a distro from it: */
451 add_bouncy_distro(((int)(x + 1) / 32) * 32,
454 if (!counting_distros)
456 counting_distros = true;
460 if (distro_counter <= 0)
461 plevel->change(x, y, TM_IA, tile->next_tile);
463 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
464 player_status.score = player_status.score + SCORE_DISTRO;
465 player_status.distros++;
470 plevel->change(x, y, TM_IA, tile->next_tile);
472 /* Replace it with broken bits: */
473 add_broken_brick(tile,
474 ((int)(x + 1) / 32) * 32,
477 /* Get some score: */
478 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
479 player_status.score = player_status.score + SCORE_BRICK;
486 World::tryemptybox(float x, float y, Direction col_side)
488 Tile* tile = gettile(x,y);
492 // according to the collision side, set the upgrade direction
498 int posx = ((int)(x+1) / 32) * 32;
499 int posy = (int)(y/32) * 32 - 32;
502 case 1: // Box with a distro!
503 add_bouncy_distro(posx, posy);
504 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
505 player_status.score = player_status.score + SCORE_DISTRO;
506 player_status.distros++;
509 case 2: // Add an upgrade!
510 if (tux.size == SMALL) /* Tux is small, add mints! */
511 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
512 else /* Tux is big, add an iceflower: */
513 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
514 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
517 case 3: // Add a golden herring
518 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
521 case 4: // Add a 1up extra
522 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
529 level->change(x, y, TM_IA, tile->next_tile);
532 /* Try to grab a distro: */
534 World::trygrabdistro(float x, float y, int bounciness)
536 Tile* tile = gettile(x, y);
537 if (tile && tile->distro)
539 level->change(x, y, TM_IA, tile->next_tile);
540 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
542 if (bounciness == BOUNCE)
544 add_bouncy_distro(((int)(x + 1) / 32) * 32,
548 player_status.score = player_status.score + SCORE_DISTRO;
549 player_status.distros++;
553 /* Try to bump a bad guy from below: */
555 World::trybumpbadguy(float x, float y)
558 for (unsigned int i = 0; i < bad_guys.size(); i++)
560 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
561 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
563 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
569 for (unsigned int i = 0; i < upgrades.size(); i++)
571 if (upgrades[i].base.height == 32 &&
572 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
573 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
575 upgrades[i].base.xm = -upgrades[i].base.xm;
576 upgrades[i].base.ym = -8;
577 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);