WalkingBadguy Class Reference

Base class for Badguys that walk on the floor. More...

#include <walking_badguy.hpp>

Inherits BadGuy.

Inherited by CaptainSnowball, Crystallo, Haywire, Igel, MrBomb, MrIceBlock, MrTree, PoisonIvy, ShortFuse, SmartBall, Snail, SnowBall, Snowman, Spiky, SSpiky, Stumpy, and WalkingLeaf.

List of all members.

Public Member Functions

 WalkingBadguy (const Vector &pos, const std::string &sprite_name, const std::string &walk_left_action, const std::string &walk_right_action, int layer=LAYER_OBJECTS)
 WalkingBadguy (const Vector &pos, Direction direction, const std::string &sprite_name, const std::string &walk_left_action, const std::string &walk_right_action, int layer=LAYER_OBJECTS)
 WalkingBadguy (const Reader &reader, const std::string &sprite_name, const std::string &walk_left_action, const std::string &walk_right_action, int layer=LAYER_OBJECTS)
void initialize ()
 called immediately before the first call to initialize
void active_update (float elapsed_time)
 called each frame when the badguy is activated.
void active_update (float elapsed_time, float target_velocity)
void collision_solid (const CollisionHit &hit)
 Called when the badguy collided with solid ground.
HitResponse collision_badguy (BadGuy &badguy, const CollisionHit &hit)
 Called when the badguy collided with another badguy.
void freeze ()
 Called when hit by an ice bullet, and is_freezable() returns true.
void unfreeze ()
 Called to unfreeze the badguy.
float get_velocity_y () const
void set_velocity_y (float vy)
void add_velocity (const Vector &velocity)
 Adds velocity to the badguy (be careful when using this).
float get_walk_speed (void) const
void set_walk_speed (float)

Protected Member Functions

void turn_around ()

Protected Attributes

std::string walk_left_action
std::string walk_right_action
float walk_speed
int max_drop_height
 Maximum height of drop before we will turn around, or -1 to just drop from any ledge.
Timer turn_around_timer
int turn_around_counter
 counts number of turns since turn_around_timer was started


Detailed Description

Base class for Badguys that walk on the floor.

Definition at line 27 of file walking_badguy.hpp.


Constructor & Destructor Documentation

WalkingBadguy::WalkingBadguy ( const Vector pos,
const std::string &  sprite_name,
const std::string &  walk_left_action,
const std::string &  walk_right_action,
int  layer = LAYER_OBJECTS 
)

Definition at line 23 of file walking_badguy.cpp.

00027                                         :
00028   BadGuy(pos, sprite_name, layer), 
00029   walk_left_action(walk_left_action), 
00030   walk_right_action(walk_right_action), 
00031   walk_speed(80), 
00032   max_drop_height(-1),
00033   turn_around_timer(),
00034   turn_around_counter()
00035 {
00036 }

WalkingBadguy::WalkingBadguy ( const Vector pos,
Direction  direction,
const std::string &  sprite_name,
const std::string &  walk_left_action,
const std::string &  walk_right_action,
int  layer = LAYER_OBJECTS 
)

Definition at line 38 of file walking_badguy.cpp.

00043                                         :
00044   BadGuy(pos, direction, sprite_name, layer), 
00045   walk_left_action(walk_left_action), 
00046   walk_right_action(walk_right_action), 
00047   walk_speed(80), 
00048   max_drop_height(-1),
00049   turn_around_timer(),
00050   turn_around_counter()
00051 {
00052 }

WalkingBadguy::WalkingBadguy ( const Reader reader,
const std::string &  sprite_name,
const std::string &  walk_left_action,
const std::string &  walk_right_action,
int  layer = LAYER_OBJECTS 
)

Definition at line 54 of file walking_badguy.cpp.

00058                                         :
00059   BadGuy(reader, sprite_name, layer), 
00060   walk_left_action(walk_left_action), 
00061   walk_right_action(walk_right_action), 
00062   walk_speed(80), 
00063   max_drop_height(-1),
00064   turn_around_timer(),
00065   turn_around_counter()
00066 {
00067 }


Member Function Documentation

void WalkingBadguy::initialize (  )  [virtual]

called immediately before the first call to initialize

Reimplemented from BadGuy.

