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;
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 level->img_bkgd->draw_part(s, 0,0,0,level->img_bkgd->w - s, level->img_bkgd->h);
133 level->img_bkgd->draw_part(0, 0,screen->w - s ,0,s,level->img_bkgd->h);
137 drawgradient(level->bkgd_top_red, level->bkgd_top_green, level->bkgd_top_blue,
138 level->bkgd_bottom_red, level->bkgd_bottom_green, level->bkgd_bottom_blue);
141 /* Draw particle systems (background) */
142 std::vector<ParticleSystem*>::iterator p;
143 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
145 (*p)->draw(scroll_x, 0, 0);
148 /* Draw background: */
149 for (y = 0; y < 15; ++y)
151 for (x = 0; x < 21; ++x)
153 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
154 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
158 /* Draw interactive tiles: */
159 for (y = 0; y < 15; ++y)
161 for (x = 0; x < 21; ++x)
163 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
164 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
168 /* (Bouncy bricks): */
169 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
170 bouncy_bricks[i].draw();
172 for (unsigned int i = 0; i < bad_guys.size(); ++i)
177 for (unsigned int i = 0; i < bullets.size(); ++i)
180 for (unsigned int i = 0; i < floating_scores.size(); ++i)
181 floating_scores[i].draw();
183 for (unsigned int i = 0; i < upgrades.size(); ++i)
186 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
187 bouncy_distros[i].draw();
189 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
190 broken_bricks[i].draw();
192 /* Draw foreground: */
193 for (y = 0; y < 15; ++y)
195 for (x = 0; x < 21; ++x)
197 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
198 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
202 /* Draw particle systems (foreground) */
203 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
205 (*p)->draw(scroll_x, 0, 1);
210 World::action(double frame_ratio)
212 /* Handle bouncy distros: */
213 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
214 bouncy_distros[i].action(frame_ratio);
216 /* Handle broken bricks: */
217 for (unsigned int i = 0; i < broken_bricks.size(); i++)
218 broken_bricks[i].action(frame_ratio);
220 /* Handle distro counting: */
221 if (counting_distros)
225 if (distro_counter <= 0)
226 counting_distros = -1;
229 // Handle all kinds of game objects
230 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
231 bouncy_bricks[i].action(frame_ratio);
233 for (unsigned int i = 0; i < floating_scores.size(); i++)
234 floating_scores[i].action(frame_ratio);
236 for (unsigned int i = 0; i < bullets.size(); ++i)
237 bullets[i].action(frame_ratio);
239 for (unsigned int i = 0; i < upgrades.size(); i++)
240 upgrades[i].action(frame_ratio);
242 for (unsigned int i = 0; i < bad_guys.size(); i++)
243 bad_guys[i].action(frame_ratio);
245 /* update particle systems */
246 std::vector<ParticleSystem*>::iterator p;
247 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
249 (*p)->simulate(frame_ratio);
252 /* Handle all possible collisions. */
258 World::collision_handler()
260 // CO_BULLET & CO_BADGUY check
261 for(unsigned int i = 0; i < bullets.size(); ++i)
263 for(unsigned int j = 0; j < bad_guys.size(); ++j)
265 if(bad_guys[j].dying != DYING_NOT)
267 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
269 // We have detected a collision and now call the
270 // collision functions of the collided objects.
271 // collide with bad_guy first, since bullet_collision will
273 bad_guys[j].collision(0, CO_BULLET);
274 bullets[i].collision(CO_BADGUY);
275 break; // bullet is invalid now, so break
280 /* CO_BADGUY & CO_BADGUY check */
281 for(unsigned int i = 0; i < bad_guys.size(); ++i)
283 if(bad_guys[i].dying != DYING_NOT)
286 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
288 if(j == i || bad_guys[j].dying != DYING_NOT)
291 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
293 // We have detected a collision and now call the
294 // collision functions of the collided objects.
295 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
296 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
301 if(tux.dying != DYING_NOT) return;
303 // CO_BADGUY & CO_PLAYER check
304 for(unsigned int i = 0; i < bad_guys.size(); ++i)
306 if(bad_guys[i].dying != DYING_NOT)
309 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
311 // We have detected a collision and now call the collision
312 // functions of the collided objects.
313 if (tux.previous_base.y < tux.base.y &&
314 tux.previous_base.y + tux.previous_base.height
315 < bad_guys[i].base.y + bad_guys[i].base.height/2)
317 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
321 tux.collision(&bad_guys[i], CO_BADGUY);
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)
385 bad_guys.push_back(BadGuy());
386 BadGuy& new_bad_guy = bad_guys.back();
388 new_bad_guy.init(x,y,kind);
392 World::add_upgrade(float x, float y, int dir, int 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, int 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, int 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);