2 // C Implementation: world
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
24 texture_type img_distro[4];
26 World* World::current_ = 0;
32 // FIXME: Move this to action and draw and everywhere else where the
33 // world calls child functions
51 timer_init(&super_bkgd_timer, true);
53 counting_distros = false;
58 /* set current song/music */
59 set_current_music(LEVEL_MUSIC);
63 World::load(const char* subset, int level_nr)
65 return level->load(subset, level_nr);
69 World::load(const std::string& filename)
71 return level->load(filename);
75 World::arrays_free(void)
78 bouncy_distros.clear();
79 broken_bricks.clear();
80 bouncy_bricks.clear();
81 floating_scores.clear();
84 std::vector<ParticleSystem*>::iterator i;
85 for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
88 particle_systems.clear();
92 World::activate_bad_guys()
94 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
95 i != level->badguy_data.end();
98 add_bad_guy(i->x, i->y, i->kind);
103 World::activate_particle_systems()
105 if (level->particle_system == "clouds")
107 particle_systems.push_back(new CloudParticleSystem);
109 else if (level->particle_system == "snow")
111 particle_systems.push_back(new SnowParticleSystem);
113 else if (level->particle_system != "")
115 st_abort("unknown particle system specified in level", "");
125 if(timer_check(&super_bkgd_timer))
126 texture_draw(&img_super_bkgd, 0, 0);
129 /* Draw the real background */
130 if(get_level()->bkgd_image[0] != '\0')
132 int s = (int)scroll_x / 30;
133 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
134 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
138 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_brick_draw(&bouncy_bricks[i]);
173 for (unsigned int i = 0; i < bad_guys.size(); ++i)
178 for (unsigned int i = 0; i < bullets.size(); ++i)
179 bullet_draw(&bullets[i]);
181 for (unsigned int i = 0; i < floating_scores.size(); ++i)
182 floating_score_draw(&floating_scores[i]);
184 for (unsigned int i = 0; i < upgrades.size(); ++i)
185 upgrade_draw(&upgrades[i]);
187 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
188 bouncy_distro_draw(&bouncy_distros[i]);
190 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
191 broken_brick_draw(&broken_bricks[i]);
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);
213 /* Handle bouncy distros: */
214 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
215 bouncy_distro_action(&bouncy_distros[i]);
217 /* Handle broken bricks: */
218 for (unsigned int i = 0; i < broken_bricks.size(); i++)
219 broken_brick_action(&broken_bricks[i]);
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_brick_action(&bouncy_bricks[i]);
234 for (unsigned int i = 0; i < floating_scores.size(); i++)
235 floating_score_action(&floating_scores[i]);
237 for (unsigned int i = 0; i < bullets.size(); ++i)
238 bullet_action(&bullets[i]);
240 for (unsigned int i = 0; i < upgrades.size(); i++)
241 upgrade_action(&upgrades[i]);
243 for (unsigned int i = 0; i < bad_guys.size(); i++)
244 bad_guys[i].action();
248 World::add_score(float x, float y, int s)
252 floating_score_type new_floating_score;
253 floating_score_init(&new_floating_score,x,y,s);
254 floating_scores.push_back(new_floating_score);
258 World::add_bouncy_distro(float x, float y)
260 bouncy_distro_type new_bouncy_distro;
261 bouncy_distro_init(&new_bouncy_distro,x,y);
262 bouncy_distros.push_back(new_bouncy_distro);
266 World::add_broken_brick(Tile* tile, float x, float y)
268 add_broken_brick_piece(tile, x, y, -1, -4);
269 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
271 add_broken_brick_piece(tile, x + 16, y, 1, -4);
272 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
276 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
278 broken_brick_type new_broken_brick;
279 broken_brick_init(&new_broken_brick, tile, x, y, xm, ym);
280 broken_bricks.push_back(new_broken_brick);
284 World::add_bouncy_brick(float x, float y)
286 bouncy_brick_type new_bouncy_brick;
287 bouncy_brick_init(&new_bouncy_brick,x,y);
288 bouncy_bricks.push_back(new_bouncy_brick);
292 World::add_bad_guy(float x, float y, BadGuyKind kind)
294 bad_guys.push_back(BadGuy());
295 BadGuy& new_bad_guy = bad_guys.back();
297 new_bad_guy.init(x,y,kind);
301 World::add_upgrade(float x, float y, int dir, int kind)
303 upgrade_type new_upgrade;
304 upgrade_init(&new_upgrade,x,y,dir,kind);
305 upgrades.push_back(new_upgrade);
309 World::add_bullet(float x, float y, float xm, int dir)
311 bullet_type new_bullet;
312 bullet_init(&new_bullet,x,y,xm,dir);
313 bullets.push_back(new_bullet);
315 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
319 void trybreakbrick(float x, float y, bool small)
321 Level* plevel = World::current()->get_level();
323 Tile* tile = gettile(x, y);
328 /* Get a distro from it: */
329 world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
332 if (!counting_distros)
334 counting_distros = true;
338 if (distro_counter <= 0)
339 plevel->change(x, y, TM_IA, tile->next_tile);
341 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
342 score = score + SCORE_DISTRO;
348 plevel->change(x, y, TM_IA, tile->next_tile);
350 /* Replace it with broken bits: */
351 world.add_broken_brick(tile,
352 ((int)(x + 1) / 32) * 32,
355 /* Get some score: */
356 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
357 score = score + SCORE_BRICK;
363 void tryemptybox(float x, float y, int col_side)
365 Level* plevel = World::current()->get_level();
367 Tile* tile = gettile(x,y);
371 // according to the collision side, set the upgrade direction
379 case 1: //'A': /* Box with a distro! */
380 world.add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
381 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
382 score = score + SCORE_DISTRO;
386 case 2: // 'B': /* Add an upgrade! */
387 if (tux.size == SMALL) /* Tux is small, add mints! */
388 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
389 else /* Tux is big, add coffee: */
390 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
391 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
394 case 3:// '!': /* Add a golden herring */
395 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
402 plevel->change(x, y, TM_IA, tile->next_tile);
405 /* Try to grab a distro: */
406 void trygrabdistro(float x, float y, int bounciness)
408 Level* plevel = World::current()->get_level();
409 Tile* tile = gettile(x, y);
410 if (tile && tile->distro)
412 plevel->change(x, y, TM_IA, tile->next_tile);
413 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
415 if (bounciness == BOUNCE)
417 world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
421 score = score + SCORE_DISTRO;
426 /* Try to bump a bad guy from below: */
427 void trybumpbadguy(float x, float y)
430 for (unsigned int i = 0; i < world.bad_guys.size(); i++)
432 if (world.bad_guys[i].base.x >= x - 32 && world.bad_guys[i].base.x <= x + 32 &&
433 world.bad_guys[i].base.y >= y - 16 && world.bad_guys[i].base.y <= y + 16)
435 world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
441 for (unsigned int i = 0; i < world.upgrades.size(); i++)
443 if (world.upgrades[i].base.height == 32 &&
444 world.upgrades[i].base.x >= x - 32 && world.upgrades[i].base.x <= x + 32 &&
445 world.upgrades[i].base.y >= y - 16 && world.upgrades[i].base.y <= y + 16)
447 world.upgrades[i].base.xm = -world.upgrades[i].base.xm;
448 world.upgrades[i].base.ym = -8;
449 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);