4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
34 #include "resources.h"
37 #include "display_manager.h"
38 #include "background.h"
41 Surface* img_distro[4];
43 World* World::current_ = 0;
45 World::World(const std::string& filename, int level_nr)
47 // FIXME: Move this to action and draw and everywhere else where the
48 // world calls child functions
53 level->load(filename, level_nr, this);
55 level->load(filename, this);
58 tux = new Player(displaymanager);
65 activate_particle_systems();
66 background = new Background(displaymanager);
68 background->set_image(level->img_bkgd, level->bkgd_speed);
70 background->set_gradient(level->bkgd_top, level->bkgd_bottom);
72 add_object(background);
75 add_object(new TileMap(displaymanager, level));
78 camera = new Camera(tux, level);
85 World::apply_bonuses()
87 // Apply bonuses from former levels
88 switch (player_status.bonus)
90 case PlayerStatus::NO_BONUS:
93 case PlayerStatus::FLOWER_BONUS:
94 tux->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
97 case PlayerStatus::GROWUP_BONUS:
105 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
106 i != gameobjects.end(); ++i) {
114 World::set_defaults()
116 player_status.score_multiplier = 1;
118 counting_distros = false;
121 /* set current song/music */
122 currentmusic = LEVEL_MUSIC;
126 World::add_object(GameObject* object)
128 // XXX hack for now until new collision code is ready
129 BadGuy* badguy = dynamic_cast<BadGuy*> (object);
131 bad_guys.push_back(badguy);
132 Bullet* bullet = dynamic_cast<Bullet*> (object);
134 bullets.push_back(bullet);
135 Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
137 upgrades.push_back(upgrade);
138 Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
140 trampolines.push_back(trampoline);
141 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (object);
143 flying_platforms.push_back(flying_platform);
145 gameobjects.push_back(object);
149 World::parse_objects(lisp_object_t* cur)
151 while(!lisp_nil_p(cur)) {
152 lisp_object_t* data = lisp_car(cur);
153 std::string object_type = lisp_symbol(lisp_car(data));
155 LispReader reader(lisp_cdr(data));
157 if(object_type == "trampoline") {
158 add_object(new Trampoline(displaymanager, reader));
160 else if(object_type == "flying-platform") {
161 add_object(new FlyingPlatform(displaymanager, reader));
164 BadGuyKind kind = badguykind_from_string(object_type);
165 add_object(new BadGuy(displaymanager, kind, reader));
173 World::activate_particle_systems()
175 if (level->particle_system == "clouds")
177 add_object(new CloudParticleSystem(displaymanager));
179 else if (level->particle_system == "snow")
181 add_object(new SnowParticleSystem(displaymanager));
183 else if (level->particle_system != "")
185 st_abort("unknown particle system specified in level", "");
193 displaymanager.draw(*camera);
197 World::action(float elapsed_time)
199 tux->check_bounds(*camera,
200 level->back_scrolling, (bool)level->hor_autoscroll_speed);
202 /* update objects (don't use iterators here, because the list might change
203 * during the iteration)
205 for(size_t i = 0; i < gameobjects.size(); ++i)
206 if(gameobjects[i]->is_valid())
207 gameobjects[i]->action(elapsed_time);
209 /* Handle all possible collisions. */
212 /** cleanup marked objects */
213 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
214 i != gameobjects.end(); /* nothing */) {
215 if((*i)->is_valid() == false) {
216 Drawable* drawable = dynamic_cast<Drawable*> (*i);
218 displaymanager.remove_drawable(drawable);
219 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
221 bad_guys.erase(std::remove(bad_guys.begin(), bad_guys.end(), badguy),
224 Bullet* bullet = dynamic_cast<Bullet*> (*i);
227 std::remove(bullets.begin(), bullets.end(), bullet),
230 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
233 std::remove(upgrades.begin(), upgrades.end(), upgrade),
236 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
239 std::remove(trampolines.begin(), trampolines.end(), trampoline),
242 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
243 if(flying_platform) {
244 flying_platforms.erase(
245 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
246 flying_platforms.end());
250 i = gameobjects.erase(i);
258 World::collision_handler()
260 // CO_BULLET & CO_BADGUY check
261 for(unsigned int i = 0; i < bullets.size(); ++i)
263 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
265 if((*j)->dying != DYING_NOT)
268 if(rectcollision(bullets[i]->base, (*j)->base))
270 // We have detected a collision and now call the
271 // collision functions of the collided objects.
272 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
273 bullets[i]->collision(CO_BADGUY);
274 break; // bullet is invalid now, so break
279 /* CO_BADGUY & CO_BADGUY check */
280 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
282 if((*i)->dying != DYING_NOT)
285 BadGuys::iterator j = i;
287 for (; j != bad_guys.end(); ++j)
289 if(j == i || (*j)->dying != DYING_NOT)
292 if(rectcollision((*i)->base, (*j)->base))
294 // We have detected a collision and now call the
295 // collision functions of the collided objects.
296 (*j)->collision(*i, CO_BADGUY);
297 (*i)->collision(*j, CO_BADGUY);
302 if(tux->dying != DYING_NOT) return;
304 // CO_BADGUY & CO_PLAYER check
305 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
307 if((*i)->dying != DYING_NOT)
310 if(rectcollision_offset((*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 < (*i)->base.y + (*i)->base.height/2
317 && !tux->invincible_timer.started())
319 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
323 tux->collision(*i, CO_BADGUY);
324 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
329 // CO_UPGRADE & CO_PLAYER check
330 for(unsigned int i = 0; i < upgrades.size(); ++i)
332 if(rectcollision(upgrades[i]->base, tux->base))
334 // We have detected a collision and now call the collision
335 // functions of the collided objects.
336 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_NORMAL);
340 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
341 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
343 if (rectcollision((*i)->base, tux->base))
345 if (tux->previous_base.y < tux->base.y &&
346 tux->previous_base.y + tux->previous_base.height
347 < (*i)->base.y + (*i)->base.height/2)
349 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
351 else if (tux->previous_base.y <= tux->base.y)
353 tux->collision(*i, CO_TRAMPOLINE);
354 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
359 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
360 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
362 if (rectcollision((*i)->base, tux->base))
364 if (tux->previous_base.y < tux->base.y &&
365 tux->previous_base.y + tux->previous_base.height
366 < (*i)->base.y + (*i)->base.height/2)
368 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
369 tux->collision(*i, CO_FLYING_PLATFORM);
371 /* else if (tux->previous_base.y <= tux->base.y)
379 World::add_score(const Vector& pos, int s)
381 player_status.score += s;
383 add_object(new FloatingScore(displaymanager, pos, s));
387 World::add_bouncy_distro(const Vector& pos)
389 add_object(new BouncyDistro(displaymanager, pos));
393 World::add_broken_brick(const Vector& pos, Tile* tile)
395 add_broken_brick_piece(pos, Vector(-1, -4), tile);
396 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
398 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
399 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
403 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
406 add_object(new BrokenBrick(displaymanager, tile, pos, movement));
410 World::add_bouncy_brick(const Vector& pos)
412 add_object(new BouncyBrick(displaymanager, pos));
416 World::add_bad_guy(float x, float y, BadGuyKind kind)
418 BadGuy* badguy = new BadGuy(displaymanager, kind, x, y);
424 World::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
426 add_object(new Upgrade(displaymanager, pos, dir, kind));
430 World::add_bullet(const Vector& pos, float xm, Direction dir)
432 if(tux->got_power == Player::FIRE_POWER)
434 if(bullets.size() > MAX_FIRE_BULLETS-1)
437 else if(tux->got_power == Player::ICE_POWER)
439 if(bullets.size() > MAX_ICE_BULLETS-1)
443 Bullet* new_bullet = 0;
444 if(tux->got_power == Player::FIRE_POWER)
445 new_bullet = new Bullet(displaymanager, pos, xm, dir, FIRE_BULLET);
446 else if(tux->got_power == Player::ICE_POWER)
447 new_bullet = new Bullet(displaymanager, pos, xm, dir, ICE_BULLET);
449 st_abort("wrong bullet type.", "");
450 add_object(new_bullet);
452 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
458 World::play_music(int musictype)
460 currentmusic = musictype;
461 switch(currentmusic) {
463 music_manager->play_music(get_level()->get_level_music_fast());
466 music_manager->play_music(get_level()->get_level_music());
469 music_manager->play_music(herring_song);
472 music_manager->halt_music();
478 World::get_music_type()
485 World::trybreakbrick(float x, float y, bool small)
487 Level* plevel = get_level();
489 Tile* tile = gettile(x, y);
494 /* Get a distro from it: */
496 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
498 // TODO: don't handle this in a global way but per-tile...
499 if (!counting_distros)
501 counting_distros = true;
509 if (distro_counter <= 0)
511 counting_distros = false;
512 plevel->change(x, y, TM_IA, tile->next_tile);
515 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
516 player_status.score = player_status.score + SCORE_DISTRO;
517 player_status.distros++;
523 plevel->change(x, y, TM_IA, tile->next_tile);
525 /* Replace it with broken bits: */
526 add_broken_brick(Vector(
527 ((int)(x + 1) / 32) * 32,
528 (int)(y / 32) * 32), tile);
530 /* Get some score: */
531 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
532 player_status.score = player_status.score + SCORE_BRICK;
543 World::tryemptybox(float x, float y, Direction col_side)
545 Tile* tile = gettile(x,y);
549 // according to the collision side, set the upgrade direction
555 int posx = ((int)(x+1) / 32) * 32;
556 int posy = (int)(y/32) * 32 - 32;
559 case 1: // Box with a distro!
560 add_bouncy_distro(Vector(posx, posy));
561 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
562 player_status.score = player_status.score + SCORE_DISTRO;
563 player_status.distros++;
566 case 2: // Add a fire flower upgrade!
567 if (tux->size == SMALL) /* Tux is small, add mints! */
568 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
569 else /* Tux is big, add a fireflower: */
570 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
571 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
574 case 5: // Add an ice flower upgrade!
575 if (tux->size == SMALL) /* Tux is small, add mints! */
576 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
577 else /* Tux is big, add an iceflower: */
578 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
579 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
582 case 3: // Add a golden herring
583 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
586 case 4: // Add a 1up extra
587 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
594 level->change(x, y, TM_IA, tile->next_tile);
597 /* Try to grab a distro: */
599 World::trygrabdistro(float x, float y, int bounciness)
601 Tile* tile = gettile(x, y);
602 if (tile && tile->distro)
604 level->change(x, y, TM_IA, tile->next_tile);
605 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
607 if (bounciness == BOUNCE)
609 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
610 (int)(y / 32) * 32));
613 player_status.score = player_status.score + SCORE_DISTRO;
614 player_status.distros++;
618 /* Try to bump a bad guy from below: */
620 World::trybumpbadguy(float x, float y)
623 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
625 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
626 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
628 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
633 for (unsigned int i = 0; i < upgrades.size(); i++)
635 if (upgrades[i]->base.height == 32 &&
636 upgrades[i]->base.x >= x - 32 && upgrades[i]->base.x <= x + 32 &&
637 upgrades[i]->base.y >= y - 16 && upgrades[i]->base.y <= y + 16)
639 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_BUMP);