Door Class Reference

#include <door.hpp>

Inherits TriggerBase.

List of all members.

Public Member Functions

 Door (const Reader &reader)
 Door (int x, int y, std::string sector, std::string spawnpoint)
virtual ~Door ()
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.
virtual HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object

Private Types

enum  DoorState { CLOSED, OPENING, OPEN, CLOSING }

Private Attributes

DoorState state
 current state of the door
std::string target_sector
 target sector to teleport to
std::string target_spawnpoint
 target spawnpoint to teleport to
std::string script
SpritePtr sprite
 "door" sprite to render
Timer stay_open_timer
 time until door will close again


Detailed Description

Definition at line 25 of file door.hpp.


Member Enumeration Documentation

enum Door::DoorState [private]

Enumerator:
CLOSED 
OPENING 
OPEN 
CLOSING 

Definition at line 38 of file door.hpp.

00038                  {
00039     CLOSED,
00040     OPENING,
00041     OPEN,
00042     CLOSING
00043   };


Constructor & Destructor Documentation

Door::Door ( const Reader reader  ) 

Definition at line 26 of file door.cpp.

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

00026                                :
00027   state(CLOSED),
00028   target_sector(),
00029   target_spawnpoint(),
00030   script(),
00031   sprite(),
00032   stay_open_timer()
00033 {
00034   reader.get("x", bbox.p1.x);
00035   reader.get("y", bbox.p1.y);
00036   reader.get("sector", target_sector);
00037   reader.get("spawnpoint", target_spawnpoint);
00038 
00039   reader.get("script", script);
00040 
00041   sprite = sprite_manager->create("images/objects/door/door.sprite");
00042   sprite->set_action("closed");
00043   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00044 
00045   sound_manager->preload("sounds/door.wav");
00046 }

Door::Door ( int  x,
int  y,
std::string  sector,
std::string  spawnpoint 
)

Definition at line 48 of file door.cpp.

References MovingObject::bbox, SpriteManager::create(), SoundManager::preload(), Rectf::set_pos(), Rectf::set_size(), sound_manager, sprite, sprite_manager, target_sector, and target_spawnpoint.

00048                                                                :
00049   state(CLOSED),
00050   target_sector(),
00051   target_spawnpoint(),
00052   script(),
00053   sprite(),
00054   stay_open_timer()
00055 {
00056   bbox.set_pos(Vector(x, y));
00057   target_sector = sector;
00058   target_spawnpoint = spawnpoint;
00059 
00060   sprite = sprite_manager->create("images/objects/door/door.sprite");
00061   sprite->set_action("closed");
00062   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00063 
00064   sound_manager->preload("sounds/door.wav");
00065 }

Door::~Door (  )  [virtual]

Definition at line 67 of file door.cpp.

00068 {
00069 }


Member Function Documentation

void Door::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 72 of file door.cpp.

References Timer::check(), CLOSED, CLOSING, OPEN, OPENING, sprite, Timer::start(), state, and stay_open_timer.

00073 {
00074   switch (state) {
00075     case CLOSED:
00076       break;
00077     case OPENING:
00078       // if door has finished opening, start timer and keep door open
00079       if(sprite->animation_done()) {
00080         state = OPEN;
00081         sprite->set_action("open");
00082         stay_open_timer.start(1.0);
00083       }
00084       break;
00085     case OPEN:
00086       // if door was open long enough, start closing it
00087       if (stay_open_timer.check()) {
00088         state = CLOSING;
00089         sprite->set_action("closing", 1);
00090       }
00091       break;
00092     case CLOSING:
00093       // if door has finished closing, keep it shut
00094       if(sprite->animation_done()) {
00095         state = CLOSED;
00096         sprite->set_action("closed");
00097       }
00098       break;
00099   }
00100 }

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

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

Reimplemented from TriggerBase.

Definition at line 103 of file door.cpp.

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

00104 {
00105   sprite->draw(context, bbox.p1, LAYER_BACKGROUNDTILES+1);
00106 }

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

Receive trigger events.

Implements TriggerBase.

Definition at line 109 of file door.cpp.

References CLOSED, CLOSING, TriggerBase::EVENT_ACTIVATE, OPEN, OPENING, SoundManager::play(), sound_manager, sprite, and state.

00110 {
00111   switch (state) {
00112     case CLOSED:
00113       // if door was activated, start opening it
00114       if (type == EVENT_ACTIVATE) {
00115         state = OPENING;
00116         sound_manager->play("sounds/door.wav");
00117         sprite->set_action("opening", 1);
00118       }
00119       break;
00120     case OPENING:
00121       break;
00122     case OPEN:
00123       break;
00124     case CLOSING:
00125       break;
00126   }
00127 }

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

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

Reimplemented from TriggerBase.

Definition at line 130 of file door.cpp.

References CLOSED, CLOSING, TriggerBase::collision(), Currenton< GameSession >::current(), Sector::current(), TriggerBase::hit, OPEN, OPENING, GameSession::respawn(), Sector::run_script(), script, sprite, state, target_sector, and target_spawnpoint.

00131 {
00132   switch (state) {
00133     case CLOSED:
00134       break;
00135     case OPENING:
00136       break;
00137     case OPEN:
00138     {
00139       // if door is open and was touched by a player, teleport the player
00140       Player* player = dynamic_cast<Player*> (&other);
00141       if (player) {
00142         state = CLOSING;
00143         sprite->set_action("closing", 1);
00144         if(!script.empty()) {
00145           std::istringstream stream(script);
00146           Sector::current()->run_script(stream, "Door");
00147         }
00148 
00149         if(!target_sector.empty()) {
00150           GameSession::current()->respawn(target_sector, target_spawnpoint);
00151         }
00152       }
00153     }
00154     break;
00155     case CLOSING:
00156       break;
00157   }
00158 
00159   return TriggerBase::collision(other, hit);
00160 }


Member Data Documentation

DoorState Door::state [private]

current state of the door

Definition at line 46 of file door.hpp.

Referenced by collision(), event(), and update().

std::string Door::target_sector [private]

target sector to teleport to

Definition at line 47 of file door.hpp.

Referenced by collision(), and Door().

std::string Door::target_spawnpoint [private]

target spawnpoint to teleport to

Definition at line 48 of file door.hpp.

Referenced by collision(), and Door().

std::string Door::script [private]

Definition at line 49 of file door.hpp.

Referenced by collision(), and Door().

SpritePtr Door::sprite [private]

"door" sprite to render

Reimplemented from TriggerBase.

Definition at line 50 of file door.hpp.

Referenced by collision(), Door(), draw(), event(), and update().

Timer Door::stay_open_timer [private]

time until door will close again

Definition at line 51 of file door.hpp.

Referenced by update().


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