Plant Class Reference

#include <plant.hpp>

Inherits BadGuy.

List of all members.

Public Member Functions

 Plant (const Reader &reader)
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.
void active_update (float elapsed_time)
 called each frame when the badguy is activated.

Protected Types

enum  PlantState { PLANT_SLEEPING, PLANT_WAKING, PLANT_WALKING }

Private Attributes

Timer timer
PlantState state


Detailed Description

Definition at line 22 of file plant.hpp.


Member Enumeration Documentation

enum Plant::PlantState [protected]

Enumerator:
PLANT_SLEEPING 
PLANT_WAKING 
PLANT_WALKING 

Definition at line 33 of file plant.hpp.

00033                   {
00034     PLANT_SLEEPING,
00035     PLANT_WAKING,
00036     PLANT_WALKING
00037   };


Constructor & Destructor Documentation

Plant::Plant ( const Reader reader  ) 

Definition at line 26 of file plant.cpp.

References PLANT_SLEEPING, and state.

00026                                  :
00027   BadGuy(reader, "images/creatures/plant/plant.sprite"),
00028   timer(),
00029   state()
00030 {
00031   state = PLANT_SLEEPING;
00032 }


Member Function Documentation

void Plant::initialize (  )  [virtual]

called immediately before the first call to initialize

Reimplemented from BadGuy.

Definition at line 35 of file plant.cpp.

References BadGuy::dir, LEFT, BadGuy::physic, PLANT_SLEEPING, RIGHT, Physic::set_velocity_x(), MovingSprite::sprite, and state.

00036 {
00037   //FIXME: turns sspiky around for debugging
00038   dir = dir == LEFT ? RIGHT : LEFT;
00039 
00040   state = PLANT_SLEEPING;
00041   physic.set_velocity_x(0);
00042   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
00043 }

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

Called when the badguy collided with solid ground.

Reimplemented from BadGuy.

Definition at line 46 of file plant.cpp.

References CollisionHit::bottom, BadGuy::dir, Physic::get_velocity_x(), LEFT, CollisionHit::left, BadGuy::physic, RIGHT, CollisionHit::right, Physic::set_velocity_x(), Physic::set_velocity_y(), MovingSprite::sprite, and CollisionHit::top.

00047 {
00048   if(hit.top || hit.bottom) {
00049     physic.set_velocity_y(0);
00050   } else if(hit.left || hit.right) {
00051     dir = dir == LEFT ? RIGHT : LEFT;
00052     sprite->set_action(dir == LEFT ? "left" : "right");
00053     physic.set_velocity_x(-physic.get_velocity_x());
00054   }
00055 }

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

Called when the badguy collided with another badguy.

Reimplemented from BadGuy.

Definition at line 58 of file plant.cpp.

References CONTINUE, BadGuy::dir, Physic::get_velocity_x(), LEFT, CollisionHit::left, BadGuy::physic, PLANT_WALKING, RIGHT, CollisionHit::right, Physic::set_velocity_x(), MovingSprite::sprite, and state.

00059 {
00060   if(state != PLANT_WALKING) return CONTINUE;
00061 
00062   if(hit.left || hit.right) {
00063     dir = dir == LEFT ? RIGHT : LEFT;
00064     sprite->set_action(dir == LEFT ? "left" : "right");
00065     physic.set_velocity_x(-physic.get_velocity_x());
00066   }
00067 
00068   return CONTINUE;
00069 }

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

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Definition at line 72 of file plant.cpp.

References BadGuy::active_update(), Timer::check(), BadGuy::dir, MovingObject::get_bbox(), BadGuy::get_nearest_player(), LEFT, Rectf::p1, Rectf::p2, BadGuy::physic, PLANT_SLEEPING, PLANT_SPEED, PLANT_WAKING, PLANT_WALKING, RIGHT, Physic::set_velocity_x(), MovingSprite::sprite, Timer::start(), Timer::started(), state, timer, WAKE_TIME, Vector::x, and Vector::y.

00072                                        {
00073   BadGuy::active_update(elapsed_time);
00074 
00075   if(state == PLANT_SLEEPING) {
00076 
00077     Player* player = this->get_nearest_player();
00078     if (player) {
00079       Rectf mb = this->get_bbox();
00080       Rectf pb = player->get_bbox();
00081 
00082       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
00083       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
00084       bool inReach_top = (pb.p2.y >= mb.p2.y);
00085       bool inReach_bottom = (pb.p1.y <= mb.p1.y);
00086 
00087       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
00088         // wake up
00089         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right");
00090         if(!timer.started()) timer.start(WAKE_TIME);
00091         state = PLANT_WAKING;
00092       }
00093     }
00094   }
00095 
00096   if(state == PLANT_WAKING) {
00097     if(timer.check()) {
00098       // start walking
00099       sprite->set_action(dir == LEFT ? "left" : "right");
00100       physic.set_velocity_x(dir == LEFT ? -PLANT_SPEED : PLANT_SPEED);
00101       state = PLANT_WALKING;
00102     }
00103   }
00104 
00105 }


Member Data Documentation

Timer Plant::timer [private]

Definition at line 40 of file plant.hpp.

Referenced by active_update().

PlantState Plant::state [private]

Reimplemented from BadGuy.

Definition at line 41 of file plant.hpp.

Referenced by active_update(), collision_badguy(), initialize(), and Plant().


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