Angel 3.2
A 2D Game Prototyping Engine
PathFinder.h
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #pragma once
31 
32 #include "../Infrastructure/Vector2.h"
33 
34 struct PathFinderMove;
35 class FindNextMoveState;
36 
38 {
39 public:
40  enum ePFMoveResult
41  {
42  PFMR_PATH_NOT_FOUND,
43  PFMR_PATH_FOUND,
44  PFMR_ARRIVED,
45  };
46 
47  PathFinder();
48  virtual ~PathFinder();
49 
50  void FindNextMove( const Vector2& from, const Vector2& to, float arrivalDist, PathFinderMove& move );
51 
52  enum ePFMoveState
53  {
54  PFMS_START,
55  PFMS_VALIDATE,
56  PFMS_FOLLOW,
57  PFMS_RECOVER,
58  PFMS_STARTRECOVER,
59 
60  PFMS_COUNT
61  };
62 
63  void Render();
64  static void EnableDrawPaths(bool enable);
65 
66 private:
67  void InitializeStates();
68 
69  FindNextMoveState* GetCurrentState();
70  void SetNewState( ePFMoveState newState );
71 
72  Vector2List _currentPath;
73  int _currentPathIndex;
74  ePFMoveState _currentState;
75  Vector2 _currentPos;
76  Vector2 _currentDest;
77  float _arrivalDist;
78 
79  FindNextMoveState* _states[PFMS_COUNT];
80 
81  friend class FindNextMoveState;
82 
83  static bool _drawPaths;
84 
85 };
86 
87 /* abstract */ class FindNextMoveState
88 {
89 public:
90  virtual ~FindNextMoveState() {}
91  virtual void Initialize( PathFinder* pathFinder ) {_pathFinder = pathFinder;}
92  virtual bool Update( PathFinderMove& move ) = 0;
93  virtual void BeginState( PathFinder::ePFMoveState /*lastState*/ ) {}
94  virtual void EndState( PathFinder::ePFMoveState /*nextState*/ ) {}
95  virtual const char* GetName() = 0;
96 
97 protected:
98  void SetNewState( PathFinder::ePFMoveState newState );
99  Vector2List& GetCurrentPath() {return _pathFinder->_currentPath;}
100  const Vector2& GetCurrentPosition() { return _pathFinder->_currentPos; }
101  const Vector2& GetCurrentDestination() { return _pathFinder->_currentDest; }
102  int& GetCurrentPathIndex() {return _pathFinder->_currentPathIndex; }
103  float GetCurrentArrivalDist() {return _pathFinder->_arrivalDist; }
104 
105 
106  PathFinder* _pathFinder;
107 };
108 
109 
111 {
112  Vector2 MoveDir;
113  Vector2 NextSubgoalPos;
114  PathFinder::ePFMoveResult LastResult;
115 };
116