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;
40 // FIXME: Move this to action and draw and everywhere else where the
41 // world calls child functions
59 player_status.score_multiplier = 1;
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", "");
130 /* Draw the real background */
131 if(get_level()->bkgd_image[0] != '\0')
133 int s = (int)scroll_x / 30;
134 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
135 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
139 clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
142 /* Draw particle systems (background) */
143 std::vector<ParticleSystem*>::iterator p;
144 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
146 (*p)->draw(scroll_x, 0, 0);
149 /* Draw background: */
150 for (y = 0; y < 15; ++y)
152 for (x = 0; x < 21; ++x)
154 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
155 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
159 /* Draw interactive tiles: */
160 for (y = 0; y < 15; ++y)
162 for (x = 0; x < 21; ++x)
164 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
165 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
169 /* (Bouncy bricks): */
170 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
171 bouncy_bricks[i].draw();
173 for (unsigned int i = 0; i < bad_guys.size(); ++i)
178 for (unsigned int i = 0; i < bullets.size(); ++i)
181 for (unsigned int i = 0; i < floating_scores.size(); ++i)
182 floating_scores[i].draw();
184 for (unsigned int i = 0; i < upgrades.size(); ++i)
187 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
188 bouncy_distros[i].draw();
190 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
191 broken_bricks[i].draw();
193 /* Draw foreground: */
194 for (y = 0; y < 15; ++y)
196 for (x = 0; x < 21; ++x)
198 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
199 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
203 /* Draw particle systems (foreground) */
204 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
206 (*p)->draw(scroll_x, 0, 1);
211 World::action(double frame_ratio)
213 /* Handle bouncy distros: */
214 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
215 bouncy_distros[i].action(frame_ratio);
217 /* Handle broken bricks: */
218 for (unsigned int i = 0; i < broken_bricks.size(); i++)
219 broken_bricks[i].action(frame_ratio);
221 /* Handle distro counting: */
222 if (counting_distros)
226 if (distro_counter <= 0)
227 counting_distros = -1;
230 // Handle all kinds of game objects
231 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
232 bouncy_bricks[i].action(frame_ratio);
234 for (unsigned int i = 0; i < floating_scores.size(); i++)
235 floating_scores[i].action(frame_ratio);
237 for (unsigned int i = 0; i < bullets.size(); ++i)
238 bullets[i].action(frame_ratio);
240 for (unsigned int i = 0; i < upgrades.size(); i++)
241 upgrades[i].action(frame_ratio);
243 for (unsigned int i = 0; i < bad_guys.size(); i++)
244 bad_guys[i].action(frame_ratio);
246 /* update particle systems */
247 std::vector<ParticleSystem*>::iterator p;
248 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
250 (*p)->simulate(frame_ratio);
253 /* Handle all possible collisions. */
259 World::collision_handler()
261 // CO_BULLET & CO_BADGUY check
262 for(unsigned int i = 0; i < bullets.size(); ++i)
264 for(unsigned int j = 0; j < bad_guys.size(); ++j)
266 if(bad_guys[j].dying != DYING_NOT)
268 if(rectcollision(&bullets[i].base, &bad_guys[j].base))
270 // We have detected a collision and now call the
271 // collision functions of the collided objects.
272 // collide with bad_guy first, since bullet_collision will
274 bad_guys[j].collision(0, CO_BULLET);
275 bullets[i].collision(CO_BADGUY);
276 break; // bullet is invalid now, so break
281 /* CO_BADGUY & CO_BADGUY check */
282 for(unsigned int i = 0; i < bad_guys.size(); ++i)
284 if(bad_guys[i].dying != DYING_NOT)
287 for(unsigned int j = i+1; j < bad_guys.size(); ++j)
289 if(j == i || bad_guys[j].dying != DYING_NOT)
292 if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
294 // We have detected a collision and now call the
295 // collision functions of the collided objects.
296 bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
297 bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
302 if(tux.dying != DYING_NOT) return;
304 // CO_BADGUY & CO_PLAYER check
305 for(unsigned int i = 0; i < bad_guys.size(); ++i)
307 if(bad_guys[i].dying != DYING_NOT)
310 if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
312 // We have detected a collision and now call the collision
313 // functions of the collided objects.
314 if (tux.previous_base.y < tux.base.y &&
315 tux.previous_base.y + tux.previous_base.height
316 < bad_guys[i].base.y + bad_guys[i].base.height/2)
318 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
322 tux.collision(&bad_guys[i], CO_BADGUY);
327 // CO_UPGRADE & CO_PLAYER check
328 for(unsigned int i = 0; i < upgrades.size(); ++i)
330 if(rectcollision(&upgrades[i].base, &tux.base))
332 // We have detected a collision and now call the collision
333 // functions of the collided objects.
334 upgrades[i].collision(&tux, CO_PLAYER);
340 World::add_score(float x, float y, int s)
342 player_status.score += s;
344 FloatingScore new_floating_score;
345 new_floating_score.init(x,y,s);
346 floating_scores.push_back(new_floating_score);
350 World::add_bouncy_distro(float x, float y)
352 BouncyDistro new_bouncy_distro;
353 new_bouncy_distro.init(x,y);
354 bouncy_distros.push_back(new_bouncy_distro);
358 World::add_broken_brick(Tile* tile, float x, float y)
360 add_broken_brick_piece(tile, x, y, -1, -4);
361 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
363 add_broken_brick_piece(tile, x + 16, y, 1, -4);
364 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
368 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
370 BrokenBrick new_broken_brick;
371 new_broken_brick.init(tile, x, y, xm, ym);
372 broken_bricks.push_back(new_broken_brick);
376 World::add_bouncy_brick(float x, float y)
378 BouncyBrick new_bouncy_brick;
379 new_bouncy_brick.init(x,y);
380 bouncy_bricks.push_back(new_bouncy_brick);
384 World::add_bad_guy(float x, float y, BadGuyKind kind)
386 bad_guys.push_back(BadGuy());
387 BadGuy& new_bad_guy = bad_guys.back();
389 new_bad_guy.init(x,y,kind);
393 World::add_upgrade(float x, float y, int dir, int kind)
396 new_upgrade.init(x,y,dir,kind);
397 upgrades.push_back(new_upgrade);
401 World::add_bullet(float x, float y, float xm, int dir)
404 new_bullet.init(x,y,xm,dir);
405 bullets.push_back(new_bullet);
407 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
412 World::trybreakbrick(float x, float y, bool small)
414 Level* plevel = get_level();
416 Tile* tile = gettile(x, y);
421 /* Get a distro from it: */
422 add_bouncy_distro(((int)(x + 1) / 32) * 32,
425 if (!counting_distros)
427 counting_distros = true;
431 if (distro_counter <= 0)
432 plevel->change(x, y, TM_IA, tile->next_tile);
434 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
435 player_status.score = player_status.score + SCORE_DISTRO;
436 player_status.distros++;
441 plevel->change(x, y, TM_IA, tile->next_tile);
443 /* Replace it with broken bits: */
444 add_broken_brick(tile,
445 ((int)(x + 1) / 32) * 32,
448 /* Get some score: */
449 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
450 player_status.score = player_status.score + SCORE_BRICK;
457 World::tryemptybox(float x, float y, int col_side)
459 Tile* tile = gettile(x,y);
463 // according to the collision side, set the upgrade direction
471 case 1: // Box with a distro!
472 add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
473 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
474 player_status.score = player_status.score + SCORE_DISTRO;
475 player_status.distros++;
478 case 2: // Add an upgrade!
479 if (tux.size == SMALL) /* Tux is small, add mints! */
480 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
481 else /* Tux is big, add coffee: */
482 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
483 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
486 case 3: // Add a golden herring
487 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
494 level->change(x, y, TM_IA, tile->next_tile);
497 /* Try to grab a distro: */
499 World::trygrabdistro(float x, float y, int bounciness)
501 Tile* tile = gettile(x, y);
502 if (tile && tile->distro)
504 level->change(x, y, TM_IA, tile->next_tile);
505 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
507 if (bounciness == BOUNCE)
509 add_bouncy_distro(((int)(x + 1) / 32) * 32,
513 player_status.score = player_status.score + SCORE_DISTRO;
514 player_status.distros++;
518 /* Try to bump a bad guy from below: */
520 World::trybumpbadguy(float x, float y)
523 for (unsigned int i = 0; i < bad_guys.size(); i++)
525 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
526 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
528 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
534 for (unsigned int i = 0; i < upgrades.size(); i++)
536 if (upgrades[i].base.height == 32 &&
537 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
538 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
540 upgrades[i].base.xm = -upgrades[i].base.xm;
541 upgrades[i].base.ym = -8;
542 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);