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();
84 player_status.score_multiplier = 1;
86 counting_distros = false;
89 /* set current song/music */
90 set_current_music(LEVEL_MUSIC);
94 World::activate_bad_guys()
96 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
97 i != level->badguy_data.end();
100 add_bad_guy(i->x, i->y, i->kind, i->stay_on_platform);
105 World::activate_particle_systems()
107 if (level->particle_system == "clouds")
109 particle_systems.push_back(new CloudParticleSystem);
111 else if (level->particle_system == "snow")
113 particle_systems.push_back(new SnowParticleSystem);
115 else if (level->particle_system != "")
117 st_abort("unknown particle system specified in level", "");
126 /* Draw the real background */
127 if(get_level()->bkgd_image[0] != '\0')
129 int s = ((int)scroll_x / 2)%640;
130 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
131 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
135 drawgradient(level->bkgd_top, level->bkgd_bottom);
138 /* Draw particle systems (background) */
139 std::vector<ParticleSystem*>::iterator p;
140 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
142 (*p)->draw(scroll_x, 0, 0);
145 /* Draw background: */
146 for (y = 0; y < 15; ++y)
148 for (x = 0; x < 21; ++x)
150 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
151 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
155 /* Draw interactive tiles: */
156 for (y = 0; y < 15; ++y)
158 for (x = 0; x < 21; ++x)
160 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
161 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
165 /* (Bouncy bricks): */
166 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
167 bouncy_bricks[i].draw();
169 for (unsigned int i = 0; i < bad_guys.size(); ++i)
174 for (unsigned int i = 0; i < bullets.size(); ++i)
177 for (unsigned int i = 0; i < floating_scores.size(); ++i)
178 floating_scores[i].draw();
180 for (unsigned int i = 0; i < upgrades.size(); ++i)
183 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
184 bouncy_distros[i].draw();
186 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
187 broken_bricks[i].draw();
189 /* Draw foreground: */
190 for (y = 0; y < 15; ++y)
192 for (x = 0; x < 21; ++x)
194 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
195 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
199 /* Draw particle systems (foreground) */
200 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
202 (*p)->draw(scroll_x, 0, 1);
207 World::action(double frame_ratio)
209 tux.action(frame_ratio);
211 /* Handle bouncy distros: */
212 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
213 bouncy_distros[i].action(frame_ratio);
215 /* Handle broken bricks: */
216 for (unsigned int i = 0; i < broken_bricks.size(); i++)
217 broken_bricks[i].action(frame_ratio);
219 /* Handle distro counting: */
220 if (counting_distros)
224 if (distro_counter <= 0)
225 counting_distros = -1;
228 // Handle all kinds of game objects
229 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
230 bouncy_bricks[i].action(frame_ratio);
232 for (unsigned int i = 0; i < floating_scores.size(); i++)
233 floating_scores[i].action(frame_ratio);
235 for (unsigned int i = 0; i < bullets.size(); ++i)
236 bullets[i].action(frame_ratio);
238 for (unsigned int i = 0; i < upgrades.size(); i++)
239 upgrades[i].action(frame_ratio);
241 for (unsigned int i = 0; i < bad_guys.size(); i++)
242 bad_guys[i].action(frame_ratio);
244 /* update particle systems */
245 std::vector<ParticleSystem*>::iterator p;
246 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
248 (*p)->simulate(frame_ratio);
251 /* Handle all possible collisions. */
257 World::collision_handler()
259 // CO_BULLET & CO_BADGUY check
260 for(unsigned int i = 0; i < bullets.size(); ++i)
262 for(unsigned int j = 0; j < bad_guys.size(); ++j)
264 if(bad_guys[j].dying != DYING_NOT)
266 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
268 // We have detected a collision and now call the
269 // collision functions of the collided objects.
270 // collide with bad_guy first, since bullet_collision will
272 bad_guys[j].collision(0, CO_BULLET);
273 bullets[i].collision(CO_BADGUY);
274 break; // bullet is invalid now, so break
279 /* CO_BADGUY & CO_BADGUY check */
280 for(unsigned int i = 0; i < bad_guys.size(); ++i)
282 if(bad_guys[i].dying != DYING_NOT)
285 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
287 if(j == i || bad_guys[j].dying != DYING_NOT)
290 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
292 // We have detected a collision and now call the
293 // collision functions of the collided objects.
294 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
295 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
300 if(tux.dying != DYING_NOT) return;
302 // CO_BADGUY & CO_PLAYER check
303 for(unsigned int i = 0; i < bad_guys.size(); ++i)
305 if(bad_guys[i].dying != DYING_NOT)
308 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
310 // We have detected a collision and now call the collision
311 // functions of the collided objects.
312 if (tux.previous_base.y < tux.base.y &&
313 tux.previous_base.y + tux.previous_base.height
314 < bad_guys[i].base.y + bad_guys[i].base.height/2)
316 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
320 tux.collision(&bad_guys[i], CO_BADGUY);
321 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
326 // CO_UPGRADE & CO_PLAYER check
327 for(unsigned int i = 0; i < upgrades.size(); ++i)
329 if(rectcollision(&upgrades[i].base, &tux.base))
331 // We have detected a collision and now call the collision
332 // functions of the collided objects.
333 upgrades[i].collision(&tux, CO_PLAYER);
339 World::add_score(float x, float y, int s)
341 player_status.score += s;
343 FloatingScore new_floating_score;
344 new_floating_score.init(x,y,s);
345 floating_scores.push_back(new_floating_score);
349 World::add_bouncy_distro(float x, float y)
351 BouncyDistro new_bouncy_distro;
352 new_bouncy_distro.init(x,y);
353 bouncy_distros.push_back(new_bouncy_distro);
357 World::add_broken_brick(Tile* tile, float x, float y)
359 add_broken_brick_piece(tile, x, y, -1, -4);
360 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
362 add_broken_brick_piece(tile, x + 16, y, 1, -4);
363 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
367 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
369 BrokenBrick new_broken_brick;
370 new_broken_brick.init(tile, x, y, xm, ym);
371 broken_bricks.push_back(new_broken_brick);
375 World::add_bouncy_brick(float x, float y)
377 BouncyBrick new_bouncy_brick;
378 new_bouncy_brick.init(x,y);
379 bouncy_bricks.push_back(new_bouncy_brick);
383 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
385 bad_guys.push_back(BadGuy());
386 BadGuy& new_bad_guy = bad_guys.back();
388 new_bad_guy.init(x,y,kind, stay_on_platform);
392 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
395 new_upgrade.init(x,y,dir,kind);
396 upgrades.push_back(new_upgrade);
400 World::add_bullet(float x, float y, float xm, Direction dir)
403 new_bullet.init(x,y,xm,dir);
404 bullets.push_back(new_bullet);
406 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
411 World::trybreakbrick(float x, float y, bool small)
413 Level* plevel = get_level();
415 Tile* tile = gettile(x, y);
420 /* Get a distro from it: */
421 add_bouncy_distro(((int)(x + 1) / 32) * 32,
424 if (!counting_distros)
426 counting_distros = true;
430 if (distro_counter <= 0)
431 plevel->change(x, y, TM_IA, tile->next_tile);
433 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
434 player_status.score = player_status.score + SCORE_DISTRO;
435 player_status.distros++;
440 plevel->change(x, y, TM_IA, tile->next_tile);
442 /* Replace it with broken bits: */
443 add_broken_brick(tile,
444 ((int)(x + 1) / 32) * 32,
447 /* Get some score: */
448 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
449 player_status.score = player_status.score + SCORE_BRICK;
456 World::tryemptybox(float x, float y, Direction col_side)
458 Tile* tile = gettile(x,y);
462 // according to the collision side, set the upgrade direction
468 int posx = ((int)(x+1) / 32) * 32;
469 int posy = (int)(y/32) * 32 - 32;
472 case 1: // Box with a distro!
473 add_bouncy_distro(posx, posy);
474 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
475 player_status.score = player_status.score + SCORE_DISTRO;
476 player_status.distros++;
479 case 2: // Add an upgrade!
480 if (tux.size == SMALL) /* Tux is small, add mints! */
481 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
482 else /* Tux is big, add an iceflower: */
483 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
484 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
487 case 3: // Add a golden herring
488 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
491 case 4: // Add a 1up extra
492 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
499 level->change(x, y, TM_IA, tile->next_tile);
502 /* Try to grab a distro: */
504 World::trygrabdistro(float x, float y, int bounciness)
506 Tile* tile = gettile(x, y);
507 if (tile && tile->distro)
509 level->change(x, y, TM_IA, tile->next_tile);
510 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
512 if (bounciness == BOUNCE)
514 add_bouncy_distro(((int)(x + 1) / 32) * 32,
518 player_status.score = player_status.score + SCORE_DISTRO;
519 player_status.distros++;
523 /* Try to bump a bad guy from below: */
525 World::trybumpbadguy(float x, float y)
528 for (unsigned int i = 0; i < bad_guys.size(); i++)
530 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
531 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
533 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
539 for (unsigned int i = 0; i < upgrades.size(); i++)
541 if (upgrades[i].base.height == 32 &&
542 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
543 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
545 upgrades[i].base.xm = -upgrades[i].base.xm;
546 upgrades[i].base.ym = -8;
547 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);