Renamed some variables for rusty_trampoline since the build process for Windows canno...
[supertux.git] / src / object / rusty_trampoline.cpp
1 //  SuperTux - Rusty Trampoline
2 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
3 //  Copyright (C) 2011 Jonas Kuemmerlin <rgcjonas@googlemail.com>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "audio/sound_manager.hpp"
19 #include "badguy/walking_badguy.hpp"
20 #include "control/controller.hpp"
21 #include "object/player.hpp"
22 #include "object/rusty_trampoline.hpp"
23 #include "sprite/sprite.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "util/reader.hpp"
27
28 /* Trampoline will accelerate player to to VY_BOUNCE, if
29  * he jumps on it to VY_TRIGGER. */
30 namespace {
31 const std::string BOUNCE_SOUND = "sounds/trampoline.wav";
32 const float VY_TRIGGER = -900; //negative, upwards
33 const float VY_BOUNCE = -500;
34 }
35
36 RustyTrampoline::RustyTrampoline(const Reader& lisp) :
37   Rock(lisp, "images/objects/rusty-trampoline/rusty-trampoline.sprite"),
38   portable(true), counter(3)
39 {
40   sound_manager->preload(BOUNCE_SOUND);
41
42   lisp.get("counter", counter);
43   lisp.get("portable", portable); //do we really need this?
44 }
45
46 void
47 RustyTrampoline::update(float elapsed_time)
48 {
49   if(sprite->animation_done()) {
50     if (counter < 1) {
51       remove_me();
52     } else {
53       sprite->set_action("normal");
54     }
55
56   }
57
58   Rock::update(elapsed_time);
59 }
60
61 HitResponse
62 RustyTrampoline::collision(GameObject& other, const CollisionHit& hit)
63 {
64
65   //Trampoline 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_TRIGGER;
75         } else {
76           vy = VY_BOUNCE;
77         }
78         player->get_physic().set_velocity_y(vy);
79         sound_manager->play(BOUNCE_SOUND);
80         counter--;
81         if (counter > 0) {
82           sprite->set_action("swinging", 1);
83         } else {
84           sprite->set_action("breaking", 1);
85         }
86
87         return FORCE_MOVE;
88       }
89     }
90     WalkingBadguy* walking_badguy = dynamic_cast<WalkingBadguy*> (&other);
91     //Trampoline also works for WalkingBadguy
92     if(walking_badguy) {
93       float vy = walking_badguy->get_velocity_y();
94       //walking_badguy is falling down on trampoline
95       if(hit.top && vy >= 0) {
96         vy = VY_BOUNCE;
97         walking_badguy->set_velocity_y(vy);
98         sound_manager->play(BOUNCE_SOUND);
99         counter--;
100         if (counter > 0) {
101           sprite->set_action("swinging", 1);
102         } else {
103           sprite->set_action("breaking", 1);
104         }
105         return FORCE_MOVE;
106       }
107     }
108   }
109
110   return Rock::collision(other, hit);
111 }
112
113 void
114 RustyTrampoline::collision_solid(const CollisionHit& hit) {
115   Rock::collision_solid(hit);
116 }
117
118 void
119 RustyTrampoline::grab(MovingObject& object, const Vector& pos, Direction dir) {
120   Rock::grab(object, pos, dir);
121 }
122
123 void
124 RustyTrampoline::ungrab(MovingObject& object, Direction dir) {
125   Rock::ungrab(object, dir);
126   sprite->set_action("breaking", 1);
127   counter = 0; //remove in update()
128 }
129
130 bool
131 RustyTrampoline::is_portable() const
132 {
133   return Rock::is_portable() && portable;
134 }
135
136 /* EOF */