Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
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  *  A template file is available at <a href="http://cvs.tekkotsu.org/cgi/viewcvs.cgi/Tekkotsu/project/templates/transition.h?rev=HEAD&content-type=text/vnd.viewcvs-markup"><i>project</i><tt>/templates/transition.h</tt></a>,
00036  *  which will help you get moving faster.
00037  */
00038 class Transition : public BehaviorBase {
00039   friend class StateNode;
00040 public:
00041   //!destructor
00042   virtual ~Transition() {}
00043 
00044   //!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.
00045   virtual void fire();
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   //!constructor, pass your subclass type name as a string for the default name
00062   explicit Transition(const std::string& classname) : BehaviorBase(classname), srcs(), dsts(), sound() {}
00063   //!constructor, specify destination StateNode (ignores NULL)
00064   Transition(const std::string& classname, StateNode* destination) : BehaviorBase(classname), srcs(), dsts(), sound() {
00065     addDestination(destination);
00066   }
00067   //!constructor, pass your subclass type name as a string for the default name, and a separate instance name
00068   Transition(const std::string& classname, const std::string& instancename) : BehaviorBase(classname,instancename), srcs(), dsts(), sound() {}
00069   //!constructor, specify names and destination StateNode (ignores NULL)
00070   Transition(const std::string& classname, const std::string& instancename, StateNode* destination) : BehaviorBase(classname,instancename), srcs(), dsts(), sound() {
00071     addDestination(destination);
00072   }
00073   //!copy constructor, just in case you need it
00074   Transition(const Transition& t) : BehaviorBase(t), srcs(t.srcs), dsts(t.dsts), sound(t.sound) {}
00075 
00076   //!assignment operator (only does shallow copy)
00077   Transition& operator=(const Transition& t) { BehaviorBase::operator=(t); srcs=t.srcs; dsts=t.dsts; sound=t.sound; return *this; }
00078 
00079   //! if @a source is non-null, add it to the source list
00080   /*! Only StateNodes should be calling this - you add a transition to a source, not a source to a transition.
00081    *  @see StateNode::addTransition() */
00082   virtual void addSource(StateNode* source) { if(source!=NULL) srcs.push_back(source); } 
00083 
00084   std::vector<StateNode*> srcs; //!< the node being transitioned from
00085   std::vector<StateNode*> dsts; //!< the node being transitioned to
00086   std::string sound;            //!< sound to play on transitioning
00087 };
00088 
00089 /*! @file
00090  * @brief Describes Transition, represents a transition between StateNodes.
00091  * @author ejt (Creator)
00092  *
00093  * $Author: ejt $
00094  * $Name: tekkotsu-4_0 $
00095  * $Revision: 1.13 $
00096  * $State: Exp $
00097  * $Date: 2006/10/03 21:08:20 $
00098  */
00099 
00100 #endif

Tekkotsu v4.0
Generated Thu Nov 22 00:54:56 2007 by Doxygen 1.5.4