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 gameobjects[i]->action(elapsed_time);
208 /* Handle all possible collisions. */
211 /** cleanup marked objects */
212 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
213 i != gameobjects.end(); /* nothing */) {
214 if((*i)->is_valid() == false) {
215 Drawable* drawable = dynamic_cast<Drawable*> (*i);
217 displaymanager.remove_drawable(drawable);
218 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
220 bad_guys.erase(std::remove(bad_guys.begin(), bad_guys.end(), badguy),
223 Bullet* bullet = dynamic_cast<Bullet*> (*i);
226 std::remove(bullets.begin(), bullets.end(), bullet),
229 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
232 std::remove(upgrades.begin(), upgrades.end(), upgrade),
235 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
238 std::remove(trampolines.begin(), trampolines.end(), trampoline),
241 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
242 if(flying_platform) {
243 flying_platforms.erase(
244 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
245 flying_platforms.end());
249 i = gameobjects.erase(i);
257 World::collision_handler()
259 // CO_BULLET & CO_BADGUY check
260 for(unsigned int i = 0; i < bullets.size(); ++i)
262 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
264 if((*j)->dying != DYING_NOT)
267 if(rectcollision(bullets[i]->base, (*j)->base))
269 // We have detected a collision and now call the
270 // collision functions of the collided objects.
271 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
272 bullets[i]->collision(CO_BADGUY);
273 break; // bullet is invalid now, so break
278 /* CO_BADGUY & CO_BADGUY check */
279 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
281 if((*i)->dying != DYING_NOT)
284 BadGuys::iterator j = i;
286 for (; j != bad_guys.end(); ++j)
288 if(j == i || (*j)->dying != DYING_NOT)
291 if(rectcollision((*i)->base, (*j)->base))
293 // We have detected a collision and now call the
294 // collision functions of the collided objects.
295 (*j)->collision(*i, CO_BADGUY);
296 (*i)->collision(*j, CO_BADGUY);
301 if(tux->dying != DYING_NOT) return;
303 // CO_BADGUY & CO_PLAYER check
304 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
306 if((*i)->dying != DYING_NOT)
309 if(rectcollision_offset((*i)->base, tux->base, 0, 0))
311 // We have detected a collision and now call the collision
312 // functions of the collided objects.
313 if (tux->previous_base.y < tux->base.y &&
314 tux->previous_base.y + tux->previous_base.height
315 < (*i)->base.y + (*i)->base.height/2
316 && !tux->invincible_timer.started())
318 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
322 tux->collision(*i, CO_BADGUY);
323 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
328 // CO_UPGRADE & CO_PLAYER check
329 for(unsigned int i = 0; i < upgrades.size(); ++i)
331 if(rectcollision(upgrades[i]->base, tux->base))
333 // We have detected a collision and now call the collision
334 // functions of the collided objects.
335 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_NORMAL);
339 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
340 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
342 if (rectcollision((*i)->base, tux->base))
344 if (tux->previous_base.y < tux->base.y &&
345 tux->previous_base.y + tux->previous_base.height
346 < (*i)->base.y + (*i)->base.height/2)
348 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
350 else if (tux->previous_base.y <= tux->base.y)
352 tux->collision(*i, CO_TRAMPOLINE);
353 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
358 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
359 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
361 if (rectcollision((*i)->base, tux->base))
363 if (tux->previous_base.y < tux->base.y &&
364 tux->previous_base.y + tux->previous_base.height
365 < (*i)->base.y + (*i)->base.height/2)
367 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
368 tux->collision(*i, CO_FLYING_PLATFORM);
370 /* else if (tux->previous_base.y <= tux->base.y)
378 World::add_score(const Vector& pos, int s)
380 player_status.score += s;
382 add_object(new FloatingScore(displaymanager, pos, s));
386 World::add_bouncy_distro(const Vector& pos)
388 add_object(new BouncyDistro(displaymanager, pos));
392 World::add_broken_brick(const Vector& pos, Tile* tile)
394 add_broken_brick_piece(pos, Vector(-1, -4), tile);
395 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
397 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
398 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
402 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
405 add_object(new BrokenBrick(displaymanager, tile, pos, movement));
409 World::add_bouncy_brick(const Vector& pos)
411 add_object(new BouncyBrick(displaymanager, pos));
415 World::add_bad_guy(float x, float y, BadGuyKind kind)
417 BadGuy* badguy = new BadGuy(displaymanager, kind, x, y);
423 World::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
425 add_object(new Upgrade(displaymanager, pos, dir, kind));
429 World::add_bullet(const Vector& pos, float xm, Direction dir)
431 if(tux->got_power == Player::FIRE_POWER)
433 if(bullets.size() > MAX_FIRE_BULLETS-1)
436 else if(tux->got_power == Player::ICE_POWER)
438 if(bullets.size() > MAX_ICE_BULLETS-1)
442 Bullet* new_bullet = 0;
443 if(tux->got_power == Player::FIRE_POWER)
444 new_bullet = new Bullet(displaymanager, pos, xm, dir, FIRE_BULLET);
445 else if(tux->got_power == Player::ICE_POWER)
446 new_bullet = new Bullet(displaymanager, pos, xm, dir, ICE_BULLET);
448 st_abort("wrong bullet type.", "");
449 add_object(new_bullet);
451 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
457 World::play_music(int musictype)
459 currentmusic = musictype;
460 switch(currentmusic) {
462 music_manager->play_music(get_level()->get_level_music_fast());
465 music_manager->play_music(get_level()->get_level_music());
468 music_manager->play_music(herring_song);
471 music_manager->halt_music();
477 World::get_music_type()
484 World::trybreakbrick(float x, float y, bool small)
486 Level* plevel = get_level();
488 Tile* tile = gettile(x, y);
493 /* Get a distro from it: */
495 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
497 // TODO: don't handle this in a global way but per-tile...
498 if (!counting_distros)
500 counting_distros = true;
508 if (distro_counter <= 0)
510 counting_distros = false;
511 plevel->change(x, y, TM_IA, tile->next_tile);
514 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
515 player_status.score = player_status.score + SCORE_DISTRO;
516 player_status.distros++;
522 plevel->change(x, y, TM_IA, tile->next_tile);
524 /* Replace it with broken bits: */
525 add_broken_brick(Vector(
526 ((int)(x + 1) / 32) * 32,
527 (int)(y / 32) * 32), tile);
529 /* Get some score: */
530 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
531 player_status.score = player_status.score + SCORE_BRICK;
542 World::tryemptybox(float x, float y, Direction col_side)
544 Tile* tile = gettile(x,y);
548 // according to the collision side, set the upgrade direction
554 int posx = ((int)(x+1) / 32) * 32;
555 int posy = (int)(y/32) * 32 - 32;
558 case 1: // Box with a distro!
559 add_bouncy_distro(Vector(posx, posy));
560 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
561 player_status.score = player_status.score + SCORE_DISTRO;
562 player_status.distros++;
565 case 2: // Add a fire flower upgrade!
566 if (tux->size == SMALL) /* Tux is small, add mints! */
567 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
568 else /* Tux is big, add a fireflower: */
569 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
570 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
573 case 5: // Add an ice flower upgrade!
574 if (tux->size == SMALL) /* Tux is small, add mints! */
575 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
576 else /* Tux is big, add an iceflower: */
577 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
578 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
581 case 3: // Add a golden herring
582 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
585 case 4: // Add a 1up extra
586 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
593 level->change(x, y, TM_IA, tile->next_tile);
596 /* Try to grab a distro: */
598 World::trygrabdistro(float x, float y, int bounciness)
600 Tile* tile = gettile(x, y);
601 if (tile && tile->distro)
603 level->change(x, y, TM_IA, tile->next_tile);
604 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
606 if (bounciness == BOUNCE)
608 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
609 (int)(y / 32) * 32));
612 player_status.score = player_status.score + SCORE_DISTRO;
613 player_status.distros++;
617 /* Try to bump a bad guy from below: */
619 World::trybumpbadguy(float x, float y)
622 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
624 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
625 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
627 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
632 for (unsigned int i = 0; i < upgrades.size(); i++)
634 if (upgrades[i]->base.height == 32 &&
635 upgrades[i]->base.x >= x - 32 && upgrades[i]->base.x <= x + 32 &&
636 upgrades[i]->base.y >= y - 16 && upgrades[i]->base.y <= y + 16)
638 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_BUMP);