Reimplemented in MrIceBlock, Snail, SSpiky, and Stumpy.

Definition at line 70 of file walking_badguy.cpp.

References MovingObject::bbox, BadGuy::dir, BadGuy::frozen, LEFT, BadGuy::physic, Physic::set_acceleration_x(), Rectf::set_size(), Physic::set_velocity_x(), MovingSprite::sprite, walk_left_action, walk_right_action, and walk_speed.

Referenced by Stumpy::active_update(), SSpiky::active_update(), Snail::be_normal(), Igel::be_normal(), Stumpy::initialize(), Snail::initialize(), MrIceBlock::initialize(), MrIceBlock::set_state(), and unfreeze().

00071 {
00072   if(frozen)
00073     return;
00074   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
00075   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00076   physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
00077   physic.set_acceleration_x (0.0);
00078 }

void WalkingBadguy::active_update ( float  elapsed_time  )  [virtual]

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Reimplemented in CaptainSnowball, Crystallo, Haywire, Igel, MrBomb, MrIceBlock, Snail, SSpiky, and Stumpy.

Definition at line 152 of file walking_badguy.cpp.

References BadGuy::dir, LEFT, and walk_speed.

Referenced by Stumpy::active_update(), SSpiky::active_update(), Snail::active_update(), MrIceBlock::active_update(), MrBomb::active_update(), Igel::active_update(), Haywire::active_update(), and CaptainSnowball::active_update().

00153 {
00154   this->active_update (elapsed_time, (dir == LEFT) ? -walk_speed : +walk_speed);
00155 }

void WalkingBadguy::active_update ( float  elapsed_time,
float  target_velocity 
)

Definition at line 94 of file walking_badguy.cpp.

References BadGuy::active_update(), BadGuy::dir, BadGuy::frozen, Physic::get_velocity_x(), LEFT, max_drop_height, BadGuy::might_fall(), BadGuy::on_ground(), BadGuy::physic, RIGHT, Physic::set_acceleration_x(), MovingSprite::set_action(), Physic::set_velocity_x(), turn_around(), walk_left_action, and walk_right_action.

00095 {
00096   BadGuy::active_update(elapsed_time);
00097 
00098   float current_x_velocity = physic.get_velocity_x ();
00099 
00100   if (frozen)
00101   {
00102     physic.set_velocity_x (0.0);
00103     physic.set_acceleration_x (0.0);
00104   }
00105   /* We're very close to our target speed. Just set it to avoid oscillation */
00106   else if ((current_x_velocity > (dest_x_velocity - 5.0))
00107       && (current_x_velocity < (dest_x_velocity + 5.0)))
00108   {
00109     physic.set_velocity_x (dest_x_velocity);
00110     physic.set_acceleration_x (0.0);
00111   }
00112   /* Check if we're going too slow or even in the wrong direction */
00113   else if (((dest_x_velocity <= 0.0) && (current_x_velocity > dest_x_velocity))
00114       || ((dest_x_velocity > 0.0) && (current_x_velocity < dest_x_velocity)))
00115   {
00116     /* acceleration == walk-speed => it will take one second to get from zero
00117      * to full speed. */
00118     physic.set_acceleration_x (dest_x_velocity);
00119   }
00120   /* Check if we're going too fast */
00121   else if (((dest_x_velocity <= 0.0) && (current_x_velocity < dest_x_velocity))
00122       || ((dest_x_velocity > 0.0) && (current_x_velocity > dest_x_velocity)))
00123   {
00124     /* acceleration == walk-speed => it will take one second to get twice the
00125      * speed to normal speed. */
00126     physic.set_acceleration_x ((-1.0) * dest_x_velocity);
00127   }
00128   else
00129   {
00130     /* The above should have covered all cases. */
00131     assert (23 == 42);
00132   }
00133 
00134   if (max_drop_height > -1) {
00135     if (on_ground() && might_fall(max_drop_height+1))
00136     {
00137       turn_around();
00138     }
00139   }
00140 
00141   if ((dir == LEFT) && (physic.get_velocity_x () > 0.0)) {
00142     dir = RIGHT;
00143     set_action (walk_right_action, /* loops = */ -1);
00144   }
00145   else if ((dir == RIGHT) && (physic.get_velocity_x () < 0.0)) {
00146     dir = LEFT;
00147     set_action (walk_left_action, /* loops = */ -1);
00148   }
00149 }

