PneumaticPlatform Class Reference

Used to construct a pair of pneumatic platforms: If one is pushed down, the other one rises. More...

#include <pneumatic_platform.hpp>

Inherits MovingSprite.

List of all members.

Public Member Functions

 PneumaticPlatform (const Reader &reader)
 PneumaticPlatform (PneumaticPlatform *master)
virtual ~PneumaticPlatform ()
virtual HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.

Protected Attributes

PneumaticPlatformmaster
 pointer to PneumaticPlatform that does movement calculation
PneumaticPlatformslave
 pointer to PneumaticPlatform that reacts to master platform's movement calculation
float start_y
 vertical start position
float offset_y
 vertical offset from the start position in px
float speed_y
 vertical speed
std::set< GameObject * > contacts
 objects that are currently pushing on the platform

Private Member Functions

 PneumaticPlatform (const PneumaticPlatform &)
PneumaticPlatformoperator= (const PneumaticPlatform &)


Detailed Description

Used to construct a pair of pneumatic platforms: If one is pushed down, the other one rises.

Definition at line 25 of file pneumatic_platform.hpp.


Constructor & Destructor Documentation

PneumaticPlatform::PneumaticPlatform ( const Reader reader  ) 

Definition at line 24 of file pneumatic_platform.cpp.

References MovingObject::get_pos(), start_y, and Vector::y.

Referenced by update().

00024                                                          :
00025   MovingSprite(reader, LAYER_OBJECTS, COLGROUP_STATIC), 
00026   master(0), 
00027   slave(0), 
00028   start_y(0),
00029   offset_y(0), 
00030   speed_y(0),
00031   contacts()
00032 {
00033   start_y = get_pos().y;
00034 }

PneumaticPlatform::PneumaticPlatform ( PneumaticPlatform master  ) 

Definition at line 36 of file pneumatic_platform.cpp.

References MovingObject::get_bbox(), MovingObject::get_pos(), Rectf::get_width(), master, MovingObject::set_pos(), and slave.

00036                                                               :
00037   MovingSprite(*master), 
00038   master(master), 
00039   slave(this), 
00040   start_y(master->start_y),
00041   offset_y(-master->offset_y), 
00042   speed_y(0),
00043   contacts()
00044 {
00045   set_pos(get_pos() + Vector(master->get_bbox().get_width(), 0));
00046   master->master = master;
00047   master->slave = this;
00048 }

PneumaticPlatform::~PneumaticPlatform (  )  [virtual]

Definition at line 50 of file pneumatic_platform.cpp.

References master, and slave.

00051 {
00052   if ((this == master) && (master)) {
00053     slave->master = 0;
00054     slave->slave = 0;
00055   }
00056   if ((master) && (this == slave)) {
00057     master->master = 0;
00058     master->slave = 0;
00059   }
00060   master = 0;
00061   slave = 0;
00062 }

PneumaticPlatform::PneumaticPlatform ( const PneumaticPlatform  )  [private]


Member Function Documentation

HitResponse PneumaticPlatform::collision ( GameObject other,
const CollisionHit hit 
) [virtual]

this function is called when the object collided with any other object

Implements MovingObject.

Definition at line 65 of file pneumatic_platform.cpp.

References contacts, FORCE_MOVE, MovingObject::get_bbox(), Player::get_grabbed_object(), Player::is_big(), Rectf::p2, and Vector::y.

00066 {
00067 
00068   // somehow the hit parameter does not get filled in, so to determine (hit.top == true) we do this:
00069   MovingObject* mo = dynamic_cast<MovingObject*>(&other);
00070   if (!mo) return FORCE_MOVE;
00071   if ((mo->get_bbox().p2.y) > (get_bbox().p1.y + 2)) return FORCE_MOVE;
00072 
00073   Player* pl = dynamic_cast<Player*>(mo);
00074   if (pl) {
00075     if (pl->is_big()) contacts.insert(0);
00076     Portable* po = pl->get_grabbed_object();
00077     MovingObject* pomo = dynamic_cast<MovingObject*>(po);
00078     if (pomo) contacts.insert(pomo);
00079   }
00080 
00081   contacts.insert(&other);
00082   return FORCE_MOVE;
00083 }

void PneumaticPlatform::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Reimplemented from MovingSprite.

Definition at line 86 of file pneumatic_platform.cpp.

References Sector::add_object(), contacts, Sector::current(), MovingObject::get_pos(), master, MovingObject::movement, offset_y, PneumaticPlatform(), slave, speed_y, and start_y.

00087 {
00088   if (!slave) {
00089     Sector::current()->add_object(new PneumaticPlatform(this));
00090     return;
00091   }
00092   if (!master) {
00093     return;
00094   }
00095   if (this == slave) {
00096     offset_y = -master->offset_y;
00097     movement = Vector(0, (start_y + offset_y) - get_pos().y);
00098   }
00099   if (this == master) {
00100     int contact_diff = contacts.size() - slave->contacts.size();
00101     contacts.clear();
00102     slave->contacts.clear();
00103 
00104     speed_y += ((float)contact_diff * elapsed_time) * 128.0f;
00105     speed_y -= (offset_y * elapsed_time * 0.5f);
00106     speed_y *= 1 - elapsed_time;
00107     offset_y += speed_y * elapsed_time;
00108     if (offset_y < -256) { offset_y = -256; speed_y = 0; } 
00109     if (offset_y > 256) { offset_y = 256; speed_y = -0; } 
00110     movement = Vector(0, (start_y + offset_y) - get_pos().y);
00111   }
00112 }

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


Member Data Documentation

PneumaticPlatform* PneumaticPlatform::master [protected]

pointer to PneumaticPlatform that does movement calculation

Definition at line 36 of file pneumatic_platform.hpp.

Referenced by PneumaticPlatform(), update(), and ~PneumaticPlatform().

PneumaticPlatform* PneumaticPlatform::slave [protected]

pointer to PneumaticPlatform that reacts to master platform's movement calculation

Definition at line 37 of file pneumatic_platform.hpp.

Referenced by PneumaticPlatform(), update(), and ~PneumaticPlatform().

float PneumaticPlatform::start_y [protected]

vertical start position

Definition at line 38 of file pneumatic_platform.hpp.

Referenced by PneumaticPlatform(), and update().

float PneumaticPlatform::offset_y [protected]

vertical offset from the start position in px

Definition at line 39 of file pneumatic_platform.hpp.

Referenced by update().

float PneumaticPlatform::speed_y [protected]

vertical speed

Definition at line 40 of file pneumatic_platform.hpp.

Referenced by update().

std::set<GameObject*> PneumaticPlatform::contacts [protected]

objects that are currently pushing on the platform

Definition at line 41 of file pneumatic_platform.hpp.

Referenced by collision(), and update().


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