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();
72 play_music(LEVEL_MUSIC);
77 halt_music(); // just to be sure (because levelmusic is freed now)
87 player_status.score_multiplier = 1;
89 counting_distros = false;
92 /* set current song/music */
93 currentmusic = LEVEL_MUSIC;
97 World::activate_bad_guys()
99 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
100 i != level->badguy_data.end();
103 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
108 World::activate_particle_systems()
110 if (level->particle_system == "clouds")
112 particle_systems.push_back(new CloudParticleSystem);
114 else if (level->particle_system == "snow")
116 particle_systems.push_back(new SnowParticleSystem);
118 else if (level->particle_system != "")
120 st_abort("unknown particle system specified in level", "");
129 /* Draw the real background */
130 if(get_level()->bkgd_image[0] != '\0')
132 int s = ((int)scroll_x / 2)%640;
133 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
134 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
138 drawgradient(level->bkgd_top, level->bkgd_bottom);
141 /* Draw particle systems (background) */
142 std::vector<ParticleSystem*>::iterator p;
143 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
145 (*p)->draw(scroll_x, 0, 0);
148 /* Draw background: */
149 for (y = 0; y < 15; ++y)
151 for (x = 0; x < 21; ++x)
153 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
154 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
158 /* Draw interactive tiles: */
159 for (y = 0; y < 15; ++y)
161 for (x = 0; x < 21; ++x)
163 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
164 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
168 /* (Bouncy bricks): */
169 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
170 bouncy_bricks[i].draw();
172 for (unsigned int i = 0; i < bad_guys.size(); ++i)
177 for (unsigned int i = 0; i < bullets.size(); ++i)
180 for (unsigned int i = 0; i < floating_scores.size(); ++i)
181 floating_scores[i].draw();
183 for (unsigned int i = 0; i < upgrades.size(); ++i)
186 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
187 bouncy_distros[i].draw();
189 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
190 broken_bricks[i].draw();
192 /* Draw foreground: */
193 for (y = 0; y < 15; ++y)
195 for (x = 0; x < 21; ++x)
197 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
198 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
202 /* Draw particle systems (foreground) */
203 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
205 (*p)->draw(scroll_x, 0, 1);
210 World::action(double frame_ratio)
212 tux.action(frame_ratio);
214 /* Handle bouncy distros: */
215 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
216 bouncy_distros[i].action(frame_ratio);
218 /* Handle broken bricks: */
219 for (unsigned int i = 0; i < broken_bricks.size(); i++)
220 broken_bricks[i].action(frame_ratio);
222 /* Handle distro counting: */
223 if (counting_distros)
227 if (distro_counter <= 0)
228 counting_distros = -1;
231 // Handle all kinds of game objects
232 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
233 bouncy_bricks[i].action(frame_ratio);
235 for (unsigned int i = 0; i < floating_scores.size(); i++)
236 floating_scores[i].action(frame_ratio);
238 for (unsigned int i = 0; i < bullets.size(); ++i)
239 bullets[i].action(frame_ratio);
241 for (unsigned int i = 0; i < upgrades.size(); i++)
242 upgrades[i].action(frame_ratio);
244 for (unsigned int i = 0; i < bad_guys.size(); i++)
245 bad_guys[i].action(frame_ratio);
247 /* update particle systems */
248 std::vector<ParticleSystem*>::iterator p;
249 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
251 (*p)->simulate(frame_ratio);
254 /* Handle all possible collisions. */
260 World::collision_handler()
262 // CO_BULLET & CO_BADGUY check
263 for(unsigned int i = 0; i < bullets.size(); ++i)
265 for(unsigned int j = 0; j < bad_guys.size(); ++j)
267 if(bad_guys[j].dying != DYING_NOT)
269 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
271 // We have detected a collision and now call the
272 // collision functions of the collided objects.
273 // collide with bad_guy first, since bullet_collision will
275 bad_guys[j].collision(0, CO_BULLET);
276 bullets[i].collision(CO_BADGUY);
277 break; // bullet is invalid now, so break
282 /* CO_BADGUY & CO_BADGUY check */
283 for(unsigned int i = 0; i < bad_guys.size(); ++i)
285 if(bad_guys[i].dying != DYING_NOT)
288 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
290 if(j == i || bad_guys[j].dying != DYING_NOT)
293 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
295 // We have detected a collision and now call the
296 // collision functions of the collided objects.
297 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
298 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
303 if(tux.dying != DYING_NOT) return;
305 // CO_BADGUY & CO_PLAYER check
306 for(unsigned int i = 0; i < bad_guys.size(); ++i)
308 if(bad_guys[i].dying != DYING_NOT)
311 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
313 // We have detected a collision and now call the collision
314 // functions of the collided objects.
315 if (tux.previous_base.y < tux.base.y &&
316 tux.previous_base.y + tux.previous_base.height
317 < bad_guys[i].base.y + bad_guys[i].base.height/2)
319 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
323 tux.collision(&bad_guys[i], CO_BADGUY);
324 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
329 // CO_UPGRADE & CO_PLAYER check
330 for(unsigned int i = 0; i < upgrades.size(); ++i)
332 if(rectcollision(&upgrades[i].base, &tux.base))
334 // We have detected a collision and now call the collision
335 // functions of the collided objects.
336 upgrades[i].collision(&tux, CO_PLAYER);
342 World::add_score(float x, float y, int s)
344 player_status.score += s;
346 FloatingScore new_floating_score;
347 new_floating_score.init(x,y,s);
348 floating_scores.push_back(new_floating_score);
352 World::add_bouncy_distro(float x, float y)
354 BouncyDistro new_bouncy_distro;
355 new_bouncy_distro.init(x,y);
356 bouncy_distros.push_back(new_bouncy_distro);
360 World::add_broken_brick(Tile* tile, float x, float y)
362 add_broken_brick_piece(tile, x, y, -1, -4);
363 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
365 add_broken_brick_piece(tile, x + 16, y, 1, -4);
366 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
370 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
372 BrokenBrick new_broken_brick;
373 new_broken_brick.init(tile, x, y, xm, ym);
374 broken_bricks.push_back(new_broken_brick);
378 World::add_bouncy_brick(float x, float y)
380 BouncyBrick new_bouncy_brick;
381 new_bouncy_brick.init(x,y);
382 bouncy_bricks.push_back(new_bouncy_brick);
386 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
388 bad_guys.push_back(BadGuy());
389 BadGuy& new_bad_guy = bad_guys.back();
391 new_bad_guy.init(x,y,kind, stay_on_platform);
395 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
398 new_upgrade.init(x,y,dir,kind);
399 upgrades.push_back(new_upgrade);
403 World::add_bullet(float x, float y, float xm, Direction dir)
405 if(bullets.size() > MAX_BULLETS-1)
409 new_bullet.init(x,y,xm,dir);
410 bullets.push_back(new_bullet);
412 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
416 World::play_music(int musictype)
418 currentmusic = musictype;
419 switch(currentmusic) {
421 ::play_music(get_level()->get_level_music_fast());
424 ::play_music(get_level()->get_level_music());
427 ::play_music(herring_song);
436 World::get_music_type()
443 World::trybreakbrick(float x, float y, bool small)
445 Level* plevel = get_level();
447 Tile* tile = gettile(x, y);
452 /* Get a distro from it: */
453 add_bouncy_distro(((int)(x + 1) / 32) * 32,
456 if (!counting_distros)
458 counting_distros = true;
462 if (distro_counter <= 0)
463 plevel->change(x, y, TM_IA, tile->next_tile);
465 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
466 player_status.score = player_status.score + SCORE_DISTRO;
467 player_status.distros++;
472 plevel->change(x, y, TM_IA, tile->next_tile);
474 /* Replace it with broken bits: */
475 add_broken_brick(tile,
476 ((int)(x + 1) / 32) * 32,
479 /* Get some score: */
480 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
481 player_status.score = player_status.score + SCORE_BRICK;
488 World::tryemptybox(float x, float y, Direction col_side)
490 Tile* tile = gettile(x,y);
494 // according to the collision side, set the upgrade direction
500 int posx = ((int)(x+1) / 32) * 32;
501 int posy = (int)(y/32) * 32 - 32;
504 case 1: // Box with a distro!
505 add_bouncy_distro(posx, posy);
506 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
507 player_status.score = player_status.score + SCORE_DISTRO;
508 player_status.distros++;
511 case 2: // Add an upgrade!
512 if (tux.size == SMALL) /* Tux is small, add mints! */
513 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
514 else /* Tux is big, add an iceflower: */
515 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
516 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
519 case 3: // Add a golden herring
520 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
523 case 4: // Add a 1up extra
524 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
531 level->change(x, y, TM_IA, tile->next_tile);
534 /* Try to grab a distro: */
536 World::trygrabdistro(float x, float y, int bounciness)
538 Tile* tile = gettile(x, y);
539 if (tile && tile->distro)
541 level->change(x, y, TM_IA, tile->next_tile);
542 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
544 if (bounciness == BOUNCE)
546 add_bouncy_distro(((int)(x + 1) / 32) * 32,
550 player_status.score = player_status.score + SCORE_DISTRO;
551 player_status.distros++;
555 /* Try to bump a bad guy from below: */
557 World::trybumpbadguy(float x, float y)
560 for (unsigned int i = 0; i < bad_guys.size(); i++)
562 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
563 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
565 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
571 for (unsigned int i = 0; i < upgrades.size(); i++)
573 if (upgrades[i].base.height == 32 &&
574 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
575 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
577 upgrades[i].base.xm = -upgrades[i].base.xm;
578 upgrades[i].base.ym = -8;
579 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);