void WalkingBadguy::collision_solid ( const CollisionHit hit  )  [virtual]

Called when the badguy collided with solid ground.

Reimplemented from BadGuy.

Reimplemented in CaptainSnowball, MrIceBlock, Snail, SSpiky, and Stumpy.

Definition at line 158 of file walking_badguy.cpp.

References CollisionHit::bottom, BadGuy::dir, Physic::get_velocity_y(), LEFT, CollisionHit::left, BadGuy::physic, RIGHT, CollisionHit::right, Physic::set_velocity_y(), CollisionHit::slope_normal, CollisionHit::top, turn_around(), BadGuy::update_on_ground_flag(), and Vector::y.

Referenced by Stumpy::collision_solid(), SSpiky::collision_solid(), Snail::collision_solid(), MrIceBlock::collision_solid(), and CaptainSnowball::collision_solid().

00159 {
00160 
00161   update_on_ground_flag(hit);
00162 
00163   if (hit.top) {
00164     if (physic.get_velocity_y() < 0) physic.set_velocity_y(0);
00165   }
00166   if (hit.bottom) {
00167     if (physic.get_velocity_y() > 0) physic.set_velocity_y(0);
00168   }
00169 
00170   if ((hit.left && (hit.slope_normal.y == 0) && (dir == LEFT)) || (hit.right && (hit.slope_normal.y == 0) && (dir == RIGHT))) {
00171     turn_around();
00172   }
00173 
00174 }

HitResponse WalkingBadguy::collision_badguy ( BadGuy badguy,
const CollisionHit hit 
) [virtual]

Called when the badguy collided with another badguy.

Reimplemented from BadGuy.

Reimplemented in MrIceBlock, Snail, SSpiky, and Stumpy.

Definition at line 177 of file walking_badguy.cpp.

References CONTINUE, BadGuy::dir, LEFT, CollisionHit::left, RIGHT, CollisionHit::right, and turn_around().

Referenced by Stumpy::collision_badguy(), SSpiky::collision_badguy(), Snail::collision_badguy(), and MrIceBlock::collision_badguy().

00178 {
00179 
00180   if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) {
00181     turn_around();
00182   }
00183 
00184   return CONTINUE;
00185 }

void WalkingBadguy::freeze (  )  [virtual]

Called when hit by an ice bullet, and is_freezable() returns true.

Reimplemented from BadGuy.

Reimplemented in Haywire, MrBomb, Spiky, and SSpiky.

Definition at line 208 of file walking_badguy.cpp.

References BadGuy::freeze(), BadGuy::physic, and Physic::set_velocity_x().

Referenced by SSpiky::freeze(), Spiky::freeze(), MrBomb::freeze(), and Haywire::freeze().

00209 {
00210   BadGuy::freeze();
00211   physic.set_velocity_x(0);
00212 }

void WalkingBadguy::unfreeze (  )  [virtual]

Called to unfreeze the badguy.

Reimplemented from BadGuy.

Definition at line 215 of file walking_badguy.cpp.

References initialize(), and BadGuy::unfreeze().

00216 {
00217   BadGuy::unfreeze();
00218   WalkingBadguy::initialize();
00219 }

float WalkingBadguy::get_velocity_y (  )  const

Definition at line 222 of file walking_badguy.cpp.

References Physic::get_velocity_y(), and BadGuy::physic.

Referenced by Trampoline::collision().

00223 {
00224   return physic.get_velocity_y();
00225 }

void WalkingBadguy::set_velocity_y ( float  vy  ) 

Definition at line 228 of file walking_badguy.cpp.

References BadGuy::physic, and Physic::set_velocity_y().

Referenced by Trampoline::collision().

00229 {
00230   physic.set_velocity_y(vy);
00231 }

void WalkingBadguy::add_velocity ( const Vector velocity  ) 

Adds velocity to the badguy (be careful when using this).

Definition at line 88 of file walking_badguy.cpp.

References Physic::get_velocity(), BadGuy::physic, and Physic::set_velocity().

Referenced by Explosion::explode().

