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