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
58 timer_init(&super_bkgd_timer, true);
60 counting_distros = false;
65 /* set current song/music */
66 set_current_music(LEVEL_MUSIC);
70 World::load(const char* subset, int level_nr)
72 return level->load(subset, level_nr);
76 World::load(const std::string& filename)
78 return level->load(filename);
82 World::arrays_free(void)
85 bouncy_distros.clear();
86 broken_bricks.clear();
87 bouncy_bricks.clear();
88 floating_scores.clear();
91 std::vector<ParticleSystem*>::iterator i;
92 for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
95 particle_systems.clear();
99 World::activate_bad_guys()
101 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
102 i != level->badguy_data.end();
105 add_bad_guy(i->x, i->y, i->kind);
110 World::activate_particle_systems()
112 if (level->particle_system == "clouds")
114 particle_systems.push_back(new CloudParticleSystem);
116 else if (level->particle_system == "snow")
118 particle_systems.push_back(new SnowParticleSystem);
120 else if (level->particle_system != "")
122 st_abort("unknown particle system specified in level", "");
132 if(timer_check(&super_bkgd_timer))
133 texture_draw(&img_super_bkgd, 0, 0);
136 /* Draw the real background */
137 if(get_level()->bkgd_image[0] != '\0')
139 int s = (int)scroll_x / 30;
140 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
141 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
145 clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
149 /* Draw particle systems (background) */
150 std::vector<ParticleSystem*>::iterator p;
151 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
153 (*p)->draw(scroll_x, 0, 0);
156 /* Draw background: */
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->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
166 /* Draw interactive tiles: */
167 for (y = 0; y < 15; ++y)
169 for (x = 0; x < 21; ++x)
171 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
172 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
176 /* (Bouncy bricks): */
177 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
178 bouncy_bricks[i].draw();
180 for (unsigned int i = 0; i < bad_guys.size(); ++i)
185 for (unsigned int i = 0; i < bullets.size(); ++i)
186 bullet_draw(&bullets[i]);
188 for (unsigned int i = 0; i < floating_scores.size(); ++i)
189 floating_scores[i].draw();
191 for (unsigned int i = 0; i < upgrades.size(); ++i)
192 upgrade_draw(&upgrades[i]);
194 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
195 bouncy_distros[i].draw();
197 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
198 broken_bricks[i].draw();
200 /* Draw foreground: */
201 for (y = 0; y < 15; ++y)
203 for (x = 0; x < 21; ++x)
205 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
206 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
210 /* Draw particle systems (foreground) */
211 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
213 (*p)->draw(scroll_x, 0, 1);
220 /* Handle bouncy distros: */
221 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
222 bouncy_distros[i].action();
224 /* Handle broken bricks: */
225 for (unsigned int i = 0; i < broken_bricks.size(); i++)
226 broken_bricks[i].action();
228 /* Handle distro counting: */
229 if (counting_distros)
233 if (distro_counter <= 0)
234 counting_distros = -1;
237 // Handle all kinds of game objects
238 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
239 bouncy_bricks[i].action();
241 for (unsigned int i = 0; i < floating_scores.size(); i++)
242 floating_scores[i].action();
244 for (unsigned int i = 0; i < bullets.size(); ++i)
245 bullet_action(&bullets[i]);
247 for (unsigned int i = 0; i < upgrades.size(); i++)
248 upgrade_action(&upgrades[i]);
250 for (unsigned int i = 0; i < bad_guys.size(); i++)
251 bad_guys[i].action();
253 /* update particle systems */
254 std::vector<ParticleSystem*>::iterator p;
255 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
257 (*p)->simulate(frame_ratio);
260 /* Handle all possible collisions. */
266 World::collision_handler()
268 // CO_BULLET & CO_BADGUY check
269 for(unsigned int i = 0; i < bullets.size(); ++i)
271 for(unsigned int j = 0; j < bad_guys.size(); ++j)
273 if(bad_guys[j].dying != DYING_NOT)
275 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
277 // We have detected a collision and now call the
278 // collision functions of the collided objects.
279 // collide with bad_guy first, since bullet_collision will
281 bad_guys[j].collision(0, CO_BULLET);
282 bullet_collision(&bullets[i], CO_BADGUY);
283 break; // bullet is invalid now, so break
288 /* CO_BADGUY & CO_BADGUY check */
289 for(unsigned int i = 0; i < bad_guys.size(); ++i)
291 if(bad_guys[i].dying != DYING_NOT)
294 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
296 if(j == i || bad_guys[j].dying != DYING_NOT)
299 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
301 // We have detected a collision and now call the
302 // collision functions of the collided objects.
303 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
304 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
309 if(tux.dying != DYING_NOT) return;
311 // CO_BADGUY & CO_PLAYER check
312 for(unsigned int i = 0; i < bad_guys.size(); ++i)
314 if(bad_guys[i].dying != DYING_NOT)
317 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
319 // We have detected a collision and now call the collision
320 // functions of the collided objects.
321 if (tux.previous_base.y < tux.base.y &&
322 tux.previous_base.y + tux.previous_base.height
323 < bad_guys[i].base.y + bad_guys[i].base.height/2)
325 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
329 tux.collision(&bad_guys[i], CO_BADGUY);
334 // CO_UPGRADE & CO_PLAYER check
335 for(unsigned int i = 0; i < upgrades.size(); ++i)
337 if(rectcollision(&upgrades[i].base, &tux.base))
339 // We have detected a collision and now call the collision
340 // functions of the collided objects.
341 upgrade_collision(&upgrades[i], &tux, CO_PLAYER);
347 World::add_score(float x, float y, int s)
351 FloatingScore new_floating_score;
352 new_floating_score.init(x,y,s);
353 floating_scores.push_back(new_floating_score);
357 World::add_bouncy_distro(float x, float y)
359 BouncyDistro new_bouncy_distro;
360 new_bouncy_distro.init(x,y);
361 bouncy_distros.push_back(new_bouncy_distro);
365 World::add_broken_brick(Tile* tile, float x, float y)
367 add_broken_brick_piece(tile, x, y, -1, -4);
368 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
370 add_broken_brick_piece(tile, x + 16, y, 1, -4);
371 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
375 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
377 BrokenBrick new_broken_brick;
378 new_broken_brick.init(tile, x, y, xm, ym);
379 broken_bricks.push_back(new_broken_brick);
383 World::add_bouncy_brick(float x, float y)
385 BouncyBrick new_bouncy_brick;
386 new_bouncy_brick.init(x,y);
387 bouncy_bricks.push_back(new_bouncy_brick);
391 World::add_bad_guy(float x, float y, BadGuyKind kind)
393 bad_guys.push_back(BadGuy());
394 BadGuy& new_bad_guy = bad_guys.back();
396 new_bad_guy.init(x,y,kind);
400 World::add_upgrade(float x, float y, int dir, int kind)
402 upgrade_type new_upgrade;
403 upgrade_init(&new_upgrade,x,y,dir,kind);
404 upgrades.push_back(new_upgrade);
408 World::add_bullet(float x, float y, float xm, int dir)
410 bullet_type new_bullet;
411 bullet_init(&new_bullet,x,y,xm,dir);
412 bullets.push_back(new_bullet);
414 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
419 World::trybreakbrick(float x, float y, bool small)
421 Level* plevel = get_level();
423 Tile* tile = gettile(x, y);
428 /* Get a distro from it: */
429 add_bouncy_distro(((int)(x + 1) / 32) * 32,
432 if (!counting_distros)
434 counting_distros = true;
438 if (distro_counter <= 0)
439 plevel->change(x, y, TM_IA, tile->next_tile);
441 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
442 score = score + SCORE_DISTRO;
448 plevel->change(x, y, TM_IA, tile->next_tile);
450 /* Replace it with broken bits: */
451 add_broken_brick(tile,
452 ((int)(x + 1) / 32) * 32,
455 /* Get some score: */
456 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
457 score = score + SCORE_BRICK;
464 World::tryemptybox(float x, float y, int col_side)
466 Tile* tile = gettile(x,y);
470 // according to the collision side, set the upgrade direction
478 case 1: // Box with a distro!
479 add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
480 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
481 score = score + SCORE_DISTRO;
485 case 2: // Add an upgrade!
486 if (tux.size == SMALL) /* Tux is small, add mints! */
487 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
488 else /* Tux is big, add coffee: */
489 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
490 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
493 case 3: // Add a golden herring
494 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
501 level->change(x, y, TM_IA, tile->next_tile);
504 /* Try to grab a distro: */
506 World::trygrabdistro(float x, float y, int bounciness)
508 Tile* tile = gettile(x, y);
509 if (tile && tile->distro)
511 level->change(x, y, TM_IA, tile->next_tile);
512 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
514 if (bounciness == BOUNCE)
516 add_bouncy_distro(((int)(x + 1) / 32) * 32,
520 score = score + SCORE_DISTRO;
525 /* Try to bump a bad guy from below: */
527 World::trybumpbadguy(float x, float y)
530 for (unsigned int i = 0; i < bad_guys.size(); i++)
532 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
533 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
535 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
541 for (unsigned int i = 0; i < upgrades.size(); i++)
543 if (upgrades[i].base.height == 32 &&
544 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
545 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
547 upgrades[i].base.xm = -upgrades[i].base.xm;
548 upgrades[i].base.ym = -8;
549 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);