00089 {
00090   physic.set_velocity(physic.get_velocity() + velocity);
00091 }

float WalkingBadguy::get_walk_speed ( void   )  const [inline]

Definition at line 62 of file walking_badguy.hpp.

References walk_speed.

00063   {
00064     return (walk_speed);
00065   }

void WalkingBadguy::set_walk_speed ( float   ) 

Definition at line 81 of file walking_badguy.cpp.

References walk_speed.

Referenced by Haywire::collision_squished().

00082 {
00083   walk_speed = fabs (ws);
00084   /* physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed); */
00085 }

void WalkingBadguy::turn_around (  )  [protected]

Reimplemented in Igel.

Definition at line 188 of file walking_badguy.cpp.

References BadGuy::dir, BadGuy::frozen, Physic::get_acceleration_x(), Physic::get_velocity_x(), BadGuy::kill_fall(), LEFT, BadGuy::physic, RIGHT, Physic::set_acceleration_x(), Physic::set_velocity_x(), MovingSprite::sprite, Timer::start(), Timer::started(), turn_around_counter, turn_around_timer, walk_left_action, and walk_right_action.

Referenced by active_update(), Crystallo::active_update(), collision_badguy(), collision_solid(), and Igel::turn_around().

00189 {
00190   if(frozen)
00191     return;
00192   dir = dir == LEFT ? RIGHT : LEFT;
00193   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
00194   physic.set_velocity_x(-physic.get_velocity_x());
00195   physic.set_acceleration_x (-physic.get_acceleration_x ());
00196 
00197   // if we get dizzy, we fall off the screen
00198   if (turn_around_timer.started()) {
00199     if (turn_around_counter++ > 10) kill_fall();
00200   } else {
00201     turn_around_timer.start(1);
00202     turn_around_counter = 0;
00203   }
00204 
00205 }


Member Data Documentation

std::string WalkingBadguy::walk_left_action [protected]

Definition at line 72 of file walking_badguy.hpp.

Referenced by active_update(), Haywire::collision_squished(), initialize(), and turn_around().

std::string WalkingBadguy::walk_right_action [protected]

Definition at line 73 of file walking_badguy.hpp.

Referenced by active_update(), Haywire::collision_squished(), initialize(), and turn_around().

float WalkingBadguy::walk_speed [protected]

Definition at line 74 of file walking_badguy.hpp.

Referenced by active_update(), Snail::active_update(), Haywire::active_update(), CaptainSnowball::active_update(), CaptainSnowball::CaptainSnowball(), MrIceBlock::collision_solid(), CaptainSnowball::collision_solid(), Crystallo::Crystallo(), get_walk_speed(), Haywire::Haywire(), Igel::Igel(), initialize(), MrBomb::MrBomb(), MrIceBlock::MrIceBlock(), MrTree::MrTree(), PoisonIvy::PoisonIvy(), set_walk_speed(), ShortFuse::ShortFuse(), SmartBall::SmartBall(), Snail::Snail(), SnowBall::SnowBall(), Snowman::Snowman(), Spiky::Spiky(), SSpiky::SSpiky(), Stumpy::Stumpy(), and WalkingLeaf::WalkingLeaf().

int WalkingBadguy::max_drop_height [protected]

Maximum height of drop before we will turn around, or -1 to just drop from any ledge.

Definition at line 75 of file walking_badguy.hpp.

Referenced by active_update(), CaptainSnowball::CaptainSnowball(), Crystallo::Crystallo(), Haywire::Haywire(), Igel::Igel(), MrBomb::MrBomb(), MrIceBlock::MrIceBlock(), MrTree::MrTree(), ShortFuse::ShortFuse(), SmartBall::SmartBall(), Snail::Snail(), Spiky::Spiky(), SSpiky::SSpiky(), Stumpy::Stumpy(), and WalkingLeaf::WalkingLeaf().

Timer WalkingBadguy::turn_around_timer [protected]

Definition at line 76 of file walking_badguy.hpp.

Referenced by turn_around().

int WalkingBadguy::turn_around_counter [protected]

counts number of turns since turn_around_timer was started

Definition at line 77 of file walking_badguy.hpp.

Referenced by turn_around().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:38 2014 for SuperTux by  doxygen 1.5.1