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.
31 texture_type img_distro[4];
33 World* World::current_ = 0;
39 // FIXME: Move this to action and draw and everywhere else where the
40 // world calls child functions
59 timer_init(&super_bkgd_timer, true);
61 counting_distros = false;
64 /* set current song/music */
65 set_current_music(LEVEL_MUSIC);
69 World::load(const std::string& subset, int level_nr)
71 return level->load(subset, level_nr);
75 World::load(const std::string& filename)
77 return level->load(filename);
81 World::arrays_free(void)
84 bouncy_distros.clear();
85 broken_bricks.clear();
86 bouncy_bricks.clear();
87 floating_scores.clear();
90 std::vector<ParticleSystem*>::iterator i;
91 for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
94 particle_systems.clear();
98 World::activate_bad_guys()
100 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
101 i != level->badguy_data.end();
104 add_bad_guy(i->x, i->y, i->kind);
109 World::activate_particle_systems()
111 if (level->particle_system == "clouds")
113 particle_systems.push_back(new CloudParticleSystem);
115 else if (level->particle_system == "snow")
117 particle_systems.push_back(new SnowParticleSystem);
119 else if (level->particle_system != "")
121 st_abort("unknown particle system specified in level", "");
131 if(timer_check(&super_bkgd_timer))
132 texture_draw(&img_super_bkgd, 0, 0);
135 /* Draw the real background */
136 if(get_level()->bkgd_image[0] != '\0')
138 int s = (int)scroll_x / 30;
139 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
140 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
144 clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
148 /* Draw particle systems (background) */
149 std::vector<ParticleSystem*>::iterator p;
150 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
152 (*p)->draw(scroll_x, 0, 0);
155 /* Draw background: */
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->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
165 /* Draw interactive tiles: */
166 for (y = 0; y < 15; ++y)
168 for (x = 0; x < 21; ++x)
170 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
171 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
175 /* (Bouncy bricks): */
176 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
177 bouncy_bricks[i].draw();
179 for (unsigned int i = 0; i < bad_guys.size(); ++i)
184 for (unsigned int i = 0; i < bullets.size(); ++i)
187 for (unsigned int i = 0; i < floating_scores.size(); ++i)
188 floating_scores[i].draw();
190 for (unsigned int i = 0; i < upgrades.size(); ++i)
193 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
194 bouncy_distros[i].draw();
196 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
197 broken_bricks[i].draw();
199 /* Draw foreground: */
200 for (y = 0; y < 15; ++y)
202 for (x = 0; x < 21; ++x)
204 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
205 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
209 /* Draw particle systems (foreground) */
210 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
212 (*p)->draw(scroll_x, 0, 1);
219 /* Handle bouncy distros: */
220 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
221 bouncy_distros[i].action();
223 /* Handle broken bricks: */
224 for (unsigned int i = 0; i < broken_bricks.size(); i++)
225 broken_bricks[i].action();
227 /* Handle distro counting: */
228 if (counting_distros)
232 if (distro_counter <= 0)
233 counting_distros = -1;
236 // Handle all kinds of game objects
237 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
238 bouncy_bricks[i].action();
240 for (unsigned int i = 0; i < floating_scores.size(); i++)
241 floating_scores[i].action();
243 for (unsigned int i = 0; i < bullets.size(); ++i)
246 for (unsigned int i = 0; i < upgrades.size(); i++)
247 upgrades[i].action();
249 for (unsigned int i = 0; i < bad_guys.size(); i++)
250 bad_guys[i].action();
252 /* update particle systems */
253 std::vector<ParticleSystem*>::iterator p;
254 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
256 (*p)->simulate(frame_ratio);
259 /* Handle all possible collisions. */
265 World::collision_handler()
267 // CO_BULLET & CO_BADGUY check
268 for(unsigned int i = 0; i < bullets.size(); ++i)
270 for(unsigned int j = 0; j < bad_guys.size(); ++j)
272 if(bad_guys[j].dying != DYING_NOT)
274 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
276 // We have detected a collision and now call the
277 // collision functions of the collided objects.
278 // collide with bad_guy first, since bullet_collision will
280 bad_guys[j].collision(0, CO_BULLET);
281 bullets[i].collision(CO_BADGUY);
282 break; // bullet is invalid now, so break
287 /* CO_BADGUY & CO_BADGUY check */
288 for(unsigned int i = 0; i < bad_guys.size(); ++i)
290 if(bad_guys[i].dying != DYING_NOT)
293 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
295 if(j == i || bad_guys[j].dying != DYING_NOT)
298 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
300 // We have detected a collision and now call the
301 // collision functions of the collided objects.
302 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
303 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
308 if(tux.dying != DYING_NOT) return;
310 // CO_BADGUY & CO_PLAYER check
311 for(unsigned int i = 0; i < bad_guys.size(); ++i)
313 if(bad_guys[i].dying != DYING_NOT)
316 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
318 // We have detected a collision and now call the collision
319 // functions of the collided objects.
320 if (tux.previous_base.y < tux.base.y &&
321 tux.previous_base.y + tux.previous_base.height
322 < bad_guys[i].base.y + bad_guys[i].base.height/2)
324 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
328 tux.collision(&bad_guys[i], CO_BADGUY);
333 // CO_UPGRADE & CO_PLAYER check
334 for(unsigned int i = 0; i < upgrades.size(); ++i)
336 if(rectcollision(&upgrades[i].base, &tux.base))
338 // We have detected a collision and now call the collision
339 // functions of the collided objects.
340 upgrades[i].collision(&tux, CO_PLAYER);
346 World::add_score(float x, float y, int s)
350 FloatingScore new_floating_score;
351 new_floating_score.init(x,y,s);
352 floating_scores.push_back(new_floating_score);
356 World::add_bouncy_distro(float x, float y)
358 BouncyDistro new_bouncy_distro;
359 new_bouncy_distro.init(x,y);
360 bouncy_distros.push_back(new_bouncy_distro);
364 World::add_broken_brick(Tile* tile, float x, float y)
366 add_broken_brick_piece(tile, x, y, -1, -4);
367 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
369 add_broken_brick_piece(tile, x + 16, y, 1, -4);
370 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
374 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
376 BrokenBrick new_broken_brick;
377 new_broken_brick.init(tile, x, y, xm, ym);
378 broken_bricks.push_back(new_broken_brick);
382 World::add_bouncy_brick(float x, float y)
384 BouncyBrick new_bouncy_brick;
385 new_bouncy_brick.init(x,y);
386 bouncy_bricks.push_back(new_bouncy_brick);
390 World::add_bad_guy(float x, float y, BadGuyKind kind)
392 bad_guys.push_back(BadGuy());
393 BadGuy& new_bad_guy = bad_guys.back();
395 new_bad_guy.init(x,y,kind);
399 World::add_upgrade(float x, float y, int dir, int kind)
402 new_upgrade.init(x,y,dir,kind);
403 upgrades.push_back(new_upgrade);
407 World::add_bullet(float x, float y, float xm, int dir)
410 new_bullet.init(x,y,xm,dir);
411 bullets.push_back(new_bullet);
413 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
418 World::trybreakbrick(float x, float y, bool small)
420 Level* plevel = get_level();
422 Tile* tile = gettile(x, y);
427 /* Get a distro from it: */
428 add_bouncy_distro(((int)(x + 1) / 32) * 32,
431 if (!counting_distros)
433 counting_distros = true;
437 if (distro_counter <= 0)
438 plevel->change(x, y, TM_IA, tile->next_tile);
440 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
441 score = score + SCORE_DISTRO;
447 plevel->change(x, y, TM_IA, tile->next_tile);
449 /* Replace it with broken bits: */
450 add_broken_brick(tile,
451 ((int)(x + 1) / 32) * 32,
454 /* Get some score: */
455 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
456 score = score + SCORE_BRICK;
463 World::tryemptybox(float x, float y, int col_side)
465 Tile* tile = gettile(x,y);
469 // according to the collision side, set the upgrade direction
477 case 1: // Box with a distro!
478 add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
479 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
480 score = score + SCORE_DISTRO;
484 case 2: // Add an upgrade!
485 if (tux.size == SMALL) /* Tux is small, add mints! */
486 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
487 else /* Tux is big, add coffee: */
488 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
489 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
492 case 3: // Add a golden herring
493 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
500 level->change(x, y, TM_IA, tile->next_tile);
503 /* Try to grab a distro: */
505 World::trygrabdistro(float x, float y, int bounciness)
507 Tile* tile = gettile(x, y);
508 if (tile && tile->distro)
510 level->change(x, y, TM_IA, tile->next_tile);
511 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
513 if (bounciness == BOUNCE)
515 add_bouncy_distro(((int)(x + 1) / 32) * 32,
519 score = score + SCORE_DISTRO;
524 /* Try to bump a bad guy from below: */
526 World::trybumpbadguy(float x, float y)
529 for (unsigned int i = 0; i < bad_guys.size(); i++)
531 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
532 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
534 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
540 for (unsigned int i = 0; i < upgrades.size(); i++)
542 if (upgrades[i].base.height == 32 &&
543 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
544 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
546 upgrades[i].base.xm = -upgrades[i].base.xm;
547 upgrades[i].base.ym = -8;
548 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);