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
29 #include "screen/screen.h"
34 #include "resources.h"
37 #include "background.h"
40 Surface* img_distro[4];
42 World* World::current_ = 0;
44 World::World(const std::string& filename, int level_nr)
45 : level(0), tux(0), background(0), camera(0)
47 // FIXME: Move this to action and draw and everywhere else where the
48 // world calls child functions
55 camera = new Camera(tux, level);
59 level->load(filename, level_nr, this);
61 level->load(filename, this);
63 tux->move(level->start_pos);
68 activate_particle_systems();
71 add_object(new TileMap(level));
78 World::apply_bonuses()
80 // Apply bonuses from former levels
81 switch (player_status.bonus)
83 case PlayerStatus::NO_BONUS:
86 case PlayerStatus::FLOWER_BONUS:
87 tux->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
90 case PlayerStatus::GROWUP_BONUS:
98 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
99 i != gameobjects.end(); ++i) {
109 World::set_defaults()
111 player_status.score_multiplier = 1;
113 counting_distros = false;
116 /* set current song/music */
117 currentmusic = LEVEL_MUSIC;
121 World::add_object(GameObject* object)
123 // XXX hack for now until new collision code is ready
124 BadGuy* badguy = dynamic_cast<BadGuy*> (object);
126 bad_guys.push_back(badguy);
127 Bullet* bullet = dynamic_cast<Bullet*> (object);
129 bullets.push_back(bullet);
130 Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
132 upgrades.push_back(upgrade);
133 Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
135 trampolines.push_back(trampoline);
136 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (object);
138 flying_platforms.push_back(flying_platform);
139 Background* background = dynamic_cast<Background*> (object);
141 this->background = background;
143 gameobjects.push_back(object);
147 World::parse_objects(lisp_object_t* cur)
149 while(!lisp_nil_p(cur)) {
150 lisp_object_t* data = lisp_car(cur);
151 std::string object_type = lisp_symbol(lisp_car(data));
153 LispReader reader(lisp_cdr(data));
155 if(object_type == "trampoline") {
156 add_object(new Trampoline(reader));
158 else if(object_type == "flying-platform") {
159 add_object(new FlyingPlatform(reader));
162 BadGuyKind kind = badguykind_from_string(object_type);
163 add_object(new BadGuy(kind, reader));
171 World::activate_particle_systems()
173 if (level->particle_system == "clouds")
175 add_object(new CloudParticleSystem);
177 else if (level->particle_system == "snow")
179 add_object(new SnowParticleSystem);
181 else if (level->particle_system != "")
183 st_abort("unknown particle system specified in level", "");
191 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
192 i != gameobjects.end(); ++i)
198 World::action(float elapsed_time)
200 tux->check_bounds(context);
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 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
218 bad_guys.erase(std::remove(bad_guys.begin(), bad_guys.end(), badguy),
221 Bullet* bullet = dynamic_cast<Bullet*> (*i);
224 std::remove(bullets.begin(), bullets.end(), bullet),
227 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
230 std::remove(upgrades.begin(), upgrades.end(), upgrade),
233 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
236 std::remove(trampolines.begin(), trampolines.end(), trampoline),
239 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
240 if(flying_platform) {
241 flying_platforms.erase(
242 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
243 flying_platforms.end());
247 i = gameobjects.erase(i);
255 World::collision_handler()
257 // CO_BULLET & CO_BADGUY check
258 for(unsigned int i = 0; i < bullets.size(); ++i)
260 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
262 if((*j)->dying != DYING_NOT)
265 if(rectcollision(bullets[i]->base, (*j)->base))
267 // We have detected a collision and now call the
268 // collision functions of the collided objects.
269 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
270 bullets[i]->collision(CO_BADGUY);
271 break; // bullet is invalid now, so break
276 /* CO_BADGUY & CO_BADGUY check */
277 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
279 if((*i)->dying != DYING_NOT)
282 BadGuys::iterator j = i;
284 for (; j != bad_guys.end(); ++j)
286 if(j == i || (*j)->dying != DYING_NOT)
289 if(rectcollision((*i)->base, (*j)->base))
291 // We have detected a collision and now call the
292 // collision functions of the collided objects.
293 (*j)->collision(*i, CO_BADGUY);
294 (*i)->collision(*j, CO_BADGUY);
299 if(tux->dying != DYING_NOT) return;
301 // CO_BADGUY & CO_PLAYER check
302 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
304 if((*i)->dying != DYING_NOT)
307 if(rectcollision_offset((*i)->base, tux->base, 0, 0))
309 // We have detected a collision and now call the collision
310 // functions of the collided objects.
311 if (tux->previous_base.y < tux->base.y &&
312 tux->previous_base.y + tux->previous_base.height
313 < (*i)->base.y + (*i)->base.height/2
314 && !tux->invincible_timer.started())
316 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
320 tux->collision(*i, CO_BADGUY);
321 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
326 // CO_UPGRADE & CO_PLAYER check
327 for(unsigned int i = 0; i < upgrades.size(); ++i)
329 if(rectcollision(upgrades[i]->base, tux->base))
331 // We have detected a collision and now call the collision
332 // functions of the collided objects.
333 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_NORMAL);
337 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
338 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
340 if (rectcollision((*i)->base, tux->base))
342 if (tux->previous_base.y < tux->base.y &&
343 tux->previous_base.y + tux->previous_base.height
344 < (*i)->base.y + (*i)->base.height/2)
346 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
348 else if (tux->previous_base.y <= tux->base.y)
350 tux->collision(*i, CO_TRAMPOLINE);
351 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
356 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
357 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
359 if (rectcollision((*i)->base, tux->base))
361 if (tux->previous_base.y < tux->base.y &&
362 tux->previous_base.y + tux->previous_base.height
363 < (*i)->base.y + (*i)->base.height/2)
365 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
366 tux->collision(*i, CO_FLYING_PLATFORM);
368 /* else if (tux->previous_base.y <= tux->base.y)
376 World::add_score(const Vector& pos, int s)
378 player_status.score += s;
380 add_object(new FloatingScore(pos, s));
384 World::add_bouncy_distro(const Vector& pos)
386 add_object(new BouncyDistro(pos));
390 World::add_broken_brick(const Vector& pos, Tile* tile)
392 add_broken_brick_piece(pos, Vector(-1, -4), tile);
393 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
395 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
396 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
400 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
403 add_object(new BrokenBrick(tile, pos, movement));
407 World::add_bouncy_brick(const Vector& pos)
409 add_object(new BouncyBrick(pos));
413 World::add_bad_guy(float x, float y, BadGuyKind kind)
415 BadGuy* badguy = new BadGuy(kind, x, y);
421 World::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
423 add_object(new Upgrade(pos, dir, kind));
427 World::add_bullet(const Vector& pos, float xm, Direction dir)
429 if(tux->got_power == Player::FIRE_POWER)
431 if(bullets.size() > MAX_FIRE_BULLETS-1)
434 else if(tux->got_power == Player::ICE_POWER)
436 if(bullets.size() > MAX_ICE_BULLETS-1)
440 Bullet* new_bullet = 0;
441 if(tux->got_power == Player::FIRE_POWER)
442 new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET);
443 else if(tux->got_power == Player::ICE_POWER)
444 new_bullet = new Bullet(pos, xm, dir, ICE_BULLET);
446 st_abort("wrong bullet type.", "");
447 add_object(new_bullet);
449 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
455 World::play_music(int musictype)
457 currentmusic = musictype;
458 switch(currentmusic) {
460 music_manager->play_music(get_level()->get_level_music_fast());
463 music_manager->play_music(get_level()->get_level_music());
466 music_manager->play_music(herring_song);
469 music_manager->halt_music();
475 World::get_music_type()
482 World::trybreakbrick(float x, float y, bool small)
484 Level* plevel = get_level();
486 Tile* tile = gettile(x, y);
487 if (tile->attributes & Tile::BRICK)
491 /* Get a distro from it: */
493 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
495 // TODO: don't handle this in a global way but per-tile...
496 if (!counting_distros)
498 counting_distros = true;
506 if (distro_counter <= 0)
508 counting_distros = false;
509 plevel->change(x, y, TM_IA, tile->next_tile);
512 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
513 player_status.score = player_status.score + SCORE_DISTRO;
514 player_status.distros++;
520 plevel->change(x, y, TM_IA, tile->next_tile);
522 /* Replace it with broken bits: */
523 add_broken_brick(Vector(
524 ((int)(x + 1) / 32) * 32,
525 (int)(y / 32) * 32), tile);
527 /* Get some score: */
528 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
529 player_status.score = player_status.score + SCORE_BRICK;
540 World::tryemptybox(float x, float y, Direction col_side)
542 Tile* tile = gettile(x,y);
543 if (!(tile->attributes & Tile::FULLBOX))
546 // according to the collision side, set the upgrade direction
552 int posx = ((int)(x+1) / 32) * 32;
553 int posy = (int)(y/32) * 32 - 32;
556 case 1: // Box with a distro!
557 add_bouncy_distro(Vector(posx, posy));
558 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
559 player_status.score = player_status.score + SCORE_DISTRO;
560 player_status.distros++;
563 case 2: // Add a fire flower upgrade!
564 if (tux->size == SMALL) /* Tux is small, add mints! */
565 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
566 else /* Tux is big, add a fireflower: */
567 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
568 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
571 case 5: // Add an ice flower upgrade!
572 if (tux->size == SMALL) /* Tux is small, add mints! */
573 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
574 else /* Tux is big, add an iceflower: */
575 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
576 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
579 case 3: // Add a golden herring
580 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
583 case 4: // Add a 1up extra
584 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
591 level->change(x, y, TM_IA, tile->next_tile);
594 /* Try to grab a distro: */
596 World::trygrabdistro(float x, float y, int bounciness)
598 Tile* tile = gettile(x, y);
599 if (tile && (tile->attributes & Tile::COIN))
601 level->change(x, y, TM_IA, tile->next_tile);
602 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
604 if (bounciness == BOUNCE)
606 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
607 (int)(y / 32) * 32));
610 player_status.score = player_status.score + SCORE_DISTRO;
611 player_status.distros++;
615 /* Try to bump a bad guy from below: */
617 World::trybumpbadguy(float x, float y)
620 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
622 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
623 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
625 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
630 for (unsigned int i = 0; i < upgrades.size(); i++)
632 if (upgrades[i]->base.height == 32 &&
633 upgrades[i]->base.x >= x - 32 && upgrades[i]->base.x <= x + 32 &&
634 upgrades[i]->base.y >= y - 16 && upgrades[i]->base.y <= y + 16)
636 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_BUMP);