New sound effects
[supertux.git] / src / badguy / skullyhop.cpp
1 //  SkullyHop - A Hopping Skull
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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 "badguy/skullyhop.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "math/random_generator.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
25 const float MIN_RECOVER_TIME = 0.1f; /**< minimum time to stand still before starting a (new) jump */
26 const float MAX_RECOVER_TIME = 1.0f; /**< maximum time to stand still before starting a (new) jump */
27 static const std::string SKULLYHOP_SOUND = "sounds/hop.ogg";
28 }
29
30 SkullyHop::SkullyHop(const Reader& reader) :
31   BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite"),
32   recover_timer(),
33   state()
34 {
35   SoundManager::current()->preload( SKULLYHOP_SOUND );
36 }
37
38 SkullyHop::SkullyHop(const Vector& pos, Direction d) :
39   BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite"),
40   recover_timer(),
41   state()
42 {
43   SoundManager::current()->preload( SKULLYHOP_SOUND );
44 }
45
46 void
47 SkullyHop::initialize()
48 {
49   // initial state is JUMPING, because we might start airborne
50   state = JUMPING;
51   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
52 }
53
54 void
55 SkullyHop::set_state(SkullyHopState newState)
56 {
57   if (newState == STANDING) {
58     physic.set_velocity_x(0);
59     physic.set_velocity_y(0);
60     sprite->set_action(dir == LEFT ? "standing-left" : "standing-right");
61
62     float recover_time = gameRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
63     recover_timer.start(recover_time);
64   } else
65     if (newState == CHARGING) {
66       sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
67     } else
68       if (newState == JUMPING) {
69         sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
70 const float HORIZONTAL_SPEED = 220; /**< x-speed when jumping */
71         physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
72 const float VERTICAL_SPEED = -450;   /**< y-speed when jumping */
73         physic.set_velocity_y(VERTICAL_SPEED);
74         SoundManager::current()->play( SKULLYHOP_SOUND, get_pos());
75       }
76
77   state = newState;
78 }
79
80 bool
81 SkullyHop::collision_squished(GameObject& object)
82 {
83   if (frozen)
84     return BadGuy::collision_squished(object);
85
86   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
87   kill_squished(object);
88   return true;
89 }
90
91 void
92 SkullyHop::collision_solid(const CollisionHit& hit)
93 {
94   if (frozen)
95   {
96     BadGuy::collision_solid(hit);
97     return;
98   }
99
100   // just default behaviour (i.e. stop at floor/walls) when squished
101   if (BadGuy::get_state() == STATE_SQUISHED) {
102     BadGuy::collision_solid(hit);
103   }
104
105   // ignore collisions while standing still
106   if(state != JUMPING)
107     return;
108
109   // check if we hit the floor while falling
110   if(hit.bottom && physic.get_velocity_y() > 0 ) {
111     set_state(STANDING);
112   }
113   // check if we hit the roof while climbing
114   if(hit.top) {
115     physic.set_velocity_y(0);
116   }
117
118   // check if we hit left or right while moving in either direction
119   if(hit.left || hit.right) {
120     dir = dir == LEFT ? RIGHT : LEFT;
121     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
122     physic.set_velocity_x(-0.25*physic.get_velocity_x());
123   }
124 }
125
126 HitResponse
127 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
128 {
129   // behaviour for badguy collisions is the same as for collisions with solids
130   collision_solid(hit);
131
132   return CONTINUE;
133 }
134
135 void
136 SkullyHop::active_update(float elapsed_time)
137 {
138   BadGuy::active_update(elapsed_time);
139
140   // no change if frozen
141   if (frozen)
142     return;
143
144   // charge when fully recovered
145   if ((state == STANDING) && (recover_timer.check())) {
146     set_state(CHARGING);
147     return;
148   }
149
150   // jump as soon as charging animation completed
151   if ((state == CHARGING) && (sprite->animation_done())) {
152     set_state(JUMPING);
153     return;
154   }
155 }
156
157 void
158 SkullyHop::unfreeze()
159 {
160   BadGuy::unfreeze();
161   initialize();
162 }
163
164 bool
165 SkullyHop::is_freezable() const
166 {
167   return true;
168 }
169
170 /* EOF */