Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.3 KiB

  1. // BehaviorMoveTo.h
  2. // Move to a potentially far away position
  3. // Author: Michael Booth, June 2007
  4. // Copyright (c) 2007 Turtle Rock Studios, Inc. - All Rights Reserved
  5. #ifndef _BEHAVIOR_MOVE_TO_H_
  6. #define _BEHAVIOR_MOVE_TO_H_
  7. //----------------------------------------------------------------------------------------------
  8. /**
  9. * Move to a potentially far away position, using path planning.
  10. */
  11. template < typename Actor, typename PathCost >
  12. class BehaviorMoveTo : public Action< Actor >
  13. {
  14. public:
  15. BehaviorMoveTo( const Vector &goal );
  16. virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction );
  17. virtual ActionResult< Actor > Update( Actor *me, float interval );
  18. // derive to supply specific cost functor - default uses simple shortest path
  19. virtual bool ComputePath( Actor *me, const Vector &goal, PathFollower *path );
  20. virtual const char *GetName( void ) const { return "BehaviorMoveTo"; }
  21. private:
  22. Vector m_goal;
  23. PathFollower m_path;
  24. };
  25. //----------------------------------------------------------------------------------------------
  26. template < typename Actor, typename PathCost >
  27. inline BehaviorMoveTo< Actor, PathCost >::BehaviorMoveTo( const Vector &goal )
  28. {
  29. m_goal = goal;
  30. m_path.Invalidate();
  31. }
  32. //----------------------------------------------------------------------------------------------
  33. template < typename Actor, typename PathCost >
  34. inline bool BehaviorMoveTo< Actor, PathCost >::ComputePath( Actor *me, const Vector &goal, PathFollower *path )
  35. {
  36. PathCost cost( me );
  37. return path->Compute( me, goal, cost );
  38. }
  39. //----------------------------------------------------------------------------------------------
  40. template < typename Actor, typename PathCost >
  41. inline ActionResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnStart( Actor *me, Action< Actor > *priorAction )
  42. {
  43. if ( !ComputePath( me, m_goal, &m_path ) )
  44. {
  45. return Done( "No path to goal" );
  46. }
  47. return Continue();
  48. }
  49. //----------------------------------------------------------------------------------------------
  50. template < typename Actor, typename PathCost >
  51. inline ActionResult< Actor > BehaviorMoveTo< Actor, PathCost >::Update( Actor *me, float interval )
  52. {
  53. // move along path
  54. m_path.Update( me );
  55. if ( m_path.IsValid() )
  56. {
  57. return Continue();
  58. }
  59. return Done();
  60. }
  61. #endif // _BEHAVIOR_MOVE_TO_H_