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 texture_type img_distro[4];
34 World* World::current_ = 0;
38 // FIXME: Move this to action and draw and everywhere else where the
39 // world calls child functions
57 player_status.score_multiplier = 1;
59 counting_distros = false;
62 /* set current song/music */
63 set_current_music(LEVEL_MUSIC);
67 World::load(const std::string& subset, int level_nr)
69 return level->load(subset, level_nr);
73 World::load(const std::string& filename)
75 return level->load(filename);
79 World::arrays_free(void)
82 bouncy_distros.clear();
83 broken_bricks.clear();
84 bouncy_bricks.clear();
85 floating_scores.clear();
88 std::vector<ParticleSystem*>::iterator i;
89 for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
92 particle_systems.clear();
96 World::activate_bad_guys()
98 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
99 i != level->badguy_data.end();
102 add_bad_guy(i->x, i->y, i->kind);
107 World::activate_particle_systems()
109 if (level->particle_system == "clouds")
111 particle_systems.push_back(new CloudParticleSystem);
113 else if (level->particle_system == "snow")
115 particle_systems.push_back(new SnowParticleSystem);
117 else if (level->particle_system != "")
119 st_abort("unknown particle system specified in level", "");
128 /* Draw the real background */
129 if(get_level()->bkgd_image[0] != '\0')
131 int s = (int)scroll_x / 30;
132 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
133 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
137 clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
140 /* Draw particle systems (background) */
141 std::vector<ParticleSystem*>::iterator p;
142 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
144 (*p)->draw(scroll_x, 0, 0);
147 /* Draw background: */
148 for (y = 0; y < 15; ++y)
150 for (x = 0; x < 21; ++x)
152 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
153 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
157 /* Draw interactive tiles: */
158 for (y = 0; y < 15; ++y)
160 for (x = 0; x < 21; ++x)
162 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
163 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
167 /* (Bouncy bricks): */
168 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
169 bouncy_bricks[i].draw();
171 for (unsigned int i = 0; i < bad_guys.size(); ++i)
176 for (unsigned int i = 0; i < bullets.size(); ++i)
179 for (unsigned int i = 0; i < floating_scores.size(); ++i)
180 floating_scores[i].draw();
182 for (unsigned int i = 0; i < upgrades.size(); ++i)
185 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
186 bouncy_distros[i].draw();
188 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
189 broken_bricks[i].draw();
191 /* Draw foreground: */
192 for (y = 0; y < 15; ++y)
194 for (x = 0; x < 21; ++x)
196 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
197 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
201 /* Draw particle systems (foreground) */
202 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
204 (*p)->draw(scroll_x, 0, 1);
209 World::action(double 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);
325 // CO_UPGRADE & CO_PLAYER check
326 for(unsigned int i = 0; i < upgrades.size(); ++i)
328 if(rectcollision(&upgrades[i].base, &tux.base))
330 // We have detected a collision and now call the collision
331 // functions of the collided objects.
332 upgrades[i].collision(&tux, CO_PLAYER);
338 World::add_score(float x, float y, int s)
340 player_status.score += s;
342 FloatingScore new_floating_score;
343 new_floating_score.init(x,y,s);
344 floating_scores.push_back(new_floating_score);
348 World::add_bouncy_distro(float x, float y)
350 BouncyDistro new_bouncy_distro;
351 new_bouncy_distro.init(x,y);
352 bouncy_distros.push_back(new_bouncy_distro);
356 World::add_broken_brick(Tile* tile, float x, float y)
358 add_broken_brick_piece(tile, x, y, -1, -4);
359 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
361 add_broken_brick_piece(tile, x + 16, y, 1, -4);
362 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
366 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
368 BrokenBrick new_broken_brick;
369 new_broken_brick.init(tile, x, y, xm, ym);
370 broken_bricks.push_back(new_broken_brick);
374 World::add_bouncy_brick(float x, float y)
376 BouncyBrick new_bouncy_brick;
377 new_bouncy_brick.init(x,y);
378 bouncy_bricks.push_back(new_bouncy_brick);
382 World::add_bad_guy(float x, float y, BadGuyKind kind)
384 bad_guys.push_back(BadGuy());
385 BadGuy& new_bad_guy = bad_guys.back();
387 new_bad_guy.init(x,y,kind);
391 World::add_upgrade(float x, float y, int dir, int kind)
394 new_upgrade.init(x,y,dir,kind);
395 upgrades.push_back(new_upgrade);
399 World::add_bullet(float x, float y, float xm, int dir)
402 new_bullet.init(x,y,xm,dir);
403 bullets.push_back(new_bullet);
405 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
410 World::trybreakbrick(float x, float y, bool small)
412 Level* plevel = get_level();
414 Tile* tile = gettile(x, y);
419 /* Get a distro from it: */
420 add_bouncy_distro(((int)(x + 1) / 32) * 32,
423 if (!counting_distros)
425 counting_distros = true;
429 if (distro_counter <= 0)
430 plevel->change(x, y, TM_IA, tile->next_tile);
432 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
433 player_status.score = player_status.score + SCORE_DISTRO;
434 player_status.distros++;
439 plevel->change(x, y, TM_IA, tile->next_tile);
441 /* Replace it with broken bits: */
442 add_broken_brick(tile,
443 ((int)(x + 1) / 32) * 32,
446 /* Get some score: */
447 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
448 player_status.score = player_status.score + SCORE_BRICK;
455 World::tryemptybox(float x, float y, int col_side)
457 Tile* tile = gettile(x,y);
461 // according to the collision side, set the upgrade direction
469 case 1: // Box with a distro!
470 add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
471 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
472 player_status.score = player_status.score + SCORE_DISTRO;
473 player_status.distros++;
476 case 2: // Add an upgrade!
477 if (tux.size == SMALL) /* Tux is small, add mints! */
478 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
479 else /* Tux is big, add coffee: */
480 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
481 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
484 case 3: // Add a golden herring
485 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
492 level->change(x, y, TM_IA, tile->next_tile);
495 /* Try to grab a distro: */
497 World::trygrabdistro(float x, float y, int bounciness)
499 Tile* tile = gettile(x, y);
500 if (tile && tile->distro)
502 level->change(x, y, TM_IA, tile->next_tile);
503 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
505 if (bounciness == BOUNCE)
507 add_bouncy_distro(((int)(x + 1) / 32) * 32,
511 player_status.score = player_status.score + SCORE_DISTRO;
512 player_status.distros++;
516 /* Try to bump a bad guy from below: */
518 World::trybumpbadguy(float x, float y)
521 for (unsigned int i = 0; i < bad_guys.size(); i++)
523 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
524 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
526 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
532 for (unsigned int i = 0; i < upgrades.size(); i++)
534 if (upgrades[i].base.height == 32 &&
535 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
536 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
538 upgrades[i].base.xm = -upgrades[i].base.xm;
539 upgrades[i].base.ym = -8;
540 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);