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];
39 World::load(const char* subset, int level_nr)
41 return level->load(subset, level_nr);
45 World::load(const std::string& filename)
47 return level->load(filename);
51 World::arrays_free(void)
54 bouncy_distros.clear();
55 broken_bricks.clear();
56 bouncy_bricks.clear();
57 floating_scores.clear();
60 std::vector<ParticleSystem*>::iterator i;
61 for(i = particle_systems.begin(); i != particle_systems.end(); ++i) {
64 particle_systems.clear();
69 World::activate_particle_systems()
71 if (level->particle_system == "clouds")
73 particle_systems.push_back(new CloudParticleSystem);
75 else if (level->particle_system == "snow")
77 particle_systems.push_back(new SnowParticleSystem);
79 else if (level->particle_system != "")
81 st_abort("unknown particle system specified in level", "");
91 if(timer_check(&super_bkgd_timer))
92 texture_draw(&img_super_bkgd, 0, 0);
95 /* Draw the real background */
96 if(get_level()->bkgd_image[0] != '\0')
98 int s = (int)scroll_x / 30;
99 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
100 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
104 clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
108 /* Draw particle systems (background) */
109 std::vector<ParticleSystem*>::iterator p;
110 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
112 (*p)->draw(scroll_x, 0, 0);
115 /* Draw background: */
116 for (y = 0; y < 15; ++y)
118 for (x = 0; x < 21; ++x)
120 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
121 level->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
125 /* Draw interactive tiles: */
126 for (y = 0; y < 15; ++y)
128 for (x = 0; x < 21; ++x)
130 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
131 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
135 /* (Bouncy bricks): */
136 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
137 bouncy_brick_draw(&bouncy_bricks[i]);
139 for (unsigned int i = 0; i < bad_guys.size(); ++i)
144 for (unsigned int i = 0; i < bullets.size(); ++i)
145 bullet_draw(&bullets[i]);
147 for (unsigned int i = 0; i < floating_scores.size(); ++i)
148 floating_score_draw(&floating_scores[i]);
150 for (unsigned int i = 0; i < upgrades.size(); ++i)
151 upgrade_draw(&upgrades[i]);
153 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
154 bouncy_distro_draw(&bouncy_distros[i]);
156 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
157 broken_brick_draw(&broken_bricks[i]);
159 /* Draw foreground: */
160 for (y = 0; y < 15; ++y)
162 for (x = 0; x < 21; ++x)
164 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
165 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
169 /* Draw particle systems (foreground) */
170 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
172 (*p)->draw(scroll_x, 0, 1);
179 /* Handle bouncy distros: */
180 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
181 bouncy_distro_action(&bouncy_distros[i]);
183 /* Handle broken bricks: */
184 for (unsigned int i = 0; i < broken_bricks.size(); i++)
185 broken_brick_action(&broken_bricks[i]);
187 /* Handle distro counting: */
188 if (counting_distros)
192 if (distro_counter <= 0)
193 counting_distros = -1;
196 // Handle all kinds of game objects
197 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
198 bouncy_brick_action(&bouncy_bricks[i]);
200 for (unsigned int i = 0; i < floating_scores.size(); i++)
201 floating_score_action(&floating_scores[i]);
203 for (unsigned int i = 0; i < bullets.size(); ++i)
204 bullet_action(&bullets[i]);
206 for (unsigned int i = 0; i < upgrades.size(); i++)
207 upgrade_action(&upgrades[i]);
209 for (unsigned int i = 0; i < bad_guys.size(); i++)
210 bad_guys[i].action();
214 World::add_score(float x, float y, int s)
218 floating_score_type new_floating_score;
219 floating_score_init(&new_floating_score,x,y,s);
220 floating_scores.push_back(new_floating_score);
224 World::add_bouncy_distro(float x, float y)
226 bouncy_distro_type new_bouncy_distro;
227 bouncy_distro_init(&new_bouncy_distro,x,y);
228 bouncy_distros.push_back(new_bouncy_distro);
232 World::add_broken_brick(Tile* tile, float x, float y)
234 add_broken_brick_piece(tile, x, y, -1, -4);
235 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
237 add_broken_brick_piece(tile, x + 16, y, 1, -4);
238 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
242 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
244 broken_brick_type new_broken_brick;
245 broken_brick_init(&new_broken_brick, tile, x, y, xm, ym);
246 broken_bricks.push_back(new_broken_brick);
250 World::add_bouncy_brick(float x, float y)
252 bouncy_brick_type new_bouncy_brick;
253 bouncy_brick_init(&new_bouncy_brick,x,y);
254 bouncy_bricks.push_back(new_bouncy_brick);
258 World::add_bad_guy(float x, float y, BadGuyKind kind)
260 bad_guys.push_back(BadGuy());
261 BadGuy& new_bad_guy = bad_guys.back();
263 new_bad_guy.init(x,y,kind);
267 World::add_upgrade(float x, float y, int dir, int kind)
269 upgrade_type new_upgrade;
270 upgrade_init(&new_upgrade,x,y,dir,kind);
271 upgrades.push_back(new_upgrade);
275 World::add_bullet(float x, float y, float xm, int dir)
277 bullet_type new_bullet;
278 bullet_init(&new_bullet,x,y,xm,dir);
279 bullets.push_back(new_bullet);
281 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
286 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
288 pbouncy_distro->base.x = x;
289 pbouncy_distro->base.y = y;
290 pbouncy_distro->base.ym = -2;
293 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
295 pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym * frame_ratio;
297 pbouncy_distro->base.ym += 0.1 * frame_ratio;
299 if (pbouncy_distro->base.ym >= 0)
300 world.bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
303 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
305 texture_draw(&img_distro[0],
306 pbouncy_distro->base.x - scroll_x,
307 pbouncy_distro->base.y);
310 void broken_brick_init(broken_brick_type* pbroken_brick, Tile* tile,
311 float x, float y, float xm, float ym)
313 pbroken_brick->tile = tile;
314 pbroken_brick->base.x = x;
315 pbroken_brick->base.y = y;
316 pbroken_brick->base.xm = xm;
317 pbroken_brick->base.ym = ym;
319 timer_init(&pbroken_brick->timer, true);
320 timer_start(&pbroken_brick->timer,200);
323 void broken_brick_action(broken_brick_type* pbroken_brick)
325 pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
326 pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
328 if (!timer_check(&pbroken_brick->timer))
329 world.broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
332 void broken_brick_draw(broken_brick_type* pbroken_brick)
340 dest.x = (int)(pbroken_brick->base.x - scroll_x);
341 dest.y = (int)pbroken_brick->base.y;
345 if (pbroken_brick->tile->images.size() > 0)
346 texture_draw_part(&pbroken_brick->tile->images[0],
347 src.x,src.y,dest.x,dest.y,dest.w,dest.h);
350 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
352 pbouncy_brick->base.x = x;
353 pbouncy_brick->base.y = y;
354 pbouncy_brick->offset = 0;
355 pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
356 pbouncy_brick->shape = GameSession::current()->get_level()->gettileid(x, y);
359 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
362 pbouncy_brick->offset = (pbouncy_brick->offset +
363 pbouncy_brick->offset_m * frame_ratio);
367 if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
368 pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
373 if (pbouncy_brick->offset >= 0)
374 world.bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
377 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
382 if (pbouncy_brick->base.x >= scroll_x - 32 &&
383 pbouncy_brick->base.x <= scroll_x + screen->w)
385 dest.x = (int)(pbouncy_brick->base.x - scroll_x);
386 dest.y = (int)pbouncy_brick->base.y;
390 Level* plevel = GameSession::current()->get_level();
392 // FIXME: overdrawing hack to clean the tile from the screen to
393 // paint it later at on offseted position
394 if(plevel->bkgd_image[0] == '\0')
396 fillrect(pbouncy_brick->base.x - scroll_x, pbouncy_brick->base.y,
398 plevel->bkgd_red, plevel->bkgd_green, plevel->bkgd_blue, 0);
402 s = (int)scroll_x / 30;
403 texture_draw_part(&plevel->img_bkgd, dest.x + s, dest.y,
404 dest.x, dest.y,dest.w,dest.h);
407 drawshape(pbouncy_brick->base.x - scroll_x,
408 pbouncy_brick->base.y + pbouncy_brick->offset,
409 pbouncy_brick->shape);
413 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
415 pfloating_score->base.x = x;
416 pfloating_score->base.y = y - 16;
417 timer_init(&pfloating_score->timer,true);
418 timer_start(&pfloating_score->timer,1000);
419 pfloating_score->value = s;
422 void floating_score_action(floating_score_type* pfloating_score)
424 pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
426 if(!timer_check(&pfloating_score->timer))
427 world.floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
430 void floating_score_draw(floating_score_type* pfloating_score)
433 sprintf(str, "%d", pfloating_score->value);
434 text_draw(&gold_text, str, (int)pfloating_score->base.x + 16 - strlen(str) * 8, (int)pfloating_score->base.y, 1);
438 void trybreakbrick(float x, float y, bool small)
440 Level* plevel = GameSession::current()->get_level();
442 Tile* tile = gettile(x, y);
447 /* Get a distro from it: */
448 world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
451 if (!counting_distros)
453 counting_distros = true;
457 if (distro_counter <= 0)
458 plevel->change(x, y, TM_IA, tile->next_tile);
460 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
461 score = score + SCORE_DISTRO;
467 plevel->change(x, y, TM_IA, tile->next_tile);
469 /* Replace it with broken bits: */
470 world.add_broken_brick(tile,
471 ((int)(x + 1) / 32) * 32,
474 /* Get some score: */
475 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
476 score = score + SCORE_BRICK;
482 void tryemptybox(float x, float y, int col_side)
484 Level* plevel = GameSession::current()->get_level();
486 Tile* tile = gettile(x,y);
490 // according to the collision side, set the upgrade direction
498 case 1: //'A': /* Box with a distro! */
499 world.add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
500 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
501 score = score + SCORE_DISTRO;
505 case 2: // 'B': /* Add an upgrade! */
506 if (tux.size == SMALL) /* Tux is small, add mints! */
507 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
508 else /* Tux is big, add coffee: */
509 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
510 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
513 case 3:// '!': /* Add a golden herring */
514 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
521 plevel->change(x, y, TM_IA, tile->next_tile);
524 /* Try to grab a distro: */
525 void trygrabdistro(float x, float y, int bounciness)
527 Level* plevel = GameSession::current()->get_level();
528 Tile* tile = gettile(x, y);
529 if (tile && tile->distro)
531 plevel->change(x, y, TM_IA, tile->next_tile);
532 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
534 if (bounciness == BOUNCE)
536 world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
540 score = score + SCORE_DISTRO;
545 /* Try to bump a bad guy from below: */
546 void trybumpbadguy(float x, float y)
549 for (unsigned int i = 0; i < world.bad_guys.size(); i++)
551 if (world.bad_guys[i].base.x >= x - 32 && world.bad_guys[i].base.x <= x + 32 &&
552 world.bad_guys[i].base.y >= y - 16 && world.bad_guys[i].base.y <= y + 16)
554 world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
560 for (unsigned int i = 0; i < world.upgrades.size(); i++)
562 if (world.upgrades[i].base.height == 32 &&
563 world.upgrades[i].base.x >= x - 32 && world.upgrades[i].base.x <= x + 32 &&
564 world.upgrades[i].base.y >= y - 16 && world.upgrades[i].base.y <= y + 16)
566 world.upgrades[i].base.xm = -world.upgrades[i].base.xm;
567 world.upgrades[i].base.ym = -8;
568 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);