Switch Class Reference

#include <switch.hpp>

Inherits TriggerBase.

List of all members.

Public Member Functions

 Switch (const Reader &reader)
virtual ~Switch ()
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
virtual void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
virtual void event (Player &player, EventType type)
 Receive trigger events.

Private Types

enum  SwitchState { OFF, TURN_ON, ON, TURN_OFF }

Private Attributes

std::string sprite_name
SpritePtr sprite
std::string script
SwitchState state


Detailed Description

Definition at line 27 of file switch.hpp.


Member Enumeration Documentation

enum Switch::SwitchState [private]

Enumerator:
OFF 
TURN_ON 
ON 
TURN_OFF 

Definition at line 38 of file switch.hpp.

00038                    {
00039     OFF,
00040     TURN_ON,
00041     ON,
00042     TURN_OFF
00043   };


Constructor & Destructor Documentation

Switch::Switch ( const Reader reader  ) 

Definition at line 33 of file switch.cpp.

References MovingObject::bbox, SpriteManager::create(), lisp::Lisp::get(), Rectf::p1, SoundManager::preload(), script, Rectf::set_size(), sound_manager, sprite, sprite_manager, sprite_name, SWITCH_SOUND, Vector::x, and Vector::y.

00033                                    :
00034   sprite_name(),
00035   sprite(),
00036   script(),
00037   state(OFF)
00038 {
00039   if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
00040   if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
00041   if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
00042   sprite = sprite_manager->create(sprite_name);
00043   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00044 
00045   if (!reader.get("script", script)) throw std::runtime_error("no script set");
00046   sound_manager->preload( SWITCH_SOUND );
00047 }

Switch::~Switch (  )  [virtual]

Definition at line 49 of file switch.cpp.

00050 {
00051 }


Member Function Documentation

void Switch::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 TriggerBase.

Definition at line 54 of file switch.cpp.

References MovingObject::bbox, Sector::current(), OFF, ON, Rectf::p1, Sector::run_script(), script, sprite, state, TURN_OFF, and TURN_ON.

00055 {
00056   switch (state) {
00057     case OFF:
00058       break;
00059     case TURN_ON:
00060       if(sprite->animation_done()) {
00061         std::istringstream stream(script);
00062         std::ostringstream location;
00063         location << "switch" << bbox.p1;
00064         Sector::current()->run_script(stream, location.str());
00065 
00066         sprite->set_action("on", 1);
00067         state = ON;
00068       }
00069       break;
00070     case ON:
00071       if(sprite->animation_done()) {
00072         sprite->set_action("turnoff", 1);
00073         state = TURN_OFF;
00074       }
00075       break;
00076     case TURN_OFF:
00077       if(sprite->animation_done()) {
00078         sprite->set_action("off");
00079         state = OFF;
00080       }
00081       break;
00082   }
00083 }

void Switch::draw ( DrawingContext context  )  [virtual]

The GameObject should draw itself onto the provided DrawingContext if this function is called.

Reimplemented from TriggerBase.

Definition at line 86 of file switch.cpp.

References MovingObject::bbox, LAYER_TILES, Rectf::p1, and sprite.

00087 {
00088   sprite->draw(context, bbox.p1, LAYER_TILES);
00089 }

void Switch::event ( Player player,
EventType  type 
) [virtual]

Receive trigger events.

Implements TriggerBase.

Definition at line 92 of file switch.cpp.

References TriggerBase::EVENT_ACTIVATE, OFF, ON, SoundManager::play(), sound_manager, sprite, state, SWITCH_SOUND, TURN_OFF, and TURN_ON.

00093 {
00094   if(type != EVENT_ACTIVATE) return;
00095 
00096   switch (state) {
00097     case OFF:
00098       sprite->set_action("turnon", 1);
00099       sound_manager->play( SWITCH_SOUND );
00100       state = TURN_ON;
00101       break;
00102     case TURN_ON:
00103       break;
00104     case ON:
00105       break;
00106     case TURN_OFF:
00107       break;
00108   }
00109 
00110 }


Member Data Documentation

std::string Switch::sprite_name [private]

Definition at line 46 of file switch.hpp.

Referenced by Switch().

SpritePtr Switch::sprite [private]

Reimplemented from TriggerBase.

Definition at line 47 of file switch.hpp.

Referenced by draw(), event(), Switch(), and update().

std::string Switch::script [private]

Definition at line 48 of file switch.hpp.

Referenced by Switch(), and update().

SwitchState Switch::state [private]

Definition at line 49 of file switch.hpp.

Referenced by event(), and update().


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