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)
47 // FIXME: Move this to action and draw and everywhere else where the
48 // world calls child functions
52 level->load(filename, this);
54 tux = new Player(displaymanager);
55 gameobjects.push_back(tux);
59 get_level()->load_gfx();
61 activate_particle_systems();
62 Background* bg = new Background(displaymanager);
64 bg->set_image(level->img_bkgd, level->bkgd_speed);
66 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
68 gameobjects.push_back(bg);
71 gameobjects.push_back(new TileMap(displaymanager, get_level()));
72 get_level()->load_song();
76 scrolling_timer.init(true);
79 World::World(const std::string& subset, int level_nr)
81 // FIXME: Move this to action and draw and everywhere else where the
82 // world calls child functions
86 level->load(subset, level_nr, this);
88 tux = new Player(displaymanager);
89 gameobjects.push_back(tux);
93 get_level()->load_gfx();
94 activate_particle_systems();
95 Background* bg = new Background(displaymanager);
97 bg->set_image(level->img_bkgd, level->bkgd_speed);
99 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
101 gameobjects.push_back(bg);
103 gameobjects.push_back(new TileMap(displaymanager, get_level()));
104 get_level()->load_song();
108 scrolling_timer.init(true);
112 World::apply_bonuses()
114 // Apply bonuses from former levels
115 switch (player_status.bonus)
117 case PlayerStatus::NO_BONUS:
120 case PlayerStatus::FLOWER_BONUS:
121 tux->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
124 case PlayerStatus::GROWUP_BONUS:
132 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
133 i != gameobjects.end(); ++i) {
141 World::set_defaults()
146 player_status.score_multiplier = 1;
148 counting_distros = false;
151 /* set current song/music */
152 currentmusic = LEVEL_MUSIC;
156 World::add_object(GameObject* object)
158 // XXX hack for now until new collision code is ready
159 BadGuy* badguy = dynamic_cast<BadGuy*> (object);
161 bad_guys.push_back(badguy);
162 Bullet* bullet = dynamic_cast<Bullet*> (object);
164 bullets.push_back(bullet);
165 Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
167 upgrades.push_back(upgrade);
168 Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
170 trampolines.push_back(trampoline);
172 gameobjects.push_back(object);
176 World::parse_objects(lisp_object_t* cur)
178 while(!lisp_nil_p(cur)) {
179 lisp_object_t* data = lisp_car(cur);
180 std::string object_type = lisp_symbol(lisp_car(data));
182 LispReader reader(lisp_cdr(data));
184 if(object_type == "trampoline") {
185 add_object(new Trampoline(displaymanager, reader));
187 BadGuyKind kind = badguykind_from_string(object_type);
188 add_object(new BadGuy(displaymanager, kind, reader));
196 World::activate_particle_systems()
198 if (level->particle_system == "clouds")
200 add_object(new CloudParticleSystem(displaymanager));
202 else if (level->particle_system == "snow")
204 add_object(new SnowParticleSystem(displaymanager));
206 else if (level->particle_system != "")
208 st_abort("unknown particle system specified in level", "");
216 displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y));
217 displaymanager.draw();
221 World::action(float elapsed_time)
223 /* update objects (don't use iterators here, because the list might change
224 * during the iteration)
226 for(size_t i = 0; i < gameobjects.size(); ++i)
227 gameobjects[i]->action(elapsed_time);
229 tux->check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
230 scrolling(elapsed_time);
232 /* Handle all possible collisions. */
235 /** cleanup marked objects */
236 for(std::vector<GameObject*>::iterator i = gameobjects.begin();
237 i != gameobjects.end(); /* nothing */) {
238 if((*i)->is_valid() == false) {
239 Drawable* drawable = dynamic_cast<Drawable*> (*i);
241 displaymanager.remove_drawable(drawable);
242 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
244 bad_guys.erase(std::remove(bad_guys.begin(), bad_guys.end(), badguy),
247 Bullet* bullet = dynamic_cast<Bullet*> (*i);
250 std::remove(bullets.begin(), bullets.end(), bullet),
253 Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
256 std::remove(upgrades.begin(), upgrades.end(), upgrade),
259 Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
262 std::remove(trampolines.begin(), trampolines.end(), trampoline),
267 i = gameobjects.erase(i);
274 /* the space that it takes for the screen to start scrolling, regarding */
275 /* screen bounds (in pixels) */
276 // should be higher than screen->w/2 (400)
277 #define X_SPACE (500-16)
278 // should be less than screen->h/2 (300)
281 // the time it takes to move the camera (in ms)
282 #define CHANGE_DIR_SCROLL_SPEED 2000
284 /* This functions takes cares of the scrolling */
285 void World::scrolling(float elapsed_time)
287 /* Y-axis scrolling */
289 float tux_pos_y = tux->base.y + (tux->base.height/2);
291 if(level->height > VISIBLE_TILES_Y-1 && !tux->dying)
293 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
294 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
295 else if (scroll_y > tux_pos_y - Y_SPACE)
296 scroll_y = tux_pos_y - Y_SPACE;
299 // this code prevent the screen to scroll before the start or after the level's end
300 if(scroll_y > level->height * 32 - screen->h)
301 scroll_y = level->height * 32 - screen->h;
305 /* X-axis scrolling */
308 if(level->hor_autoscroll_speed)
310 scroll_x += level->hor_autoscroll_speed * elapsed_time;
315 /* Horizontal backscrolling */
316 float tux_pos_x = tux->base.x + (tux->base.width/2);
318 if(tux->old_dir != tux->dir && level->back_scrolling)
319 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
323 if (tux->physic.get_velocity_x() > 0)
325 else if (tux->physic.get_velocity_x() < 0)
329 if (tux->dir == RIGHT)
335 if(scrolling_timer.check())
337 float final_scroll_x;
341 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
343 final_scroll_x = tux_pos_x - X_SPACE;
345 if((tux->physic.get_velocity_x() > 0 && tux->dir == RIGHT)
346 || (tux->physic.get_velocity_x() < 0 && tux->dir == LEFT))
357 float number = 2.5/(elapsed_time * CHANGE_DIR_SCROLL_SPEED/1000)*exp((CHANGE_DIR_SCROLL_SPEED-scrolling_timer.get_left())/1400.);
358 if(left) number *= -1.;
361 + constant1 * tux->physic.get_velocity_x() * elapsed_time
362 + constant2 * tux->physic.get_acceleration_x() * elapsed_time *
365 if ((right && final_scroll_x - scroll_x < 0) || (left && final_scroll_x - scroll_x > 0))
366 scroll_x = final_scroll_x;
371 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
372 scroll_x = tux_pos_x - (screen->w - X_SPACE);
373 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
374 scroll_x = tux_pos_x - X_SPACE;
377 // this code prevent the screen to scroll before the start or after the level's end
378 if(scroll_x > level->width * 32 - screen->w)
379 scroll_x = level->width * 32 - screen->w;
385 World::collision_handler()
387 // CO_BULLET & CO_BADGUY check
388 for(unsigned int i = 0; i < bullets.size(); ++i)
390 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
392 if((*j)->dying != DYING_NOT)
395 if(rectcollision(bullets[i]->base, (*j)->base))
397 // We have detected a collision and now call the
398 // collision functions of the collided objects.
399 (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL);
400 bullets[i]->collision(CO_BADGUY);
401 break; // bullet is invalid now, so break
406 /* CO_BADGUY & CO_BADGUY check */
407 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
409 if((*i)->dying != DYING_NOT)
412 BadGuys::iterator j = i;
414 for (; j != bad_guys.end(); ++j)
416 if(j == i || (*j)->dying != DYING_NOT)
419 if(rectcollision((*i)->base, (*j)->base))
421 // We have detected a collision and now call the
422 // collision functions of the collided objects.
423 (*j)->collision(*i, CO_BADGUY);
424 (*i)->collision(*j, CO_BADGUY);
429 if(tux->dying != DYING_NOT) return;
431 // CO_BADGUY & CO_PLAYER check
432 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
434 if((*i)->dying != DYING_NOT)
437 if(rectcollision_offset((*i)->base, tux->base, 0, 0))
439 // We have detected a collision and now call the collision
440 // functions of the collided objects.
441 if (tux->previous_base.y < tux->base.y &&
442 tux->previous_base.y + tux->previous_base.height
443 < (*i)->base.y + (*i)->base.height/2
444 && !tux->invincible_timer.started())
446 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
450 tux->collision(*i, CO_BADGUY);
451 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
456 // CO_UPGRADE & CO_PLAYER check
457 for(unsigned int i = 0; i < upgrades.size(); ++i)
459 if(rectcollision(upgrades[i]->base, tux->base))
461 // We have detected a collision and now call the collision
462 // functions of the collided objects.
463 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_NORMAL);
467 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
468 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
470 if (rectcollision((*i)->base, tux->base))
472 if (tux->previous_base.y < tux->base.y &&
473 tux->previous_base.y + tux->previous_base.height
474 < (*i)->base.y + (*i)->base.height/2)
476 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
478 else if (tux->previous_base.y <= tux->base.y)
480 tux->collision(*i, CO_TRAMPOLINE);
481 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
488 World::add_score(const Vector& pos, int s)
490 player_status.score += s;
492 add_object(new FloatingScore(displaymanager, pos, s));
496 World::add_bouncy_distro(const Vector& pos)
498 add_object(new BouncyDistro(displaymanager, pos));
502 World::add_broken_brick(const Vector& pos, Tile* tile)
504 add_broken_brick_piece(pos, Vector(-1, -4), tile);
505 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
507 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
508 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
512 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
515 add_object(new BrokenBrick(displaymanager, tile, pos, movement));
519 World::add_bouncy_brick(const Vector& pos)
521 add_object(new BouncyBrick(displaymanager, pos));
525 World::add_bad_guy(float x, float y, BadGuyKind kind)
527 BadGuy* badguy = new BadGuy(displaymanager, kind, x, y);
533 World::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind)
535 add_object(new Upgrade(displaymanager, pos, dir, kind));
539 World::add_bullet(const Vector& pos, float xm, Direction dir)
541 if(tux->got_power == Player::FIRE_POWER)
543 if(bullets.size() > MAX_FIRE_BULLETS-1)
546 else if(tux->got_power == Player::ICE_POWER)
548 if(bullets.size() > MAX_ICE_BULLETS-1)
552 Bullet* new_bullet = 0;
553 if(tux->got_power == Player::FIRE_POWER)
554 new_bullet = new Bullet(displaymanager, pos, xm, dir, FIRE_BULLET);
555 else if(tux->got_power == Player::ICE_POWER)
556 new_bullet = new Bullet(displaymanager, pos, xm, dir, ICE_BULLET);
558 st_abort("wrong bullet type.", "");
559 add_object(new_bullet);
561 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
565 World::play_music(int musictype)
567 currentmusic = musictype;
568 switch(currentmusic) {
570 music_manager->play_music(get_level()->get_level_music_fast());
573 music_manager->play_music(get_level()->get_level_music());
576 music_manager->play_music(herring_song);
579 music_manager->halt_music();
585 World::get_music_type()
592 World::trybreakbrick(float x, float y, bool small)
594 Level* plevel = get_level();
596 Tile* tile = gettile(x, y);
601 /* Get a distro from it: */
603 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
605 // TODO: don't handle this in a global way but per-tile...
606 if (!counting_distros)
608 counting_distros = true;
616 if (distro_counter <= 0)
618 counting_distros = false;
619 plevel->change(x, y, TM_IA, tile->next_tile);
622 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
623 player_status.score = player_status.score + SCORE_DISTRO;
624 player_status.distros++;
630 plevel->change(x, y, TM_IA, tile->next_tile);
632 /* Replace it with broken bits: */
633 add_broken_brick(Vector(
634 ((int)(x + 1) / 32) * 32,
635 (int)(y / 32) * 32), tile);
637 /* Get some score: */
638 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
639 player_status.score = player_status.score + SCORE_BRICK;
650 World::tryemptybox(float x, float y, Direction col_side)
652 Tile* tile = gettile(x,y);
656 // according to the collision side, set the upgrade direction
662 int posx = ((int)(x+1) / 32) * 32;
663 int posy = (int)(y/32) * 32 - 32;
666 case 1: // Box with a distro!
667 add_bouncy_distro(Vector(posx, posy));
668 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
669 player_status.score = player_status.score + SCORE_DISTRO;
670 player_status.distros++;
673 case 2: // Add a fire flower upgrade!
674 if (tux->size == SMALL) /* Tux is small, add mints! */
675 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
676 else /* Tux is big, add a fireflower: */
677 add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER);
678 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
681 case 5: // Add an ice flower upgrade!
682 if (tux->size == SMALL) /* Tux is small, add mints! */
683 add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP);
684 else /* Tux is big, add an iceflower: */
685 add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER);
686 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
689 case 3: // Add a golden herring
690 add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING);
693 case 4: // Add a 1up extra
694 add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP);
701 level->change(x, y, TM_IA, tile->next_tile);
704 /* Try to grab a distro: */
706 World::trygrabdistro(float x, float y, int bounciness)
708 Tile* tile = gettile(x, y);
709 if (tile && tile->distro)
711 level->change(x, y, TM_IA, tile->next_tile);
712 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
714 if (bounciness == BOUNCE)
716 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
717 (int)(y / 32) * 32));
720 player_status.score = player_status.score + SCORE_DISTRO;
721 player_status.distros++;
725 /* Try to bump a bad guy from below: */
727 World::trybumpbadguy(float x, float y)
730 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
732 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
733 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
735 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
740 for (unsigned int i = 0; i < upgrades.size(); i++)
742 if (upgrades[i]->base.height == 32 &&
743 upgrades[i]->base.x >= x - 32 && upgrades[i]->base.x <= x + 32 &&
744 upgrades[i]->base.y >= y - 16 && upgrades[i]->base.y <= y + 16)
746 upgrades[i]->collision(tux, CO_PLAYER, COLLISION_BUMP);