PushButton Class Reference

PushButton - jump on it to run a script. More...

#include <pushbutton.hpp>

Inherits MovingSprite.

List of all members.

Public Member Functions

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

Private Types

enum  PushButtonState { OFF, ON }

Private Attributes

std::string script
PushButtonState state


Detailed Description

PushButton - jump on it to run a script.

Definition at line 25 of file pushbutton.hpp.


Member Enumeration Documentation

enum PushButton::PushButtonState [private]

Enumerator:
OFF 
ON 

Definition at line 34 of file pushbutton.hpp.

00034                        {
00035     OFF,
00036     ON
00037   };


Constructor & Destructor Documentation

PushButton::PushButton ( const Reader reader  ) 

Definition at line 33 of file pushbutton.cpp.

References MovingObject::bbox, BUTTON_SOUND, lisp::Lisp::get(), SoundManager::preload(), script, MovingSprite::set_action(), Rectf::set_size(), sound_manager, and MovingSprite::sprite.

00033                                          :
00034   MovingSprite(lisp, "images/objects/pushbutton/pushbutton.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING), 
00035   script(),
00036   state(OFF)
00037 {
00038   sound_manager->preload(BUTTON_SOUND);
00039   set_action("off", -1);
00040   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00041 
00042   if (!lisp.get("script", script)) throw std::runtime_error("no script set");
00043 }


Member Function Documentation

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

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

Implements MovingObject.

Definition at line 51 of file pushbutton.cpp.

References MovingObject::bbox, BUTTON_SOUND, Sector::current(), FORCE_MOVE, Rectf::get_height(), Player::get_physic(), MovingObject::get_pos(), Physic::get_velocity_y(), OFF, ON, SoundManager::play(), Sector::run_script(), script, MovingSprite::set_action(), MovingObject::set_pos(), Physic::set_velocity_y(), sound_manager, state, and CollisionHit::top.

00052 {
00053   Player* player = dynamic_cast<Player*>(&other);
00054   if (!player) return FORCE_MOVE;
00055   float vy = player->get_physic().get_velocity_y();
00056 
00057   //player->add_velocity(Vector(0, -150));
00058   player->get_physic().set_velocity_y(-150);
00059 
00060   if (state != OFF) return FORCE_MOVE;
00061   if (!hit.top) return FORCE_MOVE;
00062   if (vy <= 0) return FORCE_MOVE;
00063 
00064   // change appearance
00065   state = ON;
00066   float old_bbox_height = bbox.get_height();
00067   set_action("on", -1);
00068   float new_bbox_height = bbox.get_height();
00069   set_pos(get_pos() + Vector(0, old_bbox_height - new_bbox_height));
00070 
00071   // play sound
00072   sound_manager->play(BUTTON_SOUND);
00073 
00074   // run script
00075   std::istringstream stream(script);
00076   Sector::current()->run_script(stream, "PushButton");
00077 
00078   return FORCE_MOVE;
00079 }

void PushButton::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 46 of file pushbutton.cpp.

00047 {
00048 }


Member Data Documentation

std::string PushButton::script [private]

Definition at line 39 of file pushbutton.hpp.

Referenced by collision(), and PushButton().

PushButtonState PushButton::state [private]

Definition at line 40 of file pushbutton.hpp.

Referenced by collision().


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