From: Florian Forster Date: Tue, 2 Mar 2010 11:20:33 +0000 (+0000) Subject: badguy/walking_badguy.[ch]pp: Added the add_velocity() method. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=cc40de7c80476cbc86d8bbf4447fc3cc3809adc9;p=supertux.git badguy/walking_badguy.[ch]pp: Added the add_velocity() method. The speed is simply added to the current velocity of the badguy. The walking speed is regained using an acceleration of walk_speed/s, i.e. it takes one second to get from zero to full speed again (or from twice the speed back to normal). SVN-Revision: 6508 --- diff --git a/src/badguy/walking_badguy.cpp b/src/badguy/walking_badguy.cpp index e92be3f09..d5e34264b 100644 --- a/src/badguy/walking_badguy.cpp +++ b/src/badguy/walking_badguy.cpp @@ -74,13 +74,20 @@ WalkingBadguy::initialize() sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action); bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); + physic.set_acceleration_x (0.0); } void WalkingBadguy::set_walk_speed (float ws) { walk_speed = fabs (ws); - physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); + /* physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); */ +} + +void +WalkingBadguy::add_velocity (const Vector& velocity) +{ + physic.set_velocity(physic.get_velocity() + velocity); } void @@ -88,13 +95,27 @@ WalkingBadguy::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); + float abs_cur_walk_speed = fabs (physic.get_velocity_x ()); + if ((abs_cur_walk_speed > (walk_speed - 5.0)) + && (abs_cur_walk_speed < (walk_speed + 5.0))) + { + physic.set_velocity_x ((dir == LEFT) ? -walk_speed : +walk_speed); + physic.set_acceleration_x (0.0); + } + /* acceleration == walk-speed => it will take one second to get from zero to full speed. */ + else if (abs_cur_walk_speed < walk_speed) { + physic.set_acceleration_x ((dir == LEFT) ? -walk_speed : +walk_speed); + } + else if (abs_cur_walk_speed > walk_speed) { + physic.set_acceleration_x ((dir == LEFT) ? +walk_speed : -walk_speed); + } + if (max_drop_height > -1) { if (on_ground() && might_fall(max_drop_height+1)) { turn_around(); } } - } void @@ -135,6 +156,7 @@ WalkingBadguy::turn_around() dir = dir == LEFT ? RIGHT : LEFT; sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action); physic.set_velocity_x(-physic.get_velocity_x()); + physic.set_acceleration_x (-physic.get_acceleration_x ()); // if we get dizzy, we fall off the screen if (turn_around_timer.started()) { diff --git a/src/badguy/walking_badguy.hpp b/src/badguy/walking_badguy.hpp index e7da43820..4bfb4556a 100644 --- a/src/badguy/walking_badguy.hpp +++ b/src/badguy/walking_badguy.hpp @@ -53,6 +53,11 @@ public: float get_velocity_y() const; void set_velocity_y(float vy); + /** + * Adds velocity to the badguy (be careful when using this) + */ + void add_velocity(const Vector& velocity); + float get_walk_speed (void) const { return (walk_speed);