src/object/trampoline.cpp

Go to the documentation of this file.
00001 //  SuperTux - Trampoline
00002 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "audio/sound_manager.hpp"
00018 #include "badguy/walking_badguy.hpp"
00019 #include "control/controller.hpp"
00020 #include "object/player.hpp"
00021 #include "object/trampoline.hpp"
00022 #include "sprite/sprite.hpp"
00023 #include "sprite/sprite_manager.hpp"
00024 #include "supertux/object_factory.hpp"
00025 #include "util/reader.hpp"
00026 
00027 /* Trampoline will accelerate player to to VY_INITIAL, if
00028  * he jumps on it to VY_MIN. */
00029 namespace {
00030 const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
00031 const float VY_MIN = -900; //negative, upwards
00032 const float VY_INITIAL = -500;
00033 }
00034 
00035 Trampoline::Trampoline(const Reader& lisp) :
00036   Rock(lisp, "images/objects/trampoline/trampoline.sprite"),
00037   portable(true)
00038 {
00039   sound_manager->preload(TRAMPOLINE_SOUND);
00040 
00041   //Check if this trampoline is not portable
00042   if(lisp.get("portable", portable)) {
00043     if(!portable) {
00044       //we need another sprite
00045       sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
00046       sprite = sprite_manager->create(sprite_name);
00047       sprite->set_action("normal");
00048     }
00049   }
00050 }
00051 
00052 void
00053 Trampoline::update(float elapsed_time)
00054 {
00055   if(sprite->animation_done()) {
00056     sprite->set_action("normal");
00057   }
00058 
00059   Rock::update(elapsed_time);
00060 }
00061 
00062 HitResponse
00063 Trampoline::collision(GameObject& other, const CollisionHit& hit)
00064 {
00065 
00066   //Tramponine has to be on ground to work.
00067   if(on_ground) {
00068     Player* player = dynamic_cast<Player*> (&other);
00069     //Trampoline works for player
00070     if(player) {
00071       float vy = player->get_physic().get_velocity_y();
00072       //player is falling down on trampoline
00073       if(hit.top && vy >= 0) {
00074         if(player->get_controller()->hold(Controller::JUMP)) {
00075           vy = VY_MIN;
00076         } else {
00077           vy = VY_INITIAL;
00078         }
00079         player->get_physic().set_velocity_y(vy);
00080         sound_manager->play(TRAMPOLINE_SOUND);
00081         sprite->set_action("swinging", 1);
00082         return FORCE_MOVE;
00083       }
00084     }
00085     WalkingBadguy* walking_badguy = dynamic_cast<WalkingBadguy*> (&other);
00086     //Trampoline also works for WalkingBadguy
00087     if(walking_badguy) {
00088       float vy = walking_badguy->get_velocity_y();
00089       //walking_badguy is falling down on trampoline
00090       if(hit.top && vy >= 0) {
00091         vy = VY_INITIAL;
00092         walking_badguy->set_velocity_y(vy);
00093         sound_manager->play(TRAMPOLINE_SOUND);
00094         sprite->set_action("swinging", 1);
00095         return FORCE_MOVE;
00096       }
00097     }
00098   }
00099 
00100   return Rock::collision(other, hit);
00101 }
00102 
00103 void
00104 Trampoline::collision_solid(const CollisionHit& hit) {
00105   Rock::collision_solid(hit);
00106 }
00107 
00108 void
00109 Trampoline::grab(MovingObject& object, const Vector& pos, Direction dir) {
00110   sprite->set_animation_loops(0);
00111   Rock::grab(object, pos, dir);
00112 }
00113 
00114 void
00115 Trampoline::ungrab(MovingObject& object, Direction dir) {
00116   Rock::ungrab(object, dir);
00117 }
00118 
00119 bool
00120 Trampoline::is_portable() const
00121 {
00122   return Rock::is_portable() && portable;
00123 }
00124 
00125 /* EOF */

Generated on Mon Jun 9 03:38:20 2014 for SuperTux by  doxygen 1.5.1