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 fire() 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  *  fire() to do the appropriate notifications.
00024  *
00025  *  Also note that a transition can have multiple sources and
00026  *  destinations.  See fire().
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   //!destructor
00039   virtual ~Transition() {}
00040 
00041   //!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.
00042   virtual void fire();
00043 
00044   //!deprecated: use #fire() instead (just a better name
00045   virtual void activate() __attribute__((deprecated));
00046 
00047   virtual std::vector<StateNode*>& getSources() { return srcs; }  //!< returns a user-modifiable reference to the current source list
00048   virtual const std::vector<StateNode*>& getSources() const { return srcs; } //!< returns a const reference to the current source list
00049 
00050   virtual void addDestination(StateNode* destination) { if(destination!=NULL) dsts.push_back(destination); } //!< if @a destination is non-null, add it to the destination list
00051   virtual std::vector<StateNode*>& getDestinations() { return dsts; } //!< returns a user-modifiable reference to the current destination list
00052   virtual const std::vector<StateNode*>& getDestinations() const { return dsts; } //!< returns a const reference to the current destination list
00053 
00054   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
00055   virtual std::string getSound() { return sound; } //!< returns the current sound file
00056   
00057   //! If #instanceName == #className, will autogenerate a name incorporating source and destination names
00058   virtual std::string getName() const;
00059 
00060 protected:
00061   //! deprecated, use the version of the constructor where you can pass a name
00062   Transition() __attribute__((deprecated));
00063   //!constructor, pass your subclass type name as a string for the default name
00064   explicit Transition(const std::string& classname) : BehaviorBase(classname), srcs(), dsts(), sound() {}
00065   //! deprecated, use the version of the constructor where you can pass a name
00066   explicit Transition(StateNode* destination) __attribute__((deprecated));
00067   //!constructor, specify destination StateNode (ignores NULL)
00068   Transition(const std::string& classname, StateNode* destination) : BehaviorBase(classname), srcs(), dsts(), sound() {
00069     addDestination(destination);
00070   }
00071   //!constructor, pass your subclass type name as a string for the default name, and a separate instance name
00072   Transition(const std::string& classname, const std::string& instancename) : BehaviorBase(classname,instancename), srcs(), dsts(), sound() {}
00073   //!constructor, specify names and destination StateNode (ignores NULL)
00074   Transition(const std::string& classname, const std::string& instancename, StateNode* destination) : BehaviorBase(classname,instancename), srcs(), dsts(), sound() {
00075     addDestination(destination);
00076   }
00077   //!copy constructor, just in case you need it
00078   Transition(const Transition& t) : BehaviorBase(t), srcs(t.srcs), dsts(t.dsts), sound(t.sound) {}
00079 
00080   //!assignment operator (only does shallow copy)
00081   Transition& operator=(const Transition& t) { BehaviorBase::operator=(t); srcs=t.srcs; dsts=t.dsts; sound=t.sound; return *this; }
00082 
00083   //! if @a source is non-null, add it to the source list
00084   /*! Only StateNodes should be calling this - you add a transition to a source, not a source to a transition.
00085    *  @see StateNode::addTransition() */
00086   virtual void addSource(StateNode* source) { if(source!=NULL) srcs.push_back(source); } 
00087 
00088   std::vector<StateNode*> srcs; //!< the node being transitioned from
00089   std::vector<StateNode*> dsts; //!< the node being transitioned to
00090   std::string sound;            //!< sound to play on transitioning
00091 };
00092 
00093 /*! @file
00094  * @brief Describes Transition, represents a transition between StateNodes.
00095  * @author ejt (Creator)
00096  *
00097  * $Author: ejt $
00098  * $Name: tekkotsu-2_2_2 $
00099  * $Revision: 1.9 $
00100  * $State: Exp $
00101  * $Date: 2004/11/15 22:46:19 $
00102  */
00103 
00104 #endif

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:15 2005 by Doxygen 1.4.0