1 // SuperTux - Trampoline
2 // Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
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.
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.
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/>.
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"
27 /* Trampoline will accelerate player to to VY_INITIAL, if
28 * he jumps on it to VY_MIN. */
30 const std::string TRAMPOLINE_SOUND = "sounds/trampoline.wav";
31 const float VY_MIN = -900; //negative, upwards
32 const float VY_INITIAL = -500;
35 Trampoline::Trampoline(const Reader& lisp) :
36 Rock(lisp, "images/objects/trampoline/trampoline.sprite"),
39 SoundManager::current()->preload(TRAMPOLINE_SOUND);
41 //Check if this trampoline is not portable
42 if(lisp.get("portable", portable)) {
44 //we need another sprite
45 sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
46 sprite = SpriteManager::current()->create(sprite_name);
47 sprite->set_action("normal");
52 Trampoline::Trampoline(const Vector& pos, bool port) :
53 Rock(pos, "images/objects/trampoline/trampoline.sprite"),
56 SoundManager::current()->preload(TRAMPOLINE_SOUND);
58 sprite_name = "images/objects/trampoline/trampoline_fix.sprite";
59 sprite = SpriteManager::current()->create(sprite_name);
60 sprite->set_action("normal");
65 Trampoline::update(float elapsed_time)
67 if(sprite->animation_done()) {
68 sprite->set_action("normal");
71 Rock::update(elapsed_time);
75 Trampoline::collision(GameObject& other, const CollisionHit& hit)
78 //Tramponine has to be on ground to work.
80 Player* player = dynamic_cast<Player*> (&other);
81 //Trampoline works for player
83 float vy = player->get_physic().get_velocity_y();
84 //player is falling down on trampoline
85 if(hit.top && vy >= 0) {
86 if (!(player->get_status()->bonus == AIR_BONUS))
87 vy = player->get_controller()->hold(Controller::JUMP) ? VY_MIN : VY_INITIAL;
89 vy = player->get_controller()->hold(Controller::JUMP) ? VY_MIN - 300 : VY_INITIAL - 40;
90 player->get_physic().set_velocity_y(vy);
91 SoundManager::current()->play(TRAMPOLINE_SOUND);
92 sprite->set_action("swinging", 1);
96 WalkingBadguy* walking_badguy = dynamic_cast<WalkingBadguy*> (&other);
97 //Trampoline also works for WalkingBadguy
99 float vy = walking_badguy->get_velocity_y();
100 //walking_badguy is falling down on trampoline
101 if(hit.top && vy >= 0) {
103 walking_badguy->set_velocity_y(vy);
104 SoundManager::current()->play(TRAMPOLINE_SOUND);
105 sprite->set_action("swinging", 1);
111 return Rock::collision(other, hit);
115 Trampoline::collision_solid(const CollisionHit& hit) {
116 Rock::collision_solid(hit);
120 Trampoline::grab(MovingObject& object, const Vector& pos, Direction dir) {
121 sprite->set_animation_loops(0);
122 Rock::grab(object, pos, dir);
126 Trampoline::ungrab(MovingObject& object, Direction dir) {
127 Rock::ungrab(object, dir);
131 Trampoline::is_portable() const
133 return Rock::is_portable() && portable;