Totem Class Reference

"Totem" Badguy - A variable-height stack of wooden blocks More...

#include <totem.hpp>

Inherits BadGuy.

List of all members.

Public Member Functions

 Totem (const Reader &reader)
 ~Totem ()
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 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.
virtual bool updatePointers (const GameObject *from_object, GameObject *to_object)

Protected Member Functions

bool collision_squished (GameObject &object)
 Called when the player hit the badguy from above.
void kill_fall ()
 Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned upside-down).
void jump_on (Totem *target)
 jump on target
void jump_off ()
 jump off current base
void synchronize_with (Totem *baseTotem)
 synchronize position and movement with baseTotem

Private Member Functions

 Totem (const Totem &)
Totemoperator= (const Totem &)

Private Attributes

Totemcarrying
 Totem we are currently carrying (or 0).
Totemcarried_by
 Totem by which we are currently carried (or 0).


Detailed Description

"Totem" Badguy - A variable-height stack of wooden blocks

Definition at line 25 of file totem.hpp.


Constructor & Destructor Documentation

Totem::Totem ( const Reader reader  ) 

Definition at line 31 of file totem.cpp.

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

00031                                  :
00032   BadGuy(reader, "images/creatures/totem/totem.sprite"),
00033   carrying(0),
00034   carried_by(0)
00035 {
00036   sound_manager->preload( LAND_ON_TOTEM_SOUND );
00037 }

Totem::~Totem (  ) 

Definition at line 39 of file totem.cpp.

References carried_by, carrying, and jump_off().

00040 {
00041   if (carrying) carrying->jump_off();
00042   if (carried_by) jump_off();
00043 }

Totem::Totem ( const Totem  )  [private]


Member Function Documentation

void Totem::initialize (  )  [virtual]

called immediately before the first call to initialize

Reimplemented from BadGuy.

Definition at line 60 of file totem.cpp.

References carried_by, BadGuy::dir, LEFT, BadGuy::physic, Physic::set_velocity_x(), MovingSprite::sprite, synchronize_with(), and WALKSPEED.

Referenced by active_update(), collision_badguy(), collision_solid(), jump_off(), and jump_on().

00061 {
00062   if (!carried_by) {
00063 static const float WALKSPEED = 100;
00064     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
00065     sprite->set_action(dir == LEFT ? "walking-left" : "walking-right");
00066     return;
00067   } else {
00068     synchronize_with(carried_by);
00069     sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
00070     return;
00071   }
00072 }

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

called each frame when the badguy is activated.

Reimplemented from BadGuy.

Definition at line 75 of file totem.cpp.

References BadGuy::active_update(), carried_by, carrying, Sector::current(), BadGuy::dir, MovingObject::get_pos(), initialize(), JUMP_ON_SPEED_Y, LEFT, BadGuy::might_fall(), Sector::moving_objects, BadGuy::on_ground(), BadGuy::physic, RIGHT, MovingObject::set_pos(), Physic::set_velocity_y(), synchronize_with(), Vector::x, and Vector::y.

00076 {
00077   BadGuy::active_update(elapsed_time);
00078 
00079   if (!carried_by) {
00080     if (on_ground() && might_fall())
00081     {
00082       dir = (dir == LEFT ? RIGHT : LEFT);
00083       initialize();
00084     }
00085 
00086     Sector* s = Sector::current();
00087     if (s) {
00088       // jump a bit if we find a suitable totem
00089       for (std::vector<MovingObject*>::iterator i = s->moving_objects.begin(); i != s->moving_objects.end(); i++) {
00090         Totem* t = dynamic_cast<Totem*>(*i);
00091         if (!t) continue;
00092 
00093         // skip if we are not approaching each other
00094         if (!((this->dir == LEFT) && (t->dir == RIGHT))) continue;
00095 
00096         Vector p1 = this->get_pos();
00097         Vector p2 = t->get_pos();
00098 
00099         // skip if not on same height
00100         float dy = (p1.y - p2.y);
00101         if (fabsf(dy - 0) > 2) continue;
00102 
00103         // skip if too far away
00104         float dx = (p1.x - p2.x);
00105         if (fabsf(dx - 128) > 2) continue;
00106 
00107         physic.set_velocity_y(JUMP_ON_SPEED_Y);
00108         p1.y -= 1;
00109         this->set_pos(p1);
00110         break;
00111       }
00112     }
00113   }
00114 
00115   if (carried_by) {
00116     this->synchronize_with(carried_by);
00117   }
00118 
00119   if (carrying) {
00120     carrying->synchronize_with(this);
00121   }
00122 
00123 }

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

