Exclude end sequence from focus lose code
[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   if ((dir == LEFT) && (physic.get_velocity_x () > 0.0)) {
142     dir = RIGHT;
143     set_action (walk_right_action, /* loops = */ -1);
144   }
145   else if ((dir == RIGHT) && (physic.get_velocity_x () < 0.0)) {
146     dir = LEFT;
147     set_action (walk_left_action, /* loops = */ -1);
148   }
149 }
150
151 void
152 WalkingBadguy::active_update(float elapsed_time)
153 {
154   this->active_update (elapsed_time, (dir == LEFT) ? -walk_speed : +walk_speed);
155 }
156
157 void
158 WalkingBadguy::collision_solid(const CollisionHit& hit)
159 {
160
161   update_on_ground_flag(hit);
162
163   if (hit.top) {
164     if (physic.get_velocity_y() < 0) physic.set_velocity_y(0);
165   }
166   if (hit.bottom) {
167     if (physic.get_velocity_y() > 0) physic.set_velocity_y(0);
168   }
169
170   if ((hit.left && (hit.slope_normal.y == 0) && (dir == LEFT)) || (hit.right && (hit.slope_normal.y == 0) && (dir == RIGHT))) {
171     turn_around();
172   }
173
174 }
175
176 HitResponse
177 WalkingBadguy::collision_badguy(BadGuy& , const CollisionHit& hit)
178 {
179
180   if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) {
181     turn_around();
182   }
183
184   return CONTINUE;
185 }
186
187 void
188 WalkingBadguy::turn_around()
189 {
190   if(frozen)
191     return;
192   dir = dir == LEFT ? RIGHT : LEFT;
193   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
194   physic.set_velocity_x(-physic.get_velocity_x());
195   physic.set_acceleration_x (-physic.get_acceleration_x ());
196
197   // if we get dizzy, we fall off the screen
198   if (turn_around_timer.started()) {
199     if (turn_around_counter++ > 10) kill_fall();
200   } else {
201     turn_around_timer.start(1);
202     turn_around_counter = 0;
203   }
204
205 }
206
207 void
208 WalkingBadguy::freeze()
209 {
210   BadGuy::freeze();
211   physic.set_velocity_x(0);
212 }
213
214 void
215 WalkingBadguy::unfreeze()
216 {
217   BadGuy::unfreeze();
218   WalkingBadguy::initialize();
219 }
220
221 float
222 WalkingBadguy::get_velocity_y() const
223 {
224   return physic.get_velocity_y();
225 }
226
227 void
228 WalkingBadguy::set_velocity_y(float vy)
229 {
230   physic.set_velocity_y(vy);
231 }
232
233 /* EOF */