Platform Class Reference

This class is the base class for platforms that tux can stand on. More...

#include <platform.hpp>

Inherits MovingSprite, and ScriptInterface.

Inherited by HurtingPlatform.

List of all members.

Public Member Functions

 Platform (const Reader &reader)
 Platform (const Platform &platform)
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.
const Vectorget_speed () const
virtual void expose (HSQUIRRELVM vm, SQInteger table_idx)
virtual void unexpose (HSQUIRRELVM vm, SQInteger table_idx)
Pathget_path ()
Scriptable Methods
void goto_node (int node_no)
 Move platform until at given node, then stop.
void start_moving ()
 Start moving platform.
void stop_moving ()
 Stop platform at next node.

Private Attributes

std::auto_ptr< Pathpath
std::auto_ptr< PathWalkerwalker
Vector speed
bool automatic
 true if Platform will automatically pick a destination based on collisions and current Player position
bool player_contact
 true if a Player touched the Platform during the last round of collision detections
bool last_player_contact
 true if a Player touched the Platform during the round before the last round of collision detections


Detailed Description

This class is the base class for platforms that tux can stand on.

Definition at line 27 of file platform.hpp.


Constructor & Destructor Documentation

Platform::Platform ( const Reader reader  ) 

Definition at line 26 of file platform.cpp.

References automatic, MovingObject::bbox, lisp::Lisp::get(), lisp::Lisp::get_lisp(), GameObject::name, path, Rectf::set_pos(), and walker.

00026                                        :
00027   MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), 
00028   path(),
00029   walker(),
00030   speed(Vector(0,0)), 
00031   automatic(false), 
00032   player_contact(false), 
00033   last_player_contact(false)
00034 {
00035   bool running = true;
00036   reader.get("name", name);
00037   reader.get("running", running);
00038   if ((name == "") && (!running)) automatic=true;
00039   const lisp::Lisp* pathLisp = reader.get_lisp("path");
00040   if(pathLisp == NULL)
00041     throw std::runtime_error("No path specified for platform");
00042   path.reset(new Path());
00043   path->read(*pathLisp);
00044   walker.reset(new PathWalker(path.get(), running));
00045   bbox.set_pos(path->get_base());
00046 }

Platform::Platform ( const Platform platform  ) 


Member Function Documentation

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

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

Implements MovingObject.

Reimplemented in HurtingPlatform.

Definition at line 65 of file platform.cpp.

References FORCE_MOVE, and player_contact.

00066 {
00067   if (dynamic_cast<Player*>(&other)) player_contact = true;
00068   return FORCE_MOVE;
00069 }

void Platform::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 72 of file platform.cpp.

References automatic, Sector::current(), Sector::get_players(), MovingObject::get_pos(), goto_node(), last_player_contact, MovingObject::movement, path, player_contact, speed, and walker.

00073 {
00074   // check if Platform should automatically pick a destination
00075   if (automatic) {
00076 
00077     if (!player_contact && !walker->is_moving()) {
00078       // Player doesn't touch platform and Platform is not moving
00079 
00080       // Travel to node nearest to nearest player
00081       // FIXME: does not really use nearest player
00082       Player* player = 0;      
00083       std::vector<Player*> players = Sector::current()->get_players();
00084       for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
00085         player = *playerIter;
00086       }
00087       if (player) {
00088         int nearest_node_id = path->get_nearest_node_no(player->get_bbox().p2);
00089         if (nearest_node_id != -1) {
00090           goto_node(nearest_node_id);
00091         }
00092       }
00093     } 
00094 
00095     if (player_contact && !last_player_contact && !walker->is_moving()) {
00096       // Player touched platform, didn't touch last frame and Platform is not moving
00097 
00098       // Travel to node farthest from current position
00099       int farthest_node_id = path->get_farthest_node_no(get_pos());
00100       if (farthest_node_id != -1) {
00101         goto_node(farthest_node_id);
00102       } 
00103     }
00104 
00105     // Clear player_contact flag set by collision() method
00106     last_player_contact = player_contact;
00107     player_contact = false;
00108   }
00109 
00110   movement = walker->advance(elapsed_time) - get_pos();
00111   speed = movement / elapsed_time;
00112 }

const Vector& Platform::get_speed (  )  const [inline]

Definition at line 37 of file platform.hpp.

References speed.

00038   {
00039     return speed;
00040   }

void Platform::goto_node ( int  node_no  ) 

Move platform until at given node, then stop.

Definition at line 115 of file platform.cpp.

References walker.

Referenced by scripting::Platform::goto_node(), and update().

00116 {
00117   walker->goto_node(node_no);
00118 }

void Platform::start_moving (  ) 

Start moving platform.

Definition at line 121 of file platform.cpp.

References walker.

Referenced by scripting::Platform::start_moving().

00122 {
00123   walker->start_moving();
00124 }

void Platform::stop_moving (  ) 

Stop platform at next node.

Definition at line 127 of file platform.cpp.

References walker.

Referenced by scripting::Platform::stop_moving().

00128 {
00129   walker->stop_moving();
00130 }

void Platform::expose ( HSQUIRRELVM  vm,
SQInteger  table_idx 
) [virtual]

Implements ScriptInterface.

Definition at line 133 of file platform.cpp.

References scripting::expose_object(), and GameObject::name.

00134 {
00135   if (name.empty()) return;
00136   scripting::Platform* _this = new scripting::Platform(this);
00137   expose_object(vm, table_idx, _this, name, true);
00138 }

void Platform::unexpose ( HSQUIRRELVM  vm,
SQInteger  table_idx 
) [virtual]

Implements ScriptInterface.

Definition at line 141 of file platform.cpp.

References GameObject::name, and scripting::unexpose_object().

00142 {
00143   if (name.empty()) return;
00144   scripting::unexpose_object(vm, table_idx, name);
00145 }

Path& Platform::get_path (  )  [inline]

Definition at line 63 of file platform.hpp.

References path.

Referenced by FlipLevelTransformer::transform_platform().

00063                    {
00064     return *path.get();
00065   }


Member Data Documentation

std::auto_ptr<Path> Platform::path [private]

Definition at line 68 of file platform.hpp.

Referenced by get_path(), Platform(), and update().

std::auto_ptr<PathWalker> Platform::walker [private]

Definition at line 69 of file platform.hpp.

Referenced by goto_node(), Platform(), start_moving(), stop_moving(), and update().

Vector Platform::speed [private]

Definition at line 71 of file platform.hpp.

Referenced by get_speed(), and update().

bool Platform::automatic [private]

true if Platform will automatically pick a destination based on collisions and current Player position

Definition at line 73 of file platform.hpp.

Referenced by Platform(), and update().

bool Platform::player_contact [private]

true if a Player touched the Platform during the last round of collision detections

Definition at line 74 of file platform.hpp.

Referenced by collision(), and update().

bool Platform::last_player_contact [private]

true if a Player touched the Platform during the round before the last round of collision detections

Definition at line 75 of file platform.hpp.

Referenced by update().


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