Tentative checkin of tuxdev's "Object improvement patch, part 1"
[supertux.git] / src / object / trampoline.cpp
1 //  $Id$
2 //
3 //  SuperTux - Trampoline
4 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "trampoline.hpp"
23 #include "object_factory.hpp"
24 #include "player.hpp"
25 #include "audio/sound_manager.hpp"
26 #include "sprite/sprite_manager.hpp"
27
28 /* Trampoline will accelerate player to to VY_INITIAL, if
29  * he jumps on it to VY_MIN. */
30 namespace {
31   const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
32   const float VY_MIN = -900; //negative, upwards
33   const float VY_INITIAL = -500;
34 }
35
36 Trampoline::Trampoline(const lisp::Lisp& lisp)
37         : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite" )
38 {
39   sound_manager->preload( TRAMPOLINE_SOUND );
40   physic.set_velocity(0, 0);
41   physic.enable_gravity(true);
42   on_ground = false;
43
44   bool portable = true;
45   //Check if this trampoline is not portable
46   if( lisp.get( "portable", portable ) ){
47     if( !portable ){
48         //we need another sprite
49         sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
50         sprite = sprite_manager->create( sprite_name );
51         sprite->set_action("normal");
52     }
53   }
54   set_portable(portable);
55 }
56
57 void
58 Trampoline::update( float elapsed_time ){
59     if( !on_ground ){
60         movement = physic.get_movement(elapsed_time);
61     }
62     if(sprite->animation_done()) {
63       sprite->set_action("normal");
64     }
65 }
66
67 HitResponse
68 Trampoline::collision(GameObject& other, const CollisionHit& hit )
69 {
70   //Tramponine has to be on ground to work.
71   if( !on_ground ){
72       return FORCE_MOVE;
73   }
74   Player* player = dynamic_cast<Player*> (&other);
75   if ( player ) {
76     float vy = player->physic.get_velocity_y();
77     //player is falling down on trampoline
78     if(hit.top && vy > 0) {
79       if(player->get_controller()->hold(Controller::JUMP)) {
80         vy = VY_MIN;
81       } else {
82         vy = VY_INITIAL;
83       }
84       player->physic.set_velocity_y( vy );
85       sound_manager->play( TRAMPOLINE_SOUND );
86       sprite->set_action("swinging", 1);
87       return FORCE_MOVE;
88     }
89   }
90   //Fake being solid for moving_object.
91   MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
92   if( moving_object ){
93       if( hit.top ){
94         float inside = moving_object->get_bbox().get_bottom() - get_bbox().get_top();
95         if( inside > 0 ){
96           Vector pos = moving_object->get_pos();
97           pos.y -= inside;
98           moving_object->set_pos( pos );
99         }
100       }
101       CollisionHit hit_other = hit;
102       std::swap(hit_other.left, hit_other.right);
103       std::swap(hit_other.top, hit_other.bottom);
104       moving_object->collision_solid( hit_other );
105   }
106   return FORCE_MOVE;
107 }
108
109 void
110 Trampoline::collision_solid( const CollisionHit& hit ){
111   if( hit.bottom ){
112      on_ground = true;
113   }
114 }
115
116 void
117 Trampoline::grab( MovingObject&, const Vector& pos, Direction ){
118   movement = pos - get_pos();
119   set_group( COLGROUP_DISABLED );
120   on_ground = true;
121   sprite->set_animation_loops( 0 );
122 }
123
124 void
125 Trampoline::ungrab(MovingObject& , Direction ){
126   set_group( COLGROUP_MOVING );
127   on_ground = false;
128   physic.set_velocity(0, 0);
129 }
130
131
132 IMPLEMENT_FACTORY(Trampoline, "trampoline");