TriggerBase Class Reference

This class is the base class for all objects you can interact with in some way. More...

#include <trigger_base.hpp>

Inherits MovingObject, and ObjectRemoveListener.

Inherited by Climbable, Door, ScriptTrigger, SecretAreaTrigger, SequenceTrigger, and Switch.

List of all members.

Public Types

enum  EventType { EVENT_TOUCH, EVENT_LOSETOUCH, EVENT_ACTIVATE }

Public Member Functions

 TriggerBase ()
 ~TriggerBase ()
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
HitResponse collision (GameObject &other, const CollisionHit &hit)
 this function is called when the object collided with any other object
virtual void event (Player &player, EventType type)=0
 Receive trigger events.
virtual void object_removed (GameObject *object)
 Called by GameObject destructor of an object in losetouch_listeners.

Private Member Functions

 TriggerBase (const TriggerBase &)
TriggerBaseoperator= (const TriggerBase &)

Private Attributes

SpritePtr sprite
bool lasthit
bool hit
std::list< Player * > losetouch_listeners
 Players that will be informed when we lose touch with them.


Detailed Description

This class is the base class for all objects you can interact with in some way.

There are several interaction types defined like touch and activate

Definition at line 32 of file trigger_base.hpp.


Member Enumeration Documentation

enum TriggerBase::EventType

Enumerator:
EVENT_TOUCH  Object came into contact.
EVENT_LOSETOUCH  Lost contact with object.
EVENT_ACTIVATE  Action button pressed.

Definition at line 36 of file trigger_base.hpp.

00036                  {
00037     EVENT_TOUCH,     
00038     EVENT_LOSETOUCH, 
00039     EVENT_ACTIVATE   
00040   };


Constructor & Destructor Documentation

TriggerBase::TriggerBase (  ) 

Definition at line 22 of file trigger_base.cpp.

References COLGROUP_TOUCHABLE, and MovingObject::set_group().

00022                          :
00023   sprite(),
00024   lasthit(false), 
00025   hit(false),
00026   losetouch_listeners()
00027 {
00028   set_group(COLGROUP_TOUCHABLE);
00029 }

TriggerBase::~TriggerBase (  ) 

Definition at line 31 of file trigger_base.cpp.

References losetouch_listeners.

00032 {
00033   // unregister remove_listener hooks, so nobody will try to call us after we've been destroyed
00034   for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
00035     Player* p = *i;
00036     p->del_remove_listener(this);
00037   }
00038   losetouch_listeners.clear();
00039 }

TriggerBase::TriggerBase ( const TriggerBase  )  [private]


Member Function Documentation

void TriggerBase::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)

Implements GameObject.

Reimplemented in Climbable, Door, and Switch.

Definition at line 42 of file trigger_base.cpp.

References event(), EVENT_LOSETOUCH, hit, lasthit, and losetouch_listeners.

00043 {
00044   if (lasthit && !hit) {
00045     for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
00046       Player* p = *i;
00047       event(*p, EVENT_LOSETOUCH);
00048       p->del_remove_listener(this);
00049     }
00050     losetouch_listeners.clear();
00051   }
00052   lasthit = hit;
00053   hit = false;
00054 }

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

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

Implements GameObject.

Reimplemented in Climbable, Door, SecretAreaTrigger, and Switch.

Definition at line 57 of file trigger_base.cpp.

References MovingObject::get_pos(), LAYER_TILES, and sprite.

00058 {
00059   if(!sprite.get())
00060     return;
00061 
00062   sprite->draw(context, get_pos(), LAYER_TILES+1);
00063 }

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

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

Implements MovingObject.

Reimplemented in Door.

Definition at line 66 of file trigger_base.cpp.

References ABORT_MOVE, event(), EVENT_TOUCH, hit, lasthit, and losetouch_listeners.

Referenced by Door::collision().

00067 {
00068   Player* player = dynamic_cast<Player*> (&other);
00069   if(player) {
00070     hit = true;
00071     if(!lasthit) {
00072       losetouch_listeners.push_back(player);
00073       player->add_remove_listener(this);
00074       event(*player, EVENT_TOUCH);
00075     }
00076   }
00077 
00078   return ABORT_MOVE;
00079 }

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

Receive trigger events.

Implemented in Climbable, Door, ScriptTrigger, SecretAreaTrigger, and Switch.

Referenced by collision(), and update().

void TriggerBase::object_removed ( GameObject object  )  [virtual]

Called by GameObject destructor of an object in losetouch_listeners.

Implements ObjectRemoveListener.

Definition at line 82 of file trigger_base.cpp.

References losetouch_listeners.

00083 {
00084   for (std::list<Player*>::iterator i = losetouch_listeners.begin(); i != losetouch_listeners.end(); i++) {
00085     Player* p = *i;
00086     if (p == object) {
00087       losetouch_listeners.erase(i);
00088       break;
00089     }
00090   }
00091 }

TriggerBase& TriggerBase::operator= ( const TriggerBase  )  [private]


Member Data Documentation

SpritePtr TriggerBase::sprite [private]

Reimplemented in Door, and Switch.

Definition at line 60 of file trigger_base.hpp.

Referenced by draw().

bool TriggerBase::lasthit [private]

Definition at line 61 of file trigger_base.hpp.

Referenced by collision(), and update().

bool TriggerBase::hit [private]

Definition at line 62 of file trigger_base.hpp.

Referenced by collision(), Door::collision(), and update().

std::list<Player*> TriggerBase::losetouch_listeners [private]

Players that will be informed when we lose touch with them.

Definition at line 64 of file trigger_base.hpp.

Referenced by collision(), object_removed(), update(), and ~TriggerBase().


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