From e7b3e730e595b19c0c9e5a72caca1d256c106ac9 Mon Sep 17 00:00:00 2001 From: florianf Date: Tue, 2 Mar 2010 11:20:33 +0000 Subject: [PATCH] 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). git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6508 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/badguy/walking_badguy.cpp | 26 ++++++++++++++++++++++++-- src/badguy/walking_badguy.hpp | 5 +++++ 2 files changed, 29 insertions(+), 2 deletions(-) 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); -- 2.11.0