#include <switch.hpp>
Inherits TriggerBase.
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 |
Definition at line 27 of file switch.hpp.
enum Switch::SwitchState [private] |
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] |
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 }
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 }
std::string Switch::sprite_name [private] |
SpritePtr Switch::sprite [private] |
std::string Switch::script [private] |
SwitchState Switch::state [private] |