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()));
73 get_level()->load_song();
77 scrolling_timer.init(true);
80 World::World(const std::string& subset, int level_nr)
82 // FIXME: Move this to action and draw and everywhere else where the
83 // world calls child functions
87 level->load(subset, level_nr, this);
89 tux = new Player(displaymanager);
90 gameobjects.push_back(tux);
94 get_level()->load_gfx();
96 activate_particle_systems();
97 Background* bg = new Background(displaymanager);
99 bg->set_image(level->img_bkgd, level->bkgd_speed);
101 bg->set_gradient(level->bkgd_top, level->bkgd_bottom);
103 gameobjects.push_back(bg);
105 gameobjects.push_back(new TileMap(displaymanager, get_level()));
106 get_level()->load_song();
110 scrolling_timer.init(true);
114 World::apply_bonuses()
116 // Apply bonuses from former levels
117 switch (player_status.bonus)
119 case PlayerStatus::NO_BONUS:
122 case PlayerStatus::FLOWER_BONUS:
123 tux->got_power = Player::FIRE_POWER; // FIXME: add ice power to here
126 case PlayerStatus::GROWUP_BONUS:
134 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
137 for (std::vector<_GameObject*>::iterator i = gameobjects.begin();
138 i != gameobjects.end(); ++i) {
139 Drawable* drawable = dynamic_cast<Drawable*> (*i);
141 displaymanager.remove_drawable(drawable);
150 World::set_defaults()
155 player_status.score_multiplier = 1;
157 counting_distros = false;
160 /* set current song/music */
161 currentmusic = LEVEL_MUSIC;
165 World::add_object(_GameObject* object)
167 // XXX hack for now until new collision code is ready
168 BadGuy* badguy = dynamic_cast<BadGuy*> (object);
170 bad_guys.push_back(badguy);
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 = "";
182 LispReader reader(lisp_cdr(data));
183 reader.read_string("type", &object_type);
185 if(object_type == "badguy" || object_type == "")
187 BadGuyKind kind = badguykind_from_string(
188 lisp_symbol(lisp_car(data)));
189 add_object(new BadGuy(displaymanager, kind, reader));
191 // TODO add parsing code for trampolines
198 World::activate_objects()
200 for (std::vector< ObjectData<TrampolineData> >::iterator i = level->trampoline_data.begin();
201 i != level->trampoline_data.end();
204 add_object<Trampoline, ObjectData<TrampolineData> >(*i);
209 World::activate_particle_systems()
211 if (level->particle_system == "clouds")
213 add_object(new CloudParticleSystem(displaymanager));
215 else if (level->particle_system == "snow")
217 add_object(new SnowParticleSystem(displaymanager));
219 else if (level->particle_system != "")
221 st_abort("unknown particle system specified in level", "");
229 displaymanager.get_viewport().set_translation(Vector(scroll_x, scroll_y));
230 displaymanager.draw();
232 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
235 for (unsigned int i = 0; i < bullets.size(); ++i)
238 for (unsigned int i = 0; i < upgrades.size(); ++i)
243 World::action(double frame_ratio)
245 tux->check_bounds(level->back_scrolling, (bool)level->hor_autoscroll_speed);
246 scrolling(frame_ratio);
248 for (unsigned int i = 0; i < bullets.size(); ++i)
249 bullets[i].action(frame_ratio);
251 for (unsigned int i = 0; i < upgrades.size(); i++)
252 upgrades[i].action(frame_ratio);
254 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
255 (*i)->action(frame_ratio);
257 /* update objects (don't use iterators here, because the list might change
258 * during the iteration)
260 for(size_t i = 0; i < gameobjects.size(); ++i)
261 gameobjects[i]->action(frame_ratio);
263 /* Handle all possible collisions. */
266 /** cleanup marked objects */
267 for(std::vector<_GameObject*>::iterator i = gameobjects.begin();
268 i != gameobjects.end(); /* nothing */) {
269 if((*i)->is_valid() == false) {
270 Drawable* drawable = dynamic_cast<Drawable*> (*i);
272 displaymanager.remove_drawable(drawable);
273 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
275 std::remove(bad_guys.begin(), bad_guys.end(), badguy);
279 i = gameobjects.erase(i);
286 /* the space that it takes for the screen to start scrolling, regarding */
287 /* screen bounds (in pixels) */
288 // should be higher than screen->w/2 (400)
289 #define X_SPACE (500-16)
290 // should be less than screen->h/2 (300)
293 // the time it takes to move the camera (in ms)
294 #define CHANGE_DIR_SCROLL_SPEED 2000
296 /* This functions takes cares of the scrolling */
297 void World::scrolling(double frame_ratio)
299 /* Y-axis scrolling */
301 float tux_pos_y = tux->base.y + (tux->base.height/2);
303 if(level->height > VISIBLE_TILES_Y-1 && !tux->dying)
305 if (scroll_y < tux_pos_y - (screen->h - Y_SPACE))
306 scroll_y = tux_pos_y - (screen->h - Y_SPACE);
307 else if (scroll_y > tux_pos_y - Y_SPACE)
308 scroll_y = tux_pos_y - Y_SPACE;
311 // this code prevent the screen to scroll before the start or after the level's end
312 if(scroll_y > level->height * 32 - screen->h)
313 scroll_y = level->height * 32 - screen->h;
317 /* X-axis scrolling */
320 if(level->hor_autoscroll_speed)
322 scroll_x += level->hor_autoscroll_speed * frame_ratio;
327 /* Horizontal backscrolling */
328 float tux_pos_x = tux->base.x + (tux->base.width/2);
330 if(tux->old_dir != tux->dir && level->back_scrolling)
331 scrolling_timer.start(CHANGE_DIR_SCROLL_SPEED);
335 if (tux->physic.get_velocity_x() > 0)
337 else if (tux->physic.get_velocity_x() < 0)
341 if (tux->dir == RIGHT)
347 if(scrolling_timer.check())
349 float final_scroll_x;
353 final_scroll_x = tux_pos_x - (screen->w - X_SPACE);
355 final_scroll_x = tux_pos_x - X_SPACE;
357 if((tux->physic.get_velocity_x() > 0 && tux->dir == RIGHT)
358 || (tux->physic.get_velocity_x() < 0 && tux->dir == LEFT))
369 float number = 2.5/(frame_ratio * CHANGE_DIR_SCROLL_SPEED/1000)*exp((CHANGE_DIR_SCROLL_SPEED-scrolling_timer.get_left())/1400.);
370 if(left) number *= -1.;
373 + constant1 * tux->physic.get_velocity_x() * frame_ratio
374 + constant2 * tux->physic.get_acceleration_x() * frame_ratio * frame_ratio;
376 if ((right && final_scroll_x - scroll_x < 0) || (left && final_scroll_x - scroll_x > 0))
377 scroll_x = final_scroll_x;
382 if (right && scroll_x < tux_pos_x - (screen->w - X_SPACE))
383 scroll_x = tux_pos_x - (screen->w - X_SPACE);
384 else if (left && scroll_x > tux_pos_x - X_SPACE && level->back_scrolling)
385 scroll_x = tux_pos_x - X_SPACE;
388 // this code prevent the screen to scroll before the start or after the level's end
389 if(scroll_x > level->width * 32 - screen->w)
390 scroll_x = level->width * 32 - screen->w;
396 World::collision_handler()
398 // CO_BULLET & CO_BADGUY check
399 for(unsigned int i = 0; i < bullets.size(); ++i)
401 for (BadGuys::iterator j = bad_guys.begin(); j != bad_guys.end(); ++j)
403 if((*j)->dying != DYING_NOT)
406 if(rectcollision(bullets[i].base, (*j)->base))
408 // We have detected a collision and now call the
409 // collision functions of the collided objects.
410 // collide with bad_guy first, since bullet_collision will
412 (*j)->collision(&bullets[i], CO_BULLET);
413 bullets[i].collision(CO_BADGUY);
414 break; // bullet is invalid now, so break
419 /* CO_BADGUY & CO_BADGUY check */
420 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
422 if((*i)->dying != DYING_NOT)
425 BadGuys::iterator j = i;
427 for (; j != bad_guys.end(); ++j)
429 if(j == i || (*j)->dying != DYING_NOT)
432 if(rectcollision((*i)->base, (*j)->base))
434 // We have detected a collision and now call the
435 // collision functions of the collided objects.
436 (*j)->collision(*i, CO_BADGUY);
437 (*i)->collision(*j, CO_BADGUY);
442 if(tux->dying != DYING_NOT) return;
444 // CO_BADGUY & CO_PLAYER check
445 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
447 if((*i)->dying != DYING_NOT)
450 if(rectcollision_offset((*i)->base, tux->base, 0, 0))
452 // We have detected a collision and now call the collision
453 // functions of the collided objects.
454 if (tux->previous_base.y < tux->base.y &&
455 tux->previous_base.y + tux->previous_base.height
456 < (*i)->base.y + (*i)->base.height/2
457 && !tux->invincible_timer.started())
459 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
463 tux->collision(*i, CO_BADGUY);
464 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
469 // CO_UPGRADE & CO_PLAYER check
470 for(unsigned int i = 0; i < upgrades.size(); ++i)
472 if(rectcollision(upgrades[i].base, tux->base))
474 // We have detected a collision and now call the collision
475 // functions of the collided objects.
476 upgrades[i].collision(tux, CO_PLAYER, COLLISION_NORMAL);
480 // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY)
481 for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i)
483 if (rectcollision((*i)->base, tux->base))
485 if (tux->previous_base.y < tux->base.y &&
486 tux->previous_base.y + tux->previous_base.height
487 < (*i)->base.y + (*i)->base.height/2)
489 (*i)->collision(tux, CO_PLAYER, COLLISION_SQUISH);
491 else if (tux->previous_base.y <= tux->base.y)
493 tux->collision(*i, CO_TRAMPOLINE);
494 (*i)->collision(tux, CO_PLAYER, COLLISION_NORMAL);
501 World::add_score(const Vector& pos, int s)
503 player_status.score += s;
505 add_object(new FloatingScore(displaymanager, pos, s));
509 World::add_bouncy_distro(const Vector& pos)
511 add_object(new BouncyDistro(displaymanager, pos));
515 World::add_broken_brick(const Vector& pos, Tile* tile)
517 add_broken_brick_piece(pos, Vector(-1, -4), tile);
518 add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile);
520 add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile);
521 add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile);
525 World::add_broken_brick_piece(const Vector& pos, const Vector& movement,
528 add_object(new BrokenBrick(displaymanager, tile, pos, movement));
532 World::add_bouncy_brick(const Vector& pos)
534 add_object(new BouncyBrick(displaymanager, pos));
538 World::add_bad_guy(float x, float y, BadGuyKind kind)
540 BadGuy* badguy = new BadGuy(displaymanager, kind, x, y);
545 template<class T, class U>
547 World::add_object(U data)
549 T* tobject = new T(data);
551 if (data.type == OBJ_TRAMPOLINE)
552 trampolines.push_back(tobject);
558 World::add_upgrade(float x, float y, Direction dir, UpgradeKind kind)
561 new_upgrade.init(x,y,dir,kind);
562 upgrades.push_back(new_upgrade);
566 World::add_bullet(float x, float y, float xm, Direction dir)
568 if(tux->got_power == Player::FIRE_POWER)
570 if(bullets.size() > MAX_FIRE_BULLETS-1)
573 else if(tux->got_power == Player::ICE_POWER)
575 if(bullets.size() > MAX_ICE_BULLETS-1)
580 if(tux->got_power == Player::FIRE_POWER)
581 new_bullet.init(x,y,xm,dir, FIRE_BULLET);
582 else if(tux->got_power == Player::ICE_POWER)
583 new_bullet.init(x,y,xm,dir, ICE_BULLET);
584 bullets.push_back(new_bullet);
586 play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER);
590 World::play_music(int musictype)
592 currentmusic = musictype;
593 switch(currentmusic) {
595 music_manager->play_music(get_level()->get_level_music_fast());
598 music_manager->play_music(get_level()->get_level_music());
601 music_manager->play_music(herring_song);
604 music_manager->halt_music();
610 World::get_music_type()
617 World::trybreakbrick(float x, float y, bool small)
619 Level* plevel = get_level();
621 Tile* tile = gettile(x, y);
626 /* Get a distro from it: */
628 Vector(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32));
630 // TODO: don't handle this in a global way but per-tile...
631 if (!counting_distros)
633 counting_distros = true;
641 if (distro_counter <= 0)
643 counting_distros = false;
644 plevel->change(x, y, TM_IA, tile->next_tile);
647 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
648 player_status.score = player_status.score + SCORE_DISTRO;
649 player_status.distros++;
655 plevel->change(x, y, TM_IA, tile->next_tile);
657 /* Replace it with broken bits: */
658 add_broken_brick(Vector(
659 ((int)(x + 1) / 32) * 32,
660 (int)(y / 32) * 32), tile);
662 /* Get some score: */
663 play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
664 player_status.score = player_status.score + SCORE_BRICK;
675 World::tryemptybox(float x, float y, Direction col_side)
677 Tile* tile = gettile(x,y);
681 // according to the collision side, set the upgrade direction
687 int posx = ((int)(x+1) / 32) * 32;
688 int posy = (int)(y/32) * 32 - 32;
691 case 1: // Box with a distro!
692 add_bouncy_distro(Vector(posx, posy));
693 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
694 player_status.score = player_status.score + SCORE_DISTRO;
695 player_status.distros++;
698 case 2: // Add a fire flower upgrade!
699 if (tux->size == SMALL) /* Tux is small, add mints! */
700 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
701 else /* Tux is big, add a fireflower: */
702 add_upgrade(posx, posy, col_side, UPGRADE_FIREFLOWER);
703 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
706 case 5: // Add an ice flower upgrade!
707 if (tux->size == SMALL) /* Tux is small, add mints! */
708 add_upgrade(posx, posy, col_side, UPGRADE_GROWUP);
709 else /* Tux is big, add an iceflower: */
710 add_upgrade(posx, posy, col_side, UPGRADE_ICEFLOWER);
711 play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
714 case 3: // Add a golden herring
715 add_upgrade(posx, posy, col_side, UPGRADE_HERRING);
718 case 4: // Add a 1up extra
719 add_upgrade(posx, posy, col_side, UPGRADE_1UP);
726 level->change(x, y, TM_IA, tile->next_tile);
729 /* Try to grab a distro: */
731 World::trygrabdistro(float x, float y, int bounciness)
733 Tile* tile = gettile(x, y);
734 if (tile && tile->distro)
736 level->change(x, y, TM_IA, tile->next_tile);
737 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
739 if (bounciness == BOUNCE)
741 add_bouncy_distro(Vector(((int)(x + 1) / 32) * 32,
742 (int)(y / 32) * 32));
745 player_status.score = player_status.score + SCORE_DISTRO;
746 player_status.distros++;
750 /* Try to bump a bad guy from below: */
752 World::trybumpbadguy(float x, float y)
755 for (BadGuys::iterator i = bad_guys.begin(); i != bad_guys.end(); ++i)
757 if ((*i)->base.x >= x - 32 && (*i)->base.x <= x + 32 &&
758 (*i)->base.y >= y - 16 && (*i)->base.y <= y + 16)
760 (*i)->collision(tux, CO_PLAYER, COLLISION_BUMP);
765 for (unsigned int i = 0; i < upgrades.size(); i++)
767 if (upgrades[i].base.height == 32 &&
768 upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
769 upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
771 upgrades[i].collision(tux, CO_PLAYER, COLLISION_BUMP);