Called when the badguy collided with solid ground.

Reimplemented from BadGuy.

Definition at line 143 of file totem.cpp.

References CollisionHit::bottom, carried_by, collision_solid(), BadGuy::dir, initialize(), LEFT, CollisionHit::left, BadGuy::physic, CollisionHit::right, RIGHT, Physic::set_velocity_y(), CollisionHit::top, and BadGuy::update_on_ground_flag().

Referenced by collision_solid().

00144 {
00145   update_on_ground_flag(hit);
00146 
00147   // if we are being carried around, pass event to bottom of stack and ignore it
00148   if (carried_by) {
00149     carried_by->collision_solid(hit);
00150     return;
00151   }
00152 
00153   // If we hit something from above or below: stop moving in this direction
00154   if (hit.top || hit.bottom) {
00155     physic.set_velocity_y(0);
00156   }
00157 
00158   // If we are hit from the direction we are facing: turn around
00159   if (hit.left && (dir == LEFT)) {
00160     dir = RIGHT;
00161     initialize();
00162   }
00163   if (hit.right && (dir == RIGHT)) {
00164     dir = LEFT;
00165     initialize();
00166   }
00167 }

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

Called when the badguy collided with another badguy.

Reimplemented from BadGuy.

Definition at line 170 of file totem.cpp.

References carried_by, carrying, collision_badguy(), CONTINUE, BadGuy::dir, initialize(), jump_on(), LEFT, CollisionHit::left, CollisionHit::right, and RIGHT.

Referenced by collision_badguy().

00171 {
00172   // if we are being carried around, pass event to bottom of stack and ignore it
00173   if (carried_by) {
00174     carried_by->collision_badguy(badguy, hit);
00175     return CONTINUE;
00176   }
00177 
00178   // if we hit a Totem that is not from our stack: have our base jump on its top
00179   Totem* totem = dynamic_cast<Totem*>(&badguy);
00180   if (totem) {
00181     Totem* thisBase = this; while (thisBase->carried_by) thisBase=thisBase->carried_by;
00182     Totem* srcBase = totem; while (srcBase->carried_by)  srcBase=srcBase->carried_by;
00183     Totem* thisTop = this;  while (thisTop->carrying)    thisTop=thisTop->carrying;
00184     if (srcBase != thisBase) {
00185       srcBase->jump_on(thisTop);
00186     }
00187   }
00188 
00189   // If we are hit from the direction we are facing: turn around
00190   if(hit.left && (dir == LEFT)) {
00191     dir = RIGHT;
00192     initialize();
00193   }
00194   if(hit.right && (dir == RIGHT)) {
00195     dir = LEFT;
00196     initialize();
00197   }
00198 
00199   return CONTINUE;
00200 }

bool Totem::updatePointers ( const GameObject from_object,
GameObject to_object 
) [virtual]

Definition at line 46 of file totem.cpp.

References carried_by, and carrying.

00047 {
00048   if (from_object == carrying) {
00049     carrying = dynamic_cast<Totem*>(to_object);
00050     return true;
00051   }
00052   if (from_object == carried_by) {
00053     carried_by = dynamic_cast<Totem*>(to_object);
00054     return true;
00055   }
00056   return false;
00057 }

