Toad Class Reference

Badguy "Toad" - A jumping toad. More...

#include <toad.hpp>

Inherits BadGuy.

List of all members.

Public Member Functions

 Toad (const Reader &reader)
 Toad (const Vector &pos, Direction d)
void initialize ()
 called immediately before the first call to initialize
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.
bool collision_squished (GameObject &object)
 Called when the player hit the badguy from above.
void active_update (float elapsed_time)
 called each frame when the badguy is activated.

Protected Types

enum  ToadState { IDLE, JUMPING, FALLING }

Private Member Functions

void set_state (ToadState newState)

Private Attributes

Timer recover_timer
ToadState state


Detailed Description

Badguy "Toad" - A jumping toad.

Definition at line 25 of file toad.hpp.


Member Enumeration Documentation

enum Toad::ToadState [protected]

Enumerator:
IDLE 
JUMPING 
FALLING 

Definition at line 38 of file toad.hpp.

00038                  {
00039     IDLE,
00040     JUMPING,
00041     FALLING
00042   };


Constructor & Destructor Documentation

Toad::Toad ( const Reader reader  ) 

Definition at line 31 of file toad.cpp.

References HOP_SOUND, SoundManager::preload(), and sound_manager.

00031                                :
00032   BadGuy(reader, "images/creatures/toad/toad.sprite"),
00033   recover_timer(),
00034   state()
00035 {
00036   sound_manager->preload(HOP_SOUND);
00037 }

Toad::Toad ( const Vector pos,
Direction  d 
)

Definition at line 39 of file toad.cpp.

References HOP_SOUND, SoundManager::preload(), and sound_manager.

00039                                          :
00040   BadGuy(pos, d, "images/creatures/toad/toad.sprite"),
00041   recover_timer(),
00042   state()
00043 {
00044   sound_manager->preload(HOP_SOUND);
00045 }


Member Function Documentation

void Toad::initialize (  )  [virtual]

called immediately before the first call to initialize

Reimplemented from BadGuy.

Definition at line 48 of file toad.cpp.

References BadGuy::dir, JUMPING, LEFT, MovingSprite::sprite, and state.

00049 {
00050   // initial state is JUMPING, because we might start airborne
00051   state = JUMPING;
00052   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00053 }

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

Called when the badguy collided with solid ground.

Reimplemented from BadGuy.

Definition at line 91 of file toad.cpp.

References CollisionHit::bottom, BadGuy::collision_solid(), FALLING, BadGuy::get_state(), Physic::get_velocity_x(), IDLE, JUMPING, CollisionHit::left, BadGuy::physic, CollisionHit::right, set_state(), Physic::set_velocity_x(), Physic::set_velocity_y(), state, BadGuy::STATE_SQUISHED, and CollisionHit::top.

Referenced by collision_badguy().

00092 {
00093   // just default behaviour (i.e. stop at floor/walls) when squished
00094   if (BadGuy::get_state() == STATE_SQUISHED) {
00095     BadGuy::collision_solid(hit);
00096     return;
00097   }
00098 
00099   // ignore collisions while standing still
00100   if(state == IDLE) {
00101     return;
00102   }
00103 
00104   // check if we hit left or right while moving in either direction
00105   if(((physic.get_velocity_x() < 0) && hit.left) || ((physic.get_velocity_x() > 0) && hit.right)) {
00106     /*
00107       dir = dir == LEFT ? RIGHT : LEFT;
00108       if (state == JUMPING) {
00109       sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00110       } else {
00111       sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00112       }
00113     */
00114     physic.set_velocity_x(-0.25*physic.get_velocity_x());
00115   }
00116 
00117   // check if we hit the floor while falling
00118   if ((state == FALLING) && hit.bottom) {
00119     set_state(IDLE);
00120     return;
00121   }
00122 
00123   // check if we hit the roof while climbing
00124   if ((state == JUMPING) && hit.top) {
00125     physic.set_velocity_y(0);
00126   }
00127 
00128 }

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

Called when the badguy collided with another badguy.

Reimplemented from BadGuy.

Definition at line 131 of file toad.cpp.

References collision_solid(), and CONTINUE.

00132 {
00133   // behaviour for badguy collisions is the same as for collisions with solids
00134   collision_solid(hit);
00135 
00136   return CONTINUE;
00137 }

bool Toad::collision_squished ( GameObject object  )  [virtual]

Called when the player hit the badguy from above.

You should return true if the badguy was squished, false if squishing wasn't possible

Reimplemented from BadGuy.

Definition at line 83 of file toad.cpp.

References BadGuy::dir, BadGuy::kill_squished(), LEFT, and MovingSprite::sprite.

00084 {
00085   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00086   kill_squished(object);
00087   return true;
00088 }

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

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Definition at line 140 of file toad.cpp.

References BadGuy::active_update(), Timer::check(), FALLING, Physic::get_velocity_y(), IDLE, JUMPING, BadGuy::physic, recover_timer, set_state(), and state.

00141 {
00142   BadGuy::active_update(elapsed_time);
00143 
00144   // change sprite when we are falling
00145   if ((state == JUMPING) && (physic.get_velocity_y() > 0)) {
00146     set_state(FALLING);
00147     return;
00148   }
00149 
00150   // jump when fully recovered
00151   if ((state == IDLE) && (recover_timer.check())) {
00152     set_state(JUMPING);
00153     return;
00154   }
00155 
00156 }

void Toad::set_state ( ToadState  newState  )  [private]

Definition at line 56 of file toad.cpp.

References BadGuy::dir, FALLING, MovingObject::get_bbox(), BadGuy::get_nearest_player(), MovingObject::get_pos(), HOP_SOUND, HORIZONTAL_SPEED, IDLE, JUMPING, LEFT, Rectf::p1, Rectf::p2, BadGuy::physic, SoundManager::play(), recover_timer, RIGHT, Physic::set_velocity_x(), Physic::set_velocity_y(), sound_manager, MovingSprite::sprite, Timer::start(), state, TOAD_RECOVER_TIME, VERTICAL_SPEED, and Vector::x.

Referenced by active_update(), and collision_solid().

00057 {
00058   if (newState == IDLE) {
00059     physic.set_velocity_x(0);
00060     physic.set_velocity_y(0);
00061     sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00062 
00063     recover_timer.start(TOAD_RECOVER_TIME);
00064   } else
00065     if (newState == JUMPING) {
00066       sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
00067       physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
00068       physic.set_velocity_y(VERTICAL_SPEED);
00069       sound_manager->play( HOP_SOUND, get_pos());
00070     } else
00071       if (newState == FALLING) {
00072         Player* player = get_nearest_player();
00073         // face player
00074         if (player && (player->get_bbox().p2.x < get_bbox().p1.x) && (dir == RIGHT)) dir = LEFT;
00075         if (player && (player->get_bbox().p1.x > get_bbox().p2.x) && (dir == LEFT)) dir = RIGHT;
00076         sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
00077       }
00078 
00079   state = newState;
00080 }


Member Data Documentation

Timer Toad::recover_timer [private]

Definition at line 48 of file toad.hpp.

Referenced by active_update(), and set_state().

ToadState Toad::state [private]

Reimplemented from BadGuy.

Definition at line 49 of file toad.hpp.

Referenced by active_update(), collision_solid(), initialize(), and set_state().


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