3 // SuperTux - Trampoline
4 // Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "trampoline.hpp"
23 #include "object_factory.hpp"
25 #include "audio/sound_manager.hpp"
26 #include "sprite/sprite_manager.hpp"
28 /* Trampoline will accelerate player to to VY_INITIAL, if
29 * he jumps on it to VY_MIN. */
31 const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
32 const float VY_MIN = -900; //negative, upwards
33 const float VY_INITIAL = -500;
36 Trampoline::Trampoline(const lisp::Lisp& lisp)
37 : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite" )
39 sound_manager->preload( TRAMPOLINE_SOUND );
40 flags |= FLAG_PORTABLE;
42 physic.set_velocity(0, 0);
43 physic.enable_gravity(true);
47 //Check if this trampoline is not portable
48 if( lisp.get( "portable", portable ) ){
50 flags ^= FLAG_PORTABLE;
51 //we need another sprite
52 sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
53 sprite = sprite_manager->create( sprite_name );
54 sprite->set_action("normal");
60 Trampoline::update( float elapsed_time ){
62 movement = physic.get_movement(elapsed_time);
64 if(sprite->animation_done()) {
65 sprite->set_action("normal");
70 Trampoline::collision(GameObject& other, const CollisionHit& hit )
72 //Tramponine has to be on ground to work.
76 Player* player = dynamic_cast<Player*> (&other);
78 float vy = player->physic.get_velocity_y();
79 //player is falling down on trampoline
80 if(hit.top && vy > 0) {
81 if(player->get_controller()->hold(Controller::JUMP)) {
86 player->physic.set_velocity_y( vy );
87 sound_manager->play( TRAMPOLINE_SOUND );
88 sprite->set_action("swinging", 1);
92 //Fake being solid for moving_object.
93 MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
96 float inside = moving_object->get_bbox().get_bottom() - get_bbox().get_top();
98 Vector pos = moving_object->get_pos();
100 moving_object->set_pos( pos );
103 CollisionHit hit_other = hit;
104 std::swap(hit_other.left, hit_other.right);
105 std::swap(hit_other.top, hit_other.bottom);
106 moving_object->collision_solid( hit_other );
112 Trampoline::collision_solid( const CollisionHit& hit ){
119 Trampoline::grab( MovingObject&, const Vector& pos, Direction ){
120 movement = pos - get_pos();
121 set_group( COLGROUP_DISABLED );
123 sprite->set_animation_loops( 0 );
127 Trampoline::ungrab(MovingObject& , Direction ){
128 set_group( COLGROUP_MOVING );
130 physic.set_velocity(0, 0);
134 IMPLEMENT_FACTORY(Trampoline, "trampoline");