bool Totem::collision_squished ( GameObject object  )  [protected, 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 126 of file totem.cpp.

References MovingObject::bbox, Player::bounce(), carried_by, carrying, BadGuy::dir, jump_off(), BadGuy::kill_squished(), LEFT, Rectf::set_size(), and MovingSprite::sprite.

00127 {
00128   if (carrying) carrying->jump_off();
00129   if (carried_by) {
00130     Player* player = dynamic_cast<Player*>(&object);
00131     if (player) player->bounce(*this);
00132     jump_off();
00133   }
00134 
00135   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
00136   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00137 
00138   kill_squished(object);
00139   return true;
00140 }

void Totem::kill_fall (  )  [protected, virtual]

Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned upside-down).

Reimplemented from BadGuy.

Definition at line 203 of file totem.cpp.

References carried_by, carrying, jump_off(), and BadGuy::kill_fall().

00204 {
00205   if (carrying) carrying->jump_off();
00206   if (carried_by) jump_off();
00207 
00208   BadGuy::kill_fall();
00209 }

void Totem::jump_on ( Totem target  )  [protected]

jump on target

Definition at line 212 of file totem.cpp.

References MovingObject::bbox, carried_by, carrying, MovingObject::get_pos(), initialize(), LAND_ON_TOTEM_SOUND, log_warning, SoundManager::play(), Rectf::set_size(), sound_manager, MovingSprite::sprite, and synchronize_with().

Referenced by collision_badguy().

00213 {
00214   if (target->carrying) {
00215     log_warning << "target is already carrying someone" << std::endl;
00216     return;
00217   }
00218 
00219   target->carrying = this;
00220 
00221   this->carried_by = target;
00222   this->initialize();
00223   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00224 
00225   sound_manager->play( LAND_ON_TOTEM_SOUND , get_pos());
00226 
00227   this->synchronize_with(target);
00228 }

void Totem::jump_off (  )  [protected]

jump off current base

Definition at line 231 of file totem.cpp.

References MovingObject::bbox, carried_by, carrying, initialize(), JUMP_OFF_SPEED_Y, log_warning, BadGuy::physic, Rectf::set_size(), Physic::set_velocity_y(), and MovingSprite::sprite.

Referenced by collision_squished(), kill_fall(), and ~Totem().

00231                 {
00232   if (!carried_by) {
00233     log_warning << "not carried by anyone" << std::endl;
00234     return;
00235   }
00236 
00237   carried_by->carrying = 0;
00238 
00239   this->carried_by = 0;
00240 
00241   this->initialize();
00242   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00243 
00244   physic.set_velocity_y(JUMP_OFF_SPEED_Y);
00245 }

void Totem::synchronize_with ( Totem baseTotem  )  [protected]

synchronize position and movement with baseTotem

Definition at line 248 of file totem.cpp.

References BadGuy::dir, MovingObject::get_pos(), Physic::get_velocity_x(), Physic::get_velocity_y(), LEFT, BadGuy::physic, MovingObject::set_pos(), Physic::set_velocity_x(), Physic::set_velocity_y(), MovingSprite::sprite, and Vector::y.

Referenced by active_update(), initialize(), and jump_on().

00249 {
00250 
00251   if (dir != base->dir) {
00252     dir = base->dir;
00253     sprite->set_action(dir == LEFT ? "stacked-left" : "stacked-right");
00254   }
00255 
00256   Vector pos = base->get_pos();
00257   pos.y -= sprite->get_current_hitbox_height();
00258   set_pos(pos);
00259 
00260   physic.set_velocity_x(base->physic.get_velocity_x());
00261   physic.set_velocity_y(base->physic.get_velocity_y());
00262 }

Totem& Totem::operator= ( const Totem  )  [private]


Member Data Documentation

Totem* Totem::carrying [private]

Totem we are currently carrying (or 0).

Definition at line 48 of file totem.hpp.

Referenced by active_update(), collision_badguy(), collision_squished(), jump_off(), jump_on(), kill_fall(), updatePointers(), and ~Totem().

Totem* Totem::carried_by [private]

Totem by which we are currently carried (or 0).

Definition at line 49 of file totem.hpp.

Referenced by active_update(), collision_badguy(), collision_solid(), collision_squished(), initialize(), jump_off(), jump_on(), kill_fall(), updatePointers(), and ~Totem().


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