Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / trampoline.cpp
1 //  SuperTux - Trampoline
2 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "audio/sound_manager.hpp"
18 #include "badguy/walking_badguy.hpp"
19 #include "control/controller.hpp"
20 #include "object/player.hpp"
21 #include "object/trampoline.hpp"
22 #include "sprite/sprite.hpp"
23 #include "sprite/sprite_manager.hpp"
24 #include "supertux/object_factory.hpp"
25
26 /* Trampoline will accelerate player to to VY_INITIAL, if
27  * he jumps on it to VY_MIN. */
28 namespace {
29 const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
30 const float VY_MIN = -900; //negative, upwards
31 const float VY_INITIAL = -500;
32 }
33
34 Trampoline::Trampoline(const Reader& lisp)
35   : Rock(lisp, "images/objects/trampoline/trampoline.sprite")
36 {
37   sound_manager->preload(TRAMPOLINE_SOUND);
38
39   portable = true;
40   //Check if this trampoline is not portable
41   if(lisp.get("portable", portable)) {
42     if(!portable) {
43       //we need another sprite
44       sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
45       sprite = sprite_manager->create(sprite_name);
46       sprite->set_action("normal");
47     }
48   }
49 }
50
51 void
52 Trampoline::update(float elapsed_time)
53 {
54   if(sprite->animation_done()) {
55     sprite->set_action("normal");
56   }
57
58   Rock::update(elapsed_time);
59 }
60
61 HitResponse
62 Trampoline::collision(GameObject& other, const CollisionHit& hit)
63 {
64
65   //Tramponine has to be on ground to work.
66   if(on_ground) {
67     Player* player = dynamic_cast<Player*> (&other);
68     //Trampoline works for player
69     if(player) {
70       float vy = player->get_physic().get_velocity_y();
71       //player is falling down on trampoline
72       if(hit.top && vy >= 0) {
73         if(player->get_controller()->hold(Controller::JUMP)) {
74           vy = VY_MIN;
75         } else {
76           vy = VY_INITIAL;
77         }
78         player->get_physic().set_velocity_y(vy);
79         sound_manager->play(TRAMPOLINE_SOUND);
80         sprite->set_action("swinging", 1);
81         return FORCE_MOVE;
82       }
83     }
84     WalkingBadguy* walking_badguy = dynamic_cast<WalkingBadguy*> (&other);
85     //Trampoline also works for WalkingBadguy
86     if(walking_badguy) {
87       float vy = walking_badguy->get_velocity_y();
88       //walking_badguy is falling down on trampoline
89       if(hit.top && vy >= 0) {
90         vy = VY_INITIAL;
91         walking_badguy->set_velocity_y(vy);
92         sound_manager->play(TRAMPOLINE_SOUND);
93         sprite->set_action("swinging", 1);
94         return FORCE_MOVE;
95       }
96     }
97   }
98
99   return Rock::collision(other, hit);
100 }
101
102 void
103 Trampoline::collision_solid(const CollisionHit& hit) {
104   Rock::collision_solid(hit);
105 }
106
107 void
108 Trampoline::grab(MovingObject& object, const Vector& pos, Direction dir) {
109   sprite->set_animation_loops(0);
110   Rock::grab(object, pos, dir);
111 }
112
113 void
114 Trampoline::ungrab(MovingObject& object, Direction dir) {
115   Rock::ungrab(object, dir);
116 }
117
118 bool
119 Trampoline::is_portable() const
120 {
121   return Rock::is_portable() && portable;
122 }
123
124 IMPLEMENT_FACTORY(Trampoline, "trampoline");
125
126 /* EOF */