badguy/walking_badguy.[ch]pp: Make it possible to specify the target x velocity.
[supertux.git] / src / badguy / walking_badguy.cpp
1 //  SuperTux - WalkingBadguy
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 <math.h>
18
19 #include "badguy/walking_badguy.hpp"
20
21 #include "sprite/sprite.hpp"
22
23 WalkingBadguy::WalkingBadguy(const Vector& pos, 
24                              const std::string& sprite_name, 
25                              const std::string& walk_left_action, 
26                              const std::string& walk_right_action, 
27                              int layer) :
28   BadGuy(pos, sprite_name, layer), 
29   walk_left_action(walk_left_action), 
30   walk_right_action(walk_right_action), 
31   walk_speed(80), 
32   max_drop_height(-1),
33   turn_around_timer(),
34   turn_around_counter()
35 {
36 }
37
38 WalkingBadguy::WalkingBadguy(const Vector& pos, 
39                              Direction direction, 
40                              const std::string& sprite_name, 
41                              const std::string& walk_left_action, 
42                              const std::string& walk_right_action, 
43                              int layer) :
44   BadGuy(pos, direction, sprite_name, layer), 
45   walk_left_action(walk_left_action), 
46   walk_right_action(walk_right_action), 
47   walk_speed(80), 
48   max_drop_height(-1),
49   turn_around_timer(),
50   turn_around_counter()
51 {
52 }
53
54 WalkingBadguy::WalkingBadguy(const Reader& reader, 
55                              const std::string& sprite_name, 
56                              const std::string& walk_left_action, 
57                              const std::string& walk_right_action, 
58                              int layer) :
59   BadGuy(reader, sprite_name, layer), 
60   walk_left_action(walk_left_action), 
61   walk_right_action(walk_right_action), 
62   walk_speed(80), 
63   max_drop_height(-1),
64   turn_around_timer(),
65   turn_around_counter()
66 {
67 }
68
69 void
70 WalkingBadguy::initialize()
71 {
72   if(frozen)
73     return;
74   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
75   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
76   physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
77   physic.set_acceleration_x (0.0);
78 }
79
80 void
81 WalkingBadguy::set_walk_speed (float ws)
82 {
83   walk_speed = fabs (ws);
84   /* physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); */
85 }
86
87 void
88 WalkingBadguy::add_velocity (const Vector& velocity)
89 {
90   physic.set_velocity(physic.get_velocity() + velocity);
91 }
92
93 void
94 WalkingBadguy::active_update(float elapsed_time, float dest_x_velocity)
95 {
96   BadGuy::active_update(elapsed_time);
97
98   float current_x_velocity = physic.get_velocity_x ();
99
100   if (frozen)
101   {
102     physic.set_velocity_x (0.0);
103     physic.set_acceleration_x (0.0);
104   }
105   /* We're very close to our target speed. Just set it to avoid oscillation */
106   else if ((current_x_velocity > (dest_x_velocity - 5.0))
107       && (current_x_velocity < (dest_x_velocity + 5.0)))
108   {
109     physic.set_velocity_x (dest_x_velocity);
110     physic.set_acceleration_x (0.0);
111   }
112   /* Check if we're going too slow or even in the wrong direction */
113   else if (((dest_x_velocity <= 0.0) && (current_x_velocity > dest_x_velocity))
114       || ((dest_x_velocity > 0.0) && (current_x_velocity < dest_x_velocity)))
115   {
116     /* acceleration == walk-speed => it will take one second to get from zero
117      * to full speed. */
118     physic.set_acceleration_x (dest_x_velocity);
119   }
120   /* Check if we're going too fast */
121   else if (((dest_x_velocity <= 0.0) && (current_x_velocity < dest_x_velocity))
122       || ((dest_x_velocity > 0.0) && (current_x_velocity > dest_x_velocity)))
123   {
124     /* acceleration == walk-speed => it will take one second to get twice the
125      * speed to normal speed. */
126     physic.set_acceleration_x ((-1.0) * dest_x_velocity);
127   }
128   else
129   {
130     /* The above should have covered all cases. */
131     assert (23 == 42);
132   }
133
134   if (max_drop_height > -1) {
135     if (on_ground() && might_fall(max_drop_height+1))
136     {
137       turn_around();
138     }
139   }
140 }
141
142 void
143 WalkingBadguy::active_update(float elapsed_time)
144 {
145   this->active_update (elapsed_time, (dir == LEFT) ? -walk_speed : +walk_speed);
146 }
147
148 void
149 WalkingBadguy::collision_solid(const CollisionHit& hit)
150 {
151
152   update_on_ground_flag(hit);
153
154   if (hit.top) {
155     if (physic.get_velocity_y() < 0) physic.set_velocity_y(0);
156   }
157   if (hit.bottom) {
158     if (physic.get_velocity_y() > 0) physic.set_velocity_y(0);
159   }
160
161   if ((hit.left && (hit.slope_normal.y == 0) && (dir == LEFT)) || (hit.right && (hit.slope_normal.y == 0) && (dir == RIGHT))) {
162     turn_around();
163   }
164
165 }
166
167 HitResponse
168 WalkingBadguy::collision_badguy(BadGuy& , const CollisionHit& hit)
169 {
170
171   if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) {
172     turn_around();
173   }
174
175   return CONTINUE;
176 }
177
178 void
179 WalkingBadguy::turn_around()
180 {
181   if(frozen)
182     return;
183   dir = dir == LEFT ? RIGHT : LEFT;
184   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
185   physic.set_velocity_x(-physic.get_velocity_x());
186   physic.set_acceleration_x (-physic.get_acceleration_x ());
187
188   // if we get dizzy, we fall off the screen
189   if (turn_around_timer.started()) {
190     if (turn_around_counter++ > 10) kill_fall();
191   } else {
192     turn_around_timer.start(1);
193     turn_around_counter = 0;
194   }
195
196 }
197
198 void
199 WalkingBadguy::freeze()
200 {
201   BadGuy::freeze();
202   physic.set_velocity_x(0);
203 }
204
205 void
206 WalkingBadguy::unfreeze()
207 {
208   BadGuy::unfreeze();
209   WalkingBadguy::initialize();
210 }
211
212 float
213 WalkingBadguy::get_velocity_y() const
214 {
215   return physic.get_velocity_y();
216 }
217
218 void
219 WalkingBadguy::set_velocity_y(float vy)
220 {
221   physic.set_velocity_y(vy);
222 }
223
224 /* EOF */