1 // SuperTux - WalkingBadguy
2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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/>.
19 #include "badguy/walking_badguy.hpp"
21 #include "sprite/sprite.hpp"
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,
28 BadGuy(pos, sprite_name, layer),
29 walk_left_action(walk_left_action),
30 walk_right_action(walk_right_action),
38 WalkingBadguy::WalkingBadguy(const Vector& pos,
40 const std::string& sprite_name,
41 const std::string& walk_left_action,
42 const std::string& walk_right_action,
44 BadGuy(pos, direction, sprite_name, layer),
45 walk_left_action(walk_left_action),
46 walk_right_action(walk_right_action),
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,
59 BadGuy(reader, sprite_name, layer),
60 walk_left_action(walk_left_action),
61 walk_right_action(walk_right_action),
70 WalkingBadguy::initialize()
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);
81 WalkingBadguy::set_walk_speed (float ws)
83 walk_speed = fabs (ws);
84 /* physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); */
88 WalkingBadguy::add_velocity (const Vector& velocity)
90 physic.set_velocity(physic.get_velocity() + velocity);
94 WalkingBadguy::active_update(float elapsed_time)
96 BadGuy::active_update(elapsed_time);
98 float current_x_velocity = physic.get_velocity_x ();
99 float dest_x_velocity = (dir == LEFT) ? -walk_speed : +walk_speed;
103 physic.set_velocity_x (0.0);
104 physic.set_acceleration_x (0.0);
106 /* We're very close to our target speed. Just set it to avoid oscillation */
107 else if ((current_x_velocity > (dest_x_velocity - 5.0))
108 && (current_x_velocity < (dest_x_velocity + 5.0)))
110 physic.set_velocity_x (dest_x_velocity);
111 physic.set_acceleration_x (0.0);
113 /* Check if we're going too slow or even in the wrong direction */
114 else if (((dir == LEFT) && (current_x_velocity > dest_x_velocity))
115 || ((dir == RIGHT) && (current_x_velocity < dest_x_velocity)))
117 /* acceleration == walk-speed => it will take one second to get from zero
119 physic.set_acceleration_x (dest_x_velocity);
121 /* Check if we're going too fast */
122 else if (((dir == LEFT) && (current_x_velocity < dest_x_velocity))
123 || ((dir == RIGHT) && (current_x_velocity > dest_x_velocity)))
125 /* acceleration == walk-speed => it will take one second to get twice the
126 * speed to normal speed. */
127 physic.set_acceleration_x ((-1.0) * dest_x_velocity);
131 /* The above should have covered all cases. */
135 if (max_drop_height > -1) {
136 if (on_ground() && might_fall(max_drop_height+1))
144 WalkingBadguy::collision_solid(const CollisionHit& hit)
147 update_on_ground_flag(hit);
150 if (physic.get_velocity_y() < 0) physic.set_velocity_y(0);
153 if (physic.get_velocity_y() > 0) physic.set_velocity_y(0);
156 if ((hit.left && (hit.slope_normal.y == 0) && (dir == LEFT)) || (hit.right && (hit.slope_normal.y == 0) && (dir == RIGHT))) {
163 WalkingBadguy::collision_badguy(BadGuy& , const CollisionHit& hit)
166 if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) {
174 WalkingBadguy::turn_around()
178 dir = dir == LEFT ? RIGHT : LEFT;
179 sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
180 physic.set_velocity_x(-physic.get_velocity_x());
181 physic.set_acceleration_x (-physic.get_acceleration_x ());
183 // if we get dizzy, we fall off the screen
184 if (turn_around_timer.started()) {
185 if (turn_around_counter++ > 10) kill_fall();
187 turn_around_timer.start(1);
188 turn_around_counter = 0;
194 WalkingBadguy::freeze()
197 physic.set_velocity_x(0);
201 WalkingBadguy::unfreeze()
204 WalkingBadguy::initialize();
208 WalkingBadguy::get_velocity_y() const
210 return physic.get_velocity_y();
214 WalkingBadguy::set_velocity_y(float vy)
216 physic.set_velocity_y(vy);