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();
68 World::activate_bad_guys()
70 for (std::vector<BadGuyData>::iterator i = level->badguy_data.begin();
71 i != level->badguy_data.end();
74 add_bad_guy(i->x, i->y, i->kind);
79 World::activate_particle_systems()
81 if (level->particle_system == "clouds")
83 particle_systems.push_back(new CloudParticleSystem);
85 else if (level->particle_system == "snow")
87 particle_systems.push_back(new SnowParticleSystem);
89 else if (level->particle_system != "")
91 st_abort("unknown particle system specified in level", "");
101 if(timer_check(&super_bkgd_timer))
102 texture_draw(&img_super_bkgd, 0, 0);
105 /* Draw the real background */
106 if(get_level()->bkgd_image[0] != '\0')
108 int s = (int)scroll_x / 30;
109 texture_draw_part(&level->img_bkgd, s, 0,0,0,level->img_bkgd.w - s, level->img_bkgd.h);
110 texture_draw_part(&level->img_bkgd, 0, 0,screen->w - s ,0,s,level->img_bkgd.h);
114 clearscreen(level->bkgd_red, level->bkgd_green, level->bkgd_blue);
118 /* Draw particle systems (background) */
119 std::vector<ParticleSystem*>::iterator p;
120 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
122 (*p)->draw(scroll_x, 0, 0);
125 /* Draw background: */
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->bg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
135 /* Draw interactive tiles: */
136 for (y = 0; y < 15; ++y)
138 for (x = 0; x < 21; ++x)
140 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
141 level->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
145 /* (Bouncy bricks): */
146 for (unsigned int i = 0; i < bouncy_bricks.size(); ++i)
147 bouncy_brick_draw(&bouncy_bricks[i]);
149 for (unsigned int i = 0; i < bad_guys.size(); ++i)
154 for (unsigned int i = 0; i < bullets.size(); ++i)
155 bullet_draw(&bullets[i]);
157 for (unsigned int i = 0; i < floating_scores.size(); ++i)
158 floating_score_draw(&floating_scores[i]);
160 for (unsigned int i = 0; i < upgrades.size(); ++i)
161 upgrade_draw(&upgrades[i]);
163 for (unsigned int i = 0; i < bouncy_distros.size(); ++i)
164 bouncy_distro_draw(&bouncy_distros[i]);
166 for (unsigned int i = 0; i < broken_bricks.size(); ++i)
167 broken_brick_draw(&broken_bricks[i]);
169 /* Draw foreground: */
170 for (y = 0; y < 15; ++y)
172 for (x = 0; x < 21; ++x)
174 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
175 level->fg_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
179 /* Draw particle systems (foreground) */
180 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
182 (*p)->draw(scroll_x, 0, 1);
189 /* Handle bouncy distros: */
190 for (unsigned int i = 0; i < bouncy_distros.size(); i++)
191 bouncy_distro_action(&bouncy_distros[i]);
193 /* Handle broken bricks: */
194 for (unsigned int i = 0; i < broken_bricks.size(); i++)
195 broken_brick_action(&broken_bricks[i]);
197 /* Handle distro counting: */
198 if (counting_distros)
202 if (distro_counter <= 0)
203 counting_distros = -1;
206 // Handle all kinds of game objects
207 for (unsigned int i = 0; i < bouncy_bricks.size(); i++)
208 bouncy_brick_action(&bouncy_bricks[i]);
210 for (unsigned int i = 0; i < floating_scores.size(); i++)
211 floating_score_action(&floating_scores[i]);
213 for (unsigned int i = 0; i < bullets.size(); ++i)
214 bullet_action(&bullets[i]);
216 for (unsigned int i = 0; i < upgrades.size(); i++)
217 upgrade_action(&upgrades[i]);
219 for (unsigned int i = 0; i < bad_guys.size(); i++)
220 bad_guys[i].action();
224 World::add_score(float x, float y, int s)
228 floating_score_type new_floating_score;
229 floating_score_init(&new_floating_score,x,y,s);
230 floating_scores.push_back(new_floating_score);
234 World::add_bouncy_distro(float x, float y)
236 bouncy_distro_type new_bouncy_distro;
237 bouncy_distro_init(&new_bouncy_distro,x,y);
238 bouncy_distros.push_back(new_bouncy_distro);
242 World::add_broken_brick(Tile* tile, float x, float y)
244 add_broken_brick_piece(tile, x, y, -1, -4);
245 add_broken_brick_piece(tile, x, y + 16, -1.5, -3);
247 add_broken_brick_piece(tile, x + 16, y, 1, -4);
248 add_broken_brick_piece(tile, x + 16, y + 16, 1.5, -3);
252 World::add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym)
254 broken_brick_type new_broken_brick;
255 broken_brick_init(&new_broken_brick, tile, x, y, xm, ym);
256 broken_bricks.push_back(new_broken_brick);
260 World::add_bouncy_brick(float x, float y)
262 bouncy_brick_type new_bouncy_brick;
263 bouncy_brick_init(&new_bouncy_brick,x,y);
264 bouncy_bricks.push_back(new_bouncy_brick);
268 World::add_bad_guy(float x, float y, BadGuyKind kind)
270 bad_guys.push_back(BadGuy());
271 BadGuy& new_bad_guy = bad_guys.back();
273 new_bad_guy.init(x,y,kind);
277 World::add_upgrade(float x, float y, int dir, int kind)
279 upgrade_type new_upgrade;
280 upgrade_init(&new_upgrade,x,y,dir,kind);
281 upgrades.push_back(new_upgrade);
285 World::add_bullet(float x, float y, float xm, int dir)
287 bullet_type new_bullet;
288 bullet_init(&new_bullet,x,y,xm,dir);
289 bullets.push_back(new_bullet);
291 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
296 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
298 pbouncy_distro->base.x = x;
299 pbouncy_distro->base.y = y;
300 pbouncy_distro->base.ym = -2;
303 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
305 pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym * frame_ratio;
307 pbouncy_distro->base.ym += 0.1 * frame_ratio;
309 if (pbouncy_distro->base.ym >= 0)
310 world.bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
313 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
315 texture_draw(&img_distro[0],
316 pbouncy_distro->base.x - scroll_x,
317 pbouncy_distro->base.y);
320 void broken_brick_init(broken_brick_type* pbroken_brick, Tile* tile,
321 float x, float y, float xm, float ym)
323 pbroken_brick->tile = tile;
324 pbroken_brick->base.x = x;
325 pbroken_brick->base.y = y;
326 pbroken_brick->base.xm = xm;
327 pbroken_brick->base.ym = ym;
329 timer_init(&pbroken_brick->timer, true);
330 timer_start(&pbroken_brick->timer,200);
333 void broken_brick_action(broken_brick_type* pbroken_brick)
335 pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
336 pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
338 if (!timer_check(&pbroken_brick->timer))
339 world.broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
342 void broken_brick_draw(broken_brick_type* pbroken_brick)
350 dest.x = (int)(pbroken_brick->base.x - scroll_x);
351 dest.y = (int)pbroken_brick->base.y;
355 if (pbroken_brick->tile->images.size() > 0)
356 texture_draw_part(&pbroken_brick->tile->images[0],
357 src.x,src.y,dest.x,dest.y,dest.w,dest.h);
360 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
362 pbouncy_brick->base.x = x;
363 pbouncy_brick->base.y = y;
364 pbouncy_brick->offset = 0;
365 pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
366 pbouncy_brick->shape = GameSession::current()->get_level()->gettileid(x, y);
369 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
372 pbouncy_brick->offset = (pbouncy_brick->offset +
373 pbouncy_brick->offset_m * frame_ratio);
377 if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
378 pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
383 if (pbouncy_brick->offset >= 0)
384 world.bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
387 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
392 if (pbouncy_brick->base.x >= scroll_x - 32 &&
393 pbouncy_brick->base.x <= scroll_x + screen->w)
395 dest.x = (int)(pbouncy_brick->base.x - scroll_x);
396 dest.y = (int)pbouncy_brick->base.y;
400 Level* plevel = GameSession::current()->get_level();
402 // FIXME: overdrawing hack to clean the tile from the screen to
403 // paint it later at on offseted position
404 if(plevel->bkgd_image[0] == '\0')
406 fillrect(pbouncy_brick->base.x - scroll_x, pbouncy_brick->base.y,
408 plevel->bkgd_red, plevel->bkgd_green, plevel->bkgd_blue, 0);
412 s = (int)scroll_x / 30;
413 texture_draw_part(&plevel->img_bkgd, dest.x + s, dest.y,
414 dest.x, dest.y,dest.w,dest.h);
417 drawshape(pbouncy_brick->base.x - scroll_x,
418 pbouncy_brick->base.y + pbouncy_brick->offset,
419 pbouncy_brick->shape);
423 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
425 pfloating_score->base.x = x;
426 pfloating_score->base.y = y - 16;
427 timer_init(&pfloating_score->timer,true);
428 timer_start(&pfloating_score->timer,1000);
429 pfloating_score->value = s;
432 void floating_score_action(floating_score_type* pfloating_score)
434 pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
436 if(!timer_check(&pfloating_score->timer))
437 world.floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
440 void floating_score_draw(floating_score_type* pfloating_score)
443 sprintf(str, "%d", pfloating_score->value);
444 text_draw(&gold_text, str, (int)pfloating_score->base.x + 16 - strlen(str) * 8, (int)pfloating_score->base.y, 1);
448 void trybreakbrick(float x, float y, bool small)
450 Level* plevel = GameSession::current()->get_level();
452 Tile* tile = gettile(x, y);
457 /* Get a distro from it: */
458 world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
461 if (!counting_distros)
463 counting_distros = true;
467 if (distro_counter <= 0)
468 plevel->change(x, y, TM_IA, tile->next_tile);
470 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
471 score = score + SCORE_DISTRO;
477 plevel->change(x, y, TM_IA, tile->next_tile);
479 /* Replace it with broken bits: */
480 world.add_broken_brick(tile,
481 ((int)(x + 1) / 32) * 32,
484 /* Get some score: */
485 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
486 score = score + SCORE_BRICK;
492 void tryemptybox(float x, float y, int col_side)
494 Level* plevel = GameSession::current()->get_level();
496 Tile* tile = gettile(x,y);
500 // according to the collision side, set the upgrade direction
508 case 1: //'A': /* Box with a distro! */
509 world.add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32);
510 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
511 score = score + SCORE_DISTRO;
515 case 2: // 'B': /* Add an upgrade! */
516 if (tux.size == SMALL) /* Tux is small, add mints! */
517 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_MINTS);
518 else /* Tux is big, add coffee: */
519 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_COFFEE);
520 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
523 case 3:// '!': /* Add a golden herring */
524 world.add_upgrade((int)((x + 1) / 32) * 32, (int)(y / 32) * 32 - 32, col_side, UPGRADE_HERRING);
531 plevel->change(x, y, TM_IA, tile->next_tile);
534 /* Try to grab a distro: */
535 void trygrabdistro(float x, float y, int bounciness)
537 Level* plevel = GameSession::current()->get_level();
538 Tile* tile = gettile(x, y);
539 if (tile && tile->distro)
541 plevel->change(x, y, TM_IA, tile->next_tile);
542 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
544 if (bounciness == BOUNCE)
546 world.add_bouncy_distro(((int)(x + 1) / 32) * 32,
550 score = score + SCORE_DISTRO;
555 /* Try to bump a bad guy from below: */
556 void trybumpbadguy(float x, float y)
559 for (unsigned int i = 0; i < world.bad_guys.size(); i++)
561 if (world.bad_guys[i].base.x >= x - 32 && world.bad_guys[i].base.x <= x + 32 &&
562 world.bad_guys[i].base.y >= y - 16 && world.bad_guys[i].base.y <= y + 16)
564 world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
570 for (unsigned int i = 0; i < world.upgrades.size(); i++)
572 if (world.upgrades[i].base.height == 32 &&
573 world.upgrades[i].base.x >= x - 32 && world.upgrades[i].base.x <= x + 32 &&
574 world.upgrades[i].base.y >= y - 16 && world.upgrades[i].base.y <= y + 16)
576 world.upgrades[i].base.xm = -world.upgrades[i].base.xm;
577 world.upgrades[i].base.ym = -8;
578 play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);