4 // Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include "resources.h"
32 Surface* img_distro[4];
34 World* World::current_ = 0;
36 World::World(const std::string& filename)
38 // FIXME: Move this to action and draw and everywhere else where the
39 // world calls child functions
45 level->load(filename);
48 get_level()->load_gfx();
50 activate_particle_systems();
51 get_level()->load_song();
54 World::World(const std::string& subset, int level_nr)
56 // FIXME: Move this to action and draw and everywhere else where the
57 // world calls child functions
63 level->load(subset, level_nr);
66 get_level()->load_gfx();
68 activate_particle_systems();
69 get_level()->load_song();
83 player_status.score_multiplier = 1;
85 counting_distros = false;
88 /* set current song/music */
89 set_current_music(LEVEL_MUSIC);
93 World::activate_bad_guys()
95 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
96 i != level->badguy_data.end();
99 add_bad_guy(i->x, i->y, i->kind);
104 World::activate_particle_systems()
106 if (level->particle_system == "clouds")
108 particle_systems.push_back(new CloudParticleSystem);
110 else if (level->particle_system == "snow")
112 particle_systems.push_back(new SnowParticleSystem);
114 else if (level->particle_system != "")
116 st_abort("unknown particle system specified in level", "");
125 /* Draw the real background */
126 if(get_level()->bkgd_image[0] != '\0')
128 int s = ((int)scroll_x / 2)%640;
129 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
130 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
134 drawgradient(level->bkgd_top, level->bkgd_bottom);
137 /* Draw particle systems (background) */
138 std::vector<ParticleSystem*>::iterator p;
139 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
141 (*p)->draw(scroll_x, 0, 0);
144 /* Draw background: */
145 for (y = 0; y < 15; ++y)
147 for (x = 0; x < 21; ++x)
149 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
150 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
154 /* Draw interactive tiles: */
155 for (y = 0; y < 15; ++y)
157 for (x = 0; x < 21; ++x)
159 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
160 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
164 /* (Bouncy bricks): */
165 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
166 bouncy_bricks[i].draw();
168 for (unsigned int i = 0; i < bad_guys.size(); ++i)
173 for (unsigned int i = 0; i < bullets.size(); ++i)
176 for (unsigned int i = 0; i < floating_scores.size(); ++i)
177 floating_scores[i].draw();
179 for (unsigned int i = 0; i < upgrades.size(); ++i)
182 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
183 bouncy_distros[i].draw();
185 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
186 broken_bricks[i].draw();
188 /* Draw foreground: */
189 for (y = 0; y < 15; ++y)
191 for (x = 0; x < 21; ++x)
193 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
194 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
198 /* Draw particle systems (foreground) */
199 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
201 (*p)->draw(scroll_x, 0, 1);
206 World::action(double frame_ratio)
208 /* Handle bouncy distros: */
209 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
210 bouncy_distros[i].action(frame_ratio);
212 /* Handle broken bricks: */
213 for (unsigned int i = 0; i < broken_bricks.size(); i++)
214 broken_bricks[i].action(frame_ratio);
216 /* Handle distro counting: */
217 if (counting_distros)
221 if (distro_counter <= 0)
222 counting_distros = -1;
225 // Handle all kinds of game objects
226 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
227 bouncy_bricks[i].action(frame_ratio);
229 for (unsigned int i = 0; i < floating_scores.size(); i++)
230 floating_scores[i].action(frame_ratio);
232 for (unsigned int i = 0; i < bullets.size(); ++i)
233 bullets[i].action(frame_ratio);
235 for (unsigned int i = 0; i < upgrades.size(); i++)
236 upgrades[i].action(frame_ratio);
238 for (unsigned int i = 0; i < bad_guys.size(); i++)
239 bad_guys[i].action(frame_ratio);
241 /* update particle systems */
242 std::vector<ParticleSystem*>::iterator p;
243 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
245 (*p)->simulate(frame_ratio);
248 /* Handle all possible collisions. */
254 World::collision_handler()
256 // CO_BULLET & CO_BADGUY check
257 for(unsigned int i = 0; i < bullets.size(); ++i)
259 for(unsigned int j = 0; j < bad_guys.size(); ++j)
261 if(bad_guys[j].dying != DYING_NOT)
263 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
265 // We have detected a collision and now call the
266 // collision functions of the collided objects.
267 // collide with bad_guy first, since bullet_collision will
269 bad_guys[j].collision(0, CO_BULLET);
270 bullets[i].collision(CO_BADGUY);
271 break; // bullet is invalid now, so break
276 /* CO_BADGUY & CO_BADGUY check */
277 for(unsigned int i = 0; i < bad_guys.size(); ++i)
279 if(bad_guys[i].dying != DYING_NOT)
282 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
284 if(j == i || bad_guys[j].dying != DYING_NOT)
287 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
289 // We have detected a collision and now call the
290 // collision functions of the collided objects.
291 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
292 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
297 if(tux.dying != DYING_NOT) return;
299 // CO_BADGUY & CO_PLAYER check
300 for(unsigned int i = 0; i < bad_guys.size(); ++i)
302 if(bad_guys[i].dying != DYING_NOT)
305 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
307 // We have detected a collision and now call the collision
308 // functions of the collided objects.
309 if (tux.previous_base.y < tux.base.y &&
310 tux.previous_base.y + tux.previous_base.height
311 < bad_guys[i].base.y + bad_guys[i].base.height/2)
313 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
317 tux.collision(&bad_guys[i], CO_BADGUY);
322 // CO_UPGRADE & CO_PLAYER check
323 for(unsigned int i = 0; i < upgrades.size(); ++i)
325 if(rectcollision(&upgrades[i].base, &tux.base))
327 // We have detected a collision and now call the collision
328 // functions of the collided objects.
329 upgrades[i].collision(&tux, CO_PLAYER);
335 World::add_score(float x, float y, int s)
337 player_status.score += s;
339 FloatingScore new_floating_score;
340 new_floating_score.init(x,y,s);
341 floating_scores.push_back(new_floating_score);
345 World::add_bouncy_distro(float x, float y)
347 BouncyDistro new_bouncy_distro;
348 new_bouncy_distro.init(x,y);
349 bouncy_distros.push_back(new_bouncy_distro);
353 World::add_broken_brick(Tile* tile, float x, float y)
355 add_broken_brick_piece(tile, x, y, -1, -4);
356 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
358 add_broken_brick_piece(tile, x + 16, y, 1, -4);
359 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
363 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
365 BrokenBrick new_broken_brick;
366 new_broken_brick.init(tile, x, y, xm, ym);
367 broken_bricks.push_back(new_broken_brick);
371 World::add_bouncy_brick(float x, float y)
373 BouncyBrick new_bouncy_brick;
374 new_bouncy_brick.init(x,y);
375 bouncy_bricks.push_back(new_bouncy_brick);
379 World::add_bad_guy(float x, float y, BadGuyKind kind)
381 bad_guys.push_back(BadGuy());
382 BadGuy& new_bad_guy = bad_guys.back();
384 new_bad_guy.init(x,y,kind);
388 World::add_upgrade(float x, float y, int dir, int kind)
391 new_upgrade.init(x,y,dir,kind);
392 upgrades.push_back(new_upgrade);
396 World::add_bullet(float x, float y, float xm, int dir)
399 new_bullet.init(x,y,xm,dir);
400 bullets.push_back(new_bullet);
402 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
407 World::trybreakbrick(float x, float y, bool small)
409 Level* plevel = get_level();
411 Tile* tile = gettile(x, y);
416 /* Get a distro from it: */
417 add_bouncy_distro(((int)(x + 1) / 32) * 32,
420 if (!counting_distros)
422 counting_distros = true;
426 if (distro_counter <= 0)
427 plevel->change(x, y, TM_IA, tile->next_tile);
429 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
430 player_status.score = player_status.score + SCORE_DISTRO;
431 player_status.distros++;
436 plevel->change(x, y, TM_IA, tile->next_tile);
438 /* Replace it with broken bits: */
439 add_broken_brick(tile,
440 ((int)(x + 1) / 32) * 32,
443 /* Get some score: */
444 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
445 player_status.score = player_status.score + SCORE_BRICK;
452 World::tryemptybox(float x, float y, int col_side)
454 Tile* tile = gettile(x,y);
458 // according to the collision side, set the upgrade direction
464 int posx = ((int)(x+1) / 32) * 32;
465 int posy = (int)(y/32) * 32 - 32;
468 case 1: // Box with a distro!
469 add_bouncy_distro(posx, posy);
470 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
471 player_status.score = player_status.score + SCORE_DISTRO;
472 player_status.distros++;
475 case 2: // Add an upgrade!
476 if (tux.size == SMALL) /* Tux is small, add mints! */
477 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
478 else /* Tux is big, add an iceflower: */
479 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
480 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
483 case 3: // Add a golden herring
484 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
487 case 4: // Add a 1up extra
488 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
495 level->change(x, y, TM_IA, tile->next_tile);
498 /* Try to grab a distro: */
500 World::trygrabdistro(float x, float y, int bounciness)
502 Tile* tile = gettile(x, y);
503 if (tile && tile->distro)
505 level->change(x, y, TM_IA, tile->next_tile);
506 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
508 if (bounciness == BOUNCE)
510 add_bouncy_distro(((int)(x + 1) / 32) * 32,
514 player_status.score = player_status.score + SCORE_DISTRO;
515 player_status.distros++;
519 /* Try to bump a bad guy from below: */
521 World::trybumpbadguy(float x, float y)
524 for (unsigned int i = 0; i < bad_guys.size(); i++)
526 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
527 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
529 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
535 for (unsigned int i = 0; i < upgrades.size(); i++)
537 if (upgrades[i].base.height == 32 &&
538 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
539 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
541 upgrades[i].base.xm = -upgrades[i].base.xm;
542 upgrades[i].base.ym = -8;
543 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);