Trampoline is a MOVING_STATIC now.
[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   set_group(COLGROUP_MOVING_STATIC);
40   sound_manager->preload(TRAMPOLINE_SOUND);
41   physic.set_velocity(0, 0);
42   physic.enable_gravity(true);
43   on_ground = false;
44   grabbed = false;
45   portable = true;
46   //Check if this trampoline is not portable
47   if(lisp.get("portable", portable)) {
48     if(!portable) {
49         //we need another sprite
50         sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
51         sprite = sprite_manager->create(sprite_name);
52         sprite->set_action("normal");
53     }
54   }
55 }
56
57 void
58 Trampoline::update(float elapsed_time)
59 {
60   if(sprite->animation_done()) {
61     sprite->set_action("normal");
62   }
63   if(!grabbed) {
64     movement = physic.get_movement(elapsed_time);
65   }
66 }
67
68 HitResponse
69 Trampoline::collision(GameObject& other, const CollisionHit& hit )
70 {
71   //Tramponine has to be on ground to work.
72   if(!on_ground) {
73       return FORCE_MOVE;
74   }
75   Player* player = dynamic_cast<Player*> (&other);
76   if(player) {
77     float vy = player->physic.get_velocity_y();
78     //player is falling down on trampoline
79     if(hit.top && vy >= 0) {
80       if(player->get_controller()->hold(Controller::JUMP)) {
81         vy = VY_MIN;
82       } else {
83         vy = VY_INITIAL;
84       }
85       player->physic.set_velocity_y(vy);
86       sound_manager->play(TRAMPOLINE_SOUND);
87       sprite->set_action("swinging", 1);
88       return FORCE_MOVE;
89     }
90   }
91   return FORCE_MOVE;
92 }
93
94 void
95 Trampoline::collision_solid(const CollisionHit& hit) {
96   if(hit.top || hit.bottom)
97     physic.set_velocity_y(0);
98   if(hit.left || hit.right)
99     physic.set_velocity_x(0);
100   if(hit.crush)
101     physic.set_velocity(0, 0);
102
103   if(hit.bottom && !on_ground) {
104      on_ground = true;
105   }
106 }
107
108 void
109 Trampoline::grab(MovingObject&, const Vector& pos, Direction) {
110   movement = pos - get_pos();
111   set_group(COLGROUP_DISABLED);
112   sprite->set_animation_loops(0);
113   on_ground = false;
114   grabbed = true;
115 }
116
117 void
118 Trampoline::ungrab(MovingObject& , Direction ) {
119   set_group(COLGROUP_MOVING_STATIC);
120   on_ground = false;
121   physic.set_velocity(0, 0);
122   grabbed = false;
123 }
124
125 bool
126 Trampoline::is_portable() const
127 {
128   return portable;
129 }
130
131 IMPLEMENT_FACTORY(Trampoline, "trampoline");