1f303f8c332fbd7ec698d2d4aa0cfe0e516e4c57
[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 #include "sprite/sprite.hpp"
28 #include "badguy/walking_badguy.hpp"
29 #include "control/controller.hpp"
30
31 /* Trampoline will accelerate player to to VY_INITIAL, if
32  * he jumps on it to VY_MIN. */
33 namespace {
34   const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
35   const float VY_MIN = -900; //negative, upwards
36   const float VY_INITIAL = -500;
37 }
38
39 Trampoline::Trampoline(const lisp::Lisp& lisp)
40         : Rock(lisp, "images/objects/trampoline/trampoline.sprite")
41 {
42   sound_manager->preload(TRAMPOLINE_SOUND);
43
44   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 }
55
56 void
57 Trampoline::update(float elapsed_time)
58 {
59   if(sprite->animation_done()) {
60     sprite->set_action("normal");
61   }
62
63   Rock::update(elapsed_time);
64 }
65
66 HitResponse
67 Trampoline::collision(GameObject& other, const CollisionHit& hit)
68 {
69
70   //Tramponine has to be on ground to work.
71   if(on_ground) {
72     Player* player = dynamic_cast<Player*> (&other);
73     //Trampoline works for player
74     if(player) {
75       float vy = player->physic.get_velocity_y();
76       //player is falling down on trampoline
77       if(hit.top && vy >= 0) {
78         if(player->get_controller()->hold(Controller::JUMP)) {
79           vy = VY_MIN;
80         } else {
81           vy = VY_INITIAL;
82         }
83         player->physic.set_velocity_y(vy);
84         sound_manager->play(TRAMPOLINE_SOUND);
85         sprite->set_action("swinging", 1);
86         return FORCE_MOVE;
87       }
88     }
89     WalkingBadguy* walking_badguy = dynamic_cast<WalkingBadguy*> (&other);
90     //Trampoline also works for WalkingBadguy
91     if(walking_badguy) {
92       float vy = walking_badguy->get_velocity_y();
93       //walking_badguy is falling down on trampoline
94       if(hit.top && vy >= 0) {
95         vy = VY_INITIAL;
96         walking_badguy->set_velocity_y(vy);
97         sound_manager->play(TRAMPOLINE_SOUND);
98         sprite->set_action("swinging", 1);
99         return FORCE_MOVE;
100       }
101     }
102   }
103
104   return Rock::collision(other, hit);
105 }
106
107 void
108 Trampoline::collision_solid(const CollisionHit& hit) {
109   Rock::collision_solid(hit);
110 }
111
112 void
113 Trampoline::grab(MovingObject& object, const Vector& pos, Direction dir) {
114   sprite->set_animation_loops(0);
115   Rock::grab(object, pos, dir);
116 }
117
118 void
119 Trampoline::ungrab(MovingObject& object, Direction dir) {
120   Rock::ungrab(object, dir);
121 }
122
123 bool
124 Trampoline::is_portable() const
125 {
126   return Rock::is_portable() && portable;
127 }
128
129 IMPLEMENT_FACTORY(Trampoline, "trampoline");