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