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)
46 : level(0), tux(0), background(0), camera(0)
48 // FIXME: Move this to action and draw and everywhere else where the
49 // world calls child functions
52 tux = new Player(displaymanager);
56 camera = new Camera(tux, level);
60 level->load(filename, level_nr, this);
62 level->load(filename, this);
64 tux->move(level->start_pos);
70 activate_particle_systems();
71 background = new Background(displaymanager);
73 background->set_image(level->img_bkgd, level->bkgd_speed);
75 background->set_gradient(level->bkgd_top, level->bkgd_bottom);
77 add_object(background);
80 add_object(new TileMap(displaymanager, level));
87 World::apply_bonuses()
89 // Apply bonuses from former levels
90 switch (player_status.bonus)
92 case PlayerStatus::NO_BONUS:
95 case PlayerStatus::FLOWER_BONUS:
96 tux->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
99 case PlayerStatus::GROWUP_BONUS:
107 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
108 i != gameobjects.end(); ++i) {
118 World::set_defaults()
120 player_status.score_multiplier = 1;
122 counting_distros = false;
125 /* set current song/music */
126 currentmusic = LEVEL_MUSIC;
130 World::add_object(GameObject* object)
132 // XXX hack for now until new collision code is ready
133 BadGuy* badguy = dynamic_cast<BadGuy*> (object);
135 bad_guys.push_back(badguy);
136 Bullet* bullet = dynamic_cast<Bullet*> (object);
138 bullets.push_back(bullet);
139 Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
141 upgrades.push_back(upgrade);
142 Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
144 trampolines.push_back(trampoline);
145 FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (object);
147 flying_platforms.push_back(flying_platform);
149 gameobjects.push_back(object);
153 World::parse_objects(lisp_object_t* cur)
155 while(!lisp_nil_p(cur)) {
156 lisp_object_t* data = lisp_car(cur);
157 std::string object_type = lisp_symbol(lisp_car(data));
159 LispReader reader(lisp_cdr(data));
161 if(object_type == "trampoline") {
162 add_object(new Trampoline(displaymanager, reader));
164 else if(object_type == "flying-platform") {
165 add_object(new FlyingPlatform(displaymanager, reader));
168 BadGuyKind kind = badguykind_from_string(object_type);
169 add_object(new BadGuy(displaymanager, kind, reader));
177 World::activate_particle_systems()
179 if (level->particle_system == "clouds")
181 add_object(new CloudParticleSystem(displaymanager));
183 else if (level->particle_system == "snow")
185 add_object(new SnowParticleSystem(displaymanager));
187 else if (level->particle_system != "")
189 st_abort("unknown particle system specified in level", "");
197 displaymanager.draw(*camera);
201 World::action(float elapsed_time)
203 tux->check_bounds(*camera);
205 /* update objects (don't use iterators here, because the list might change
206 * during the iteration)
208 for(size_t i = 0; i < gameobjects.size(); ++i)
209 if(gameobjects[i]->is_valid())
210 gameobjects[i]->action(elapsed_time);
212 /* Handle all possible collisions. */
215 /** cleanup marked objects */
216 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
217 i != gameobjects.end(); /* nothing */) {
218 if((*i)->is_valid() == false) {
219 Drawable* drawable = dynamic_cast<Drawable*> (*i);
221 displaymanager.remove_drawable(drawable);
222 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
224 bad_guys.erase(std::remove(bad_guys.begin(), bad_guys.end(), badguy),
227 Bullet* bullet = dynamic_cast<Bullet*> (*i);
230 std::remove(bullets.begin(), bullets.end(), bullet),
233 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
236 std::remove(upgrades.begin(), upgrades.end(), upgrade),
239 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
242 std::remove(trampolines.begin(), trampolines.end(), trampoline),
245 FlyingPlatform* flying_platform= dynamic_cast<FlyingPlatform*> (*i);
246 if(flying_platform) {
247 flying_platforms.erase(
248 std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform),
249 flying_platforms.end());
253 i = gameobjects.erase(i);
261 World::collision_handler()
263 // CO_BULLET & CO_BADGUY check
264 for(unsigned int i = 0; i < bullets.size(); ++i)
266 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
268 if((*j)->dying != DYING_NOT)
271 if(rectcollision(bullets[i]->base, (*j)->base))
273 // We have detected a collision and now call the
274 // collision functions of the collided objects.
275 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
276 bullets[i]->collision(CO_BADGUY);
277 break; // bullet is invalid now, so break
282 /* CO_BADGUY & CO_BADGUY check */
283 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
285 if((*i)->dying != DYING_NOT)
288 BadGuys::iterator j = i;
290 for (; j != bad_guys.end(); ++j)
292 if(j == i || (*j)->dying != DYING_NOT)
295 if(rectcollision((*i)->base, (*j)->base))
297 // We have detected a collision and now call the
298 // collision functions of the collided objects.
299 (*j)->collision(*i, CO_BADGUY);
300 (*i)->collision(*j, CO_BADGUY);
305 if(tux->dying != DYING_NOT) return;
307 // CO_BADGUY & CO_PLAYER check
308 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
310 if((*i)->dying != DYING_NOT)
313 if(rectcollision_offset((*i)->base, tux->base, 0, 0))
315 // We have detected a collision and now call the collision
316 // functions of the collided objects.
317 if (tux->previous_base.y < tux->base.y &&
318 tux->previous_base.y + tux->previous_base.height
319 < (*i)->base.y + (*i)->base.height/2
320 && !tux->invincible_timer.started())
322 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
326 tux->collision(*i, CO_BADGUY);
327 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
332 // CO_UPGRADE & CO_PLAYER check
333 for(unsigned int i = 0; i < upgrades.size(); ++i)
335 if(rectcollision(upgrades[i]->base, tux->base))
337 // We have detected a collision and now call the collision
338 // functions of the collided objects.
339 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_NORMAL);
343 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
344 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
346 if (rectcollision((*i)->base, tux->base))
348 if (tux->previous_base.y < tux->base.y &&
349 tux->previous_base.y + tux->previous_base.height
350 < (*i)->base.y + (*i)->base.height/2)
352 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
354 else if (tux->previous_base.y <= tux->base.y)
356 tux->collision(*i, CO_TRAMPOLINE);
357 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
362 // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY)
363 for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i)
365 if (rectcollision((*i)->base, tux->base))
367 if (tux->previous_base.y < tux->base.y &&
368 tux->previous_base.y + tux->previous_base.height
369 < (*i)->base.y + (*i)->base.height/2)
371 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
372 tux->collision(*i, CO_FLYING_PLATFORM);
374 /* else if (tux->previous_base.y <= tux->base.y)
382 World::add_score(const Vector& pos, int s)
384 player_status.score += s;
386 add_object(new FloatingScore(displaymanager, pos, s));
390 World::add_bouncy_distro(const Vector& pos)
392 add_object(new BouncyDistro(displaymanager, pos));
396 World::add_broken_brick(const Vector& pos, Tile* tile)
398 add_broken_brick_piece(pos, Vector(-1, -4), tile);
399 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
401 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
402 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
406 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
409 add_object(new BrokenBrick(displaymanager, tile, pos, movement));
413 World::add_bouncy_brick(const Vector& pos)
415 add_object(new BouncyBrick(displaymanager, pos));
419 World::add_bad_guy(float x, float y, BadGuyKind kind)
421 BadGuy* badguy = new BadGuy(displaymanager, kind, x, y);
427 World::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
429 add_object(new Upgrade(displaymanager, pos, dir, kind));
433 World::add_bullet(const Vector& pos, float xm, Direction dir)
435 if(tux->got_power == Player::FIRE_POWER)
437 if(bullets.size() > MAX_FIRE_BULLETS-1)
440 else if(tux->got_power == Player::ICE_POWER)
442 if(bullets.size() > MAX_ICE_BULLETS-1)
446 Bullet* new_bullet = 0;
447 if(tux->got_power == Player::FIRE_POWER)
448 new_bullet = new Bullet(displaymanager, pos, xm, dir, FIRE_BULLET);
449 else if(tux->got_power == Player::ICE_POWER)
450 new_bullet = new Bullet(displaymanager, pos, xm, dir, ICE_BULLET);
452 st_abort("wrong bullet type.", "");
453 add_object(new_bullet);
455 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
461 World::play_music(int musictype)
463 currentmusic = musictype;
464 switch(currentmusic) {
466 music_manager->play_music(get_level()->get_level_music_fast());
469 music_manager->play_music(get_level()->get_level_music());
472 music_manager->play_music(herring_song);
475 music_manager->halt_music();
481 World::get_music_type()
488 World::trybreakbrick(float x, float y, bool small)
490 Level* plevel = get_level();
492 Tile* tile = gettile(x, y);
497 /* Get a distro from it: */
499 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
501 // TODO: don't handle this in a global way but per-tile...
502 if (!counting_distros)
504 counting_distros = true;
512 if (distro_counter <= 0)
514 counting_distros = false;
515 plevel->change(x, y, TM_IA, tile->next_tile);
518 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
519 player_status.score = player_status.score + SCORE_DISTRO;
520 player_status.distros++;
526 plevel->change(x, y, TM_IA, tile->next_tile);
528 /* Replace it with broken bits: */
529 add_broken_brick(Vector(
530 ((int)(x + 1) / 32) * 32,
531 (int)(y / 32) * 32), tile);
533 /* Get some score: */
534 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
535 player_status.score = player_status.score + SCORE_BRICK;
546 World::tryemptybox(float x, float y, Direction col_side)
548 Tile* tile = gettile(x,y);
552 // according to the collision side, set the upgrade direction
558 int posx = ((int)(x+1) / 32) * 32;
559 int posy = (int)(y/32) * 32 - 32;
562 case 1: // Box with a distro!
563 add_bouncy_distro(Vector(posx, posy));
564 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
565 player_status.score = player_status.score + SCORE_DISTRO;
566 player_status.distros++;
569 case 2: // Add a fire flower upgrade!
570 if (tux->size == SMALL) /* Tux is small, add mints! */
571 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
572 else /* Tux is big, add a fireflower: */
573 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
574 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
577 case 5: // Add an ice flower upgrade!
578 if (tux->size == SMALL) /* Tux is small, add mints! */
579 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
580 else /* Tux is big, add an iceflower: */
581 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
582 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
585 case 3: // Add a golden herring
586 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
589 case 4: // Add a 1up extra
590 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
597 level->change(x, y, TM_IA, tile->next_tile);
600 /* Try to grab a distro: */
602 World::trygrabdistro(float x, float y, int bounciness)
604 Tile* tile = gettile(x, y);
605 if (tile && tile->distro)
607 level->change(x, y, TM_IA, tile->next_tile);
608 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
610 if (bounciness == BOUNCE)
612 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
613 (int)(y / 32) * 32));
616 player_status.score = player_status.score + SCORE_DISTRO;
617 player_status.distros++;
621 /* Try to bump a bad guy from below: */
623 World::trybumpbadguy(float x, float y)
626 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
628 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
629 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
631 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
636 for (unsigned int i = 0; i < upgrades.size(); i++)
638 if (upgrades[i]->base.height == 32 &&
639 upgrades[i]->base.x >= x - 32 && upgrades[i]->base.x <= x + 32 &&
640 upgrades[i]->base.y >= y - 16 && upgrades[i]->base.y <= y + 16)
642 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_BUMP);