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();
255 World::add_score(float x, float y, int s)
259 floating_score_type new_floating_score;
260 new_floating_score.init(x,y,s);
261 floating_scores.push_back(new_floating_score);
265 World::add_bouncy_distro(float x, float y)
267 bouncy_distro_type new_bouncy_distro;
268 new_bouncy_distro.init(x,y);
269 bouncy_distros.push_back(new_bouncy_distro);
273 World::add_broken_brick(Tile* tile, float x, float y)
275 add_broken_brick_piece(tile, x, y, -1, -4);
276 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
278 add_broken_brick_piece(tile, x + 16, y, 1, -4);
279 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
283 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
285 broken_brick_type new_broken_brick;
286 new_broken_brick.init(tile, x, y, xm, ym);
287 broken_bricks.push_back(new_broken_brick);
291 World::add_bouncy_brick(float x, float y)
293 bouncy_brick_type new_bouncy_brick;
294 new_bouncy_brick.init(x,y);
295 bouncy_bricks.push_back(new_bouncy_brick);
299 World::add_bad_guy(float x, float y, BadGuyKind kind)
301 bad_guys.push_back(BadGuy());
302 BadGuy& new_bad_guy = bad_guys.back();
304 new_bad_guy.init(x,y,kind);
308 World::add_upgrade(float x, float y, int dir, int kind)
310 upgrade_type new_upgrade;
311 upgrade_init(&new_upgrade,x,y,dir,kind);
312 upgrades.push_back(new_upgrade);
316 World::add_bullet(float x, float y, float xm, int dir)
318 bullet_type new_bullet;
319 bullet_init(&new_bullet,x,y,xm,dir);
320 bullets.push_back(new_bullet);
322 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
327 World::trybreakbrick(float x, float y, bool small)
329 Level* plevel = get_level();
331 Tile* tile = gettile(x, y);
336 /* Get a distro from it: */
337 add_bouncy_distro(((int)(x + 1) / 32) * 32,
340 if (!counting_distros)
342 counting_distros = true;
346 if (distro_counter <= 0)
347 plevel->change(x, y, TM_IA, tile->next_tile);
349 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
350 score = score + SCORE_DISTRO;
356 plevel->change(x, y, TM_IA, tile->next_tile);
358 /* Replace it with broken bits: */
359 add_broken_brick(tile,
360 ((int)(x + 1) / 32) * 32,
363 /* Get some score: */
364 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
365 score = score + SCORE_BRICK;
372 World::tryemptybox(float x, float y, int col_side)
374 Tile* tile = gettile(x,y);
378 // according to the collision side, set the upgrade direction
386 case 1: // Box with a distro!
387 add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
388 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
389 score = score + SCORE_DISTRO;
393 case 2: // Add an upgrade!
394 if (tux.size == SMALL) /* Tux is small, add mints! */
395 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
396 else /* Tux is big, add coffee: */
397 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
398 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
401 case 3: // Add a golden herring
402 add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
409 level->change(x, y, TM_IA, tile->next_tile);
412 /* Try to grab a distro: */
414 World::trygrabdistro(float x, float y, int bounciness)
416 Tile* tile = gettile(x, y);
417 if (tile && tile->distro)
419 level->change(x, y, TM_IA, tile->next_tile);
420 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
422 if (bounciness == BOUNCE)
424 add_bouncy_distro(((int)(x + 1) / 32) * 32,
428 score = score + SCORE_DISTRO;
433 /* Try to bump a bad guy from below: */
435 World::trybumpbadguy(float x, float y)
438 for (unsigned int i = 0; i < bad_guys.size(); i++)
440 if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
441 bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
443 bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
449 for (unsigned int i = 0; i < upgrades.size(); i++)
451 if (upgrades[i].base.height == 32 &&
452 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
453 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
455 upgrades[i].base.xm = -upgrades[i].base.xm;
456 upgrades[i].base.ym = -8;
457 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);