Trampoline.
[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 by VY_FACTOR (or to VY_INITIAL if 
29  * that's faster) if he jumps on it. Acceleration is capped at VY_MIN. */
30 namespace{
31   static const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
32   static const float VY_MIN = -1000; //negative, upwards
33   static const float VY_FACTOR = -1.5;
34   static const float VY_INITIAL = -500;
35 }
36
37 Trampoline::Trampoline(const lisp::Lisp& lisp)
38         : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite" )
39 {
40   sound_manager->preload( TRAMPOLINE_SOUND );
41   flags |= FLAG_PORTABLE;
42   physic.set_velocity_y(0);
43   physic.enable_gravity(true);
44   on_ground = false;
45
46   //Check if we need another sprite
47   if( !lisp.get( "sprite", sprite_name ) ){
48     return;
49   }
50   if( sprite_name == "" ){
51     sprite_name = "images/objects/trampoline/trampoline.sprite";
52     return;
53   }
54   //Replace sprite 
55   sprite = sprite_manager->create( sprite_name );
56
57 }
58
59 void
60 Trampoline::update( float elapsed_time ){
61     if( !on_ground ){
62         movement = physic.get_movement(elapsed_time);
63     }
64 }
65
66 HitResponse
67 Trampoline::collision(GameObject& other, const CollisionHit& hit )
68 {
69   Player* player = dynamic_cast<Player*> (&other);
70   if ( player ) {
71     float vy = player->physic.get_velocity_y();
72     //player is falling down on trampolin holding "jump"
73     if( hit.top  && vy > 0 && player->get_controller()->hold( Controller::JUMP )){ 
74       vy *= VY_FACTOR;
75       if( vy < VY_MIN ){
76           vy = VY_MIN;
77       }
78       if( vy > VY_INITIAL ){
79           vy = VY_INITIAL;
80       }
81       player->physic.set_velocity_y( vy );
82       //printf("nachher velocity y = %f\n", player->physic.get_velocity_y());
83       sound_manager->play( TRAMPOLINE_SOUND );
84       return  SOLID;
85     }
86   }
87   
88   return SOLID; //TODO: Nobody should be able to walk through the trampoline.
89 }
90
91 void 
92 Trampoline::collision_solid( const CollisionHit& hit ){
93   if( hit.bottom ){
94      on_ground = true;
95   }
96
97
98 void
99 Trampoline::grab( MovingObject&, const Vector& pos, Direction ){
100   movement = pos - get_pos();
101   set_group( COLGROUP_DISABLED );
102   on_ground = true;
103 }
104
105 void
106 Trampoline::ungrab(MovingObject& , Direction ){
107   set_group( COLGROUP_MOVING );
108   on_ground = false;
109   physic.set_velocity_y(0);
110 }
111
112
113 IMPLEMENT_FACTORY(Trampoline, "trampoline");