Homepage Demos Overview Downloads Tutorials Reference
Credits

Transition.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_Transition_h_
00003 #define INCLUDED_Transition_h_
00004 
00005 #include "BehaviorBase.h"
00006 #include <vector>
00007 
00008 class StateNode;
00009 
00010 //! Represents a transition between StateNodes.
00011 /*! This is an abstract class - you'll want to subclass it to put
00012  *  conditions on the transitions.  Transitions are "smart" - they are
00013  *  behaviors in and of themselves and can listen for events and use
00014  *  the standard DoStart/DoStop interface.  Based on the events they
00015  *  receive, they can activate() themselves and cause a state
00016  *  transition.
00017  *
00018  *  DoStart() will be called when this transition is 'active' - it
00019  *  should listen/monitor for the transition it represents until
00020  *  DoStop() is called.
00021  *  
00022  *  If the conditions are satisified for a transition, you should call
00023  *  activate() to do the appropriate notifications.
00024  *
00025  *  Also note that a transition can have multiple sources and
00026  *  destinations.  See activate().
00027  *
00028  *  When setting up, the flow of additions follows the flow of
00029  *  control.  In other words, you add a transition to a source, and
00030  *  you add a destination to a transition.  This makes the code
00031  *  simpler because it doesn't have to worry about recursive looping
00032  *  depending whether the source was added to the transition or the
00033  *  transition was added to the source.  Confusing?  Exactly.
00034  */
00035 class Transition : public BehaviorBase {
00036   friend class StateNode;
00037 public:
00038   //!constructor
00039   Transition() : BehaviorBase(), srcs(), dsts(), sound() {}
00040   //!constructor, specify destination StateNode (ignores NULL)
00041   Transition(StateNode* destination) : BehaviorBase(), srcs(), dsts(), sound() {
00042     addDestination(destination);
00043   }
00044   //!copy constructor, just in case you need it
00045   Transition(const Transition& t) : BehaviorBase(t), srcs(t.srcs), dsts(t.dsts), sound(t.sound) {}
00046 
00047   //!assignment operator (only does shallow copy)
00048   Transition& operator=(const Transition& t) { BehaviorBase::operator=(t); srcs=t.srcs; dsts=t.dsts; sound=t.sound; return *this; }
00049 
00050   //!destructor
00051   virtual ~Transition() {}
00052 
00053   //!call this when the transition should be made, base class version simply calls StateNode::DoStop() on each active of #srcs and StateNode::DoStart() on each inactive of #dsts, but you can override.
00054   virtual void activate();
00055 
00056   virtual std::vector<StateNode*>& getSources() { return srcs; }  //!< returns a user-modifiable reference to the current source list
00057   virtual const std::vector<StateNode*>& getSources() const { return srcs; } //!< returns a const reference to the current source list
00058 
00059   virtual void addDestination(StateNode* destination) { if(destination!=NULL) dsts.push_back(destination); } //!< if @a destination is non-null, add it to the destination list
00060   virtual std::vector<StateNode*>& getDestinations() { return dsts; } //!< returns a user-modifiable reference to the current destination list
00061   virtual const std::vector<StateNode*>& getDestinations() const { return dsts; } //!< returns a const reference to the current destination list
00062 
00063   virtual void setSound(const std::string& snd) { sound=snd; } //!< set a sound file to be played upon activation; you might want to preload this in the parent node; empty string to turn off
00064   virtual std::string getSound() { return sound; } //!< returns the current sound file
00065   
00066 protected:
00067   //! if @a source is non-null, add it to the source list
00068   /*! Only StateNodes should be calling this - you add a transition to a source, not a source to a transition.
00069    *  @see StateNode::addTransition() */
00070   virtual void addSource(StateNode* source) { if(source!=NULL) srcs.push_back(source); } 
00071 
00072   std::vector<StateNode*> srcs; //!< the node being transitioned from
00073   std::vector<StateNode*> dsts; //!< the node being transitioned to
00074   std::string sound;            //!< sound to play on transitioning
00075 };
00076 
00077 /*! @file
00078  * @brief Describes Transition, represents a transition between StateNodes.
00079  * @author ejt (Creator)
00080  *
00081  * $Author: ejt $
00082  * $Name: tekkotsu-2_1 $
00083  * $Revision: 1.4 $
00084  * $State: Exp $
00085  * $Date: 2003/11/11 00:08:10 $
00086  */
00087 
00088 #endif

Tekkotsu v2.1
Generated Tue Mar 16 23:19:16 2004 by Doxygen 1.3.5