Skip to content

Commit

Permalink
badguy/walking_badguy.[ch]pp: Added the add_velocity() method.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Florian Forster committed Mar 2, 2010
1 parent 52a3f84 commit cc40de7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/badguy/walking_badguy.cpp
Expand Up @@ -74,27 +74,48 @@ 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
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
Expand Down Expand Up @@ -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()) {
Expand Down
5 changes: 5 additions & 0 deletions src/badguy/walking_badguy.hpp
Expand Up @@ -53,6 +53,11 @@ class WalkingBadguy : public BadGuy
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);
Expand Down

0 comments on commit cc40de7

Please sign in to comment.