Advertisement
Guest User

Path.h

a guest
Jan 12th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #ifndef PATH_H
  2. #define PATH_H
  3. //------------------------------------------------------------------------
  4. //
  5. //  Name:   Path.h
  6. //
  7. //  Desc:   class to define, manage, and traverse a path (defined by a series of 2D vectors)
  8. //          
  9. //
  10. //  Author: Mat Buckland 2003 (fup@ai-junkie.com)
  11. //
  12. //------------------------------------------------------------------------
  13. #include <list>
  14. #include <cassert>
  15.  
  16. #include "2d/Vector2D.h"
  17.  
  18.  
  19.  
  20.  
  21. class Path
  22. {
  23. private:
  24.  
  25.   std::list<Vector2D>            m_WayPoints;
  26.  
  27.   //points to the current waypoint
  28.   std::list<Vector2D>::iterator  curWaypoint;
  29.  
  30.   //flag to indicate if the path should be looped
  31.   //(The last waypoint connected to the first)
  32.   bool                           m_bLooped;
  33.  
  34. public:
  35.  
  36.   Path():m_bLooped(false){}
  37.  
  38.   //constructor for creating a path with initial random waypoints. MinX/Y
  39.   //& MaxX/Y define the bounding box of the path.
  40.   Path(int    NumWaypoints,
  41.        double MinX,
  42.        double MinY,
  43.        double MaxX,
  44.        double MaxY,
  45.        bool   looped):m_bLooped(looped)
  46.   {
  47.     CreateRandomPath(NumWaypoints, MinX, MinY, MaxX, MaxY);
  48.  
  49.     curWaypoint = m_WayPoints.begin();
  50.   }
  51.  
  52.  
  53.   //returns the current waypoint
  54.   Vector2D    CurrentWaypoint()const{assert(curWaypoint != NULL); return *curWaypoint;}
  55.  
  56.   //returns true if the end of the list has been reached
  57.   bool        Finished(){return !(curWaypoint != m_WayPoints.end());}
  58.  
  59.   //moves the iterator on to the next waypoint in the list
  60.   inline void SetNextWaypoint();
  61.  
  62.   //creates a random path which is bound by rectangle described by
  63.   //the min/max values
  64.   std::list<Vector2D> CreateRandomPath(int    NumWaypoints,
  65.                                        double MinX,
  66.                                        double MinY,
  67.                                        double MaxX,
  68.                                        double MaxY);
  69.  
  70.  
  71.   void LoopOn(){m_bLooped = true;}
  72.   void LoopOff(){m_bLooped = false;}
  73.  
  74.   //adds a waypoint to the end of the path
  75.   void AddWayPoint(Vector2D new_point);
  76.  
  77.   //methods for setting the path with either another Path or a list of vectors
  78.   void Set(std::list<Vector2D> new_path){m_WayPoints = new_path;curWaypoint = m_WayPoints.begin();}
  79.   void Set(const Path& path){m_WayPoints=path.GetPath(); curWaypoint = m_WayPoints.begin();}
  80.  
  81.  
  82.   void Clear(){m_WayPoints.clear();}
  83.  
  84.   std::list<Vector2D> GetPath()const{return m_WayPoints;}
  85.  
  86.   //renders the path in orange
  87.   void Render()const;
  88. };
  89.  
  90.  
  91.  
  92.  
  93. //-------------------- Methods -------------------------------------------
  94.  
  95. inline void Path::SetNextWaypoint()
  96. {
  97.   assert (m_WayPoints.size() > 0);
  98.    
  99.   if (++curWaypoint == m_WayPoints.end())
  100.   {
  101.     if (m_bLooped)
  102.     {
  103.       curWaypoint = m_WayPoints.begin();
  104.     }
  105.   }
  106. }  
  107.  
  108.  
  109.  
  110. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement