CrowdAgent.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Scene/Component.h"
  24. #include "../Navigation/DetourCrowdManager.h"
  25. namespace Atomic
  26. {
  27. enum CrowdTargetState
  28. {
  29. CROWD_AGENT_TARGET_NONE = 0,
  30. CROWD_AGENT_TARGET_FAILED,
  31. CROWD_AGENT_TARGET_VALID,
  32. CROWD_AGENT_TARGET_REQUESTING,
  33. CROWD_AGENT_TARGET_WAITINGFORQUEUE,
  34. CROWD_AGENT_TARGET_WAITINGFORPATH,
  35. CROWD_AGENT_TARGET_VELOCITY
  36. };
  37. enum CrowdAgentState
  38. {
  39. CROWD_AGENT_INVALID = 0, ///< The agent is not in a valid state.
  40. CROWD_AGENT_READY, ///< The agent is traversing a normal navigation mesh polygon.
  41. CROWD_AGENT_TRAVERSINGLINK ///< The agent is traversing an off-mesh connection.
  42. };
  43. /// DetourCrowd Agent, requires a DetourCrowdManager in the scene. Agent's radius and height is set through the navigation mesh.
  44. class ATOMIC_API CrowdAgent : public Component
  45. {
  46. OBJECT(CrowdAgent);
  47. friend class DetourCrowdManager;
  48. public:
  49. /// Construct.
  50. CrowdAgent(Context* context);
  51. /// Destruct.
  52. virtual ~CrowdAgent();
  53. /// Register object factory.
  54. static void RegisterObject(Context* context);
  55. /// Handle enabled/disabled state change.
  56. virtual void OnSetEnabled();
  57. /// Draw debug geometry.
  58. void DrawDebugGeometry(bool);
  59. /// Draw debug feelers.
  60. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  61. /// Set the navigation filter type the agent will use.
  62. void SetNavigationFilterType(unsigned filterTypeID);
  63. /// Submit a new move request for this agent.
  64. void SetMoveTarget(const Vector3& position);
  65. /// Reset any request for the specified agent.
  66. void ResetMoveTarget();
  67. /// Submit a new move velocity request for this agent.
  68. void SetMoveVelocity(const Vector3& velocity);
  69. /// Update the node position. When set to false, the node position should be updated by other means (e.g. using Physics) in response to the E_CROWD_AGENT_REPOSITION event.
  70. void SetUpdateNodePosition(bool unodepos);
  71. /// Set the agent's max acceleration.
  72. void SetMaxAccel(float val);
  73. /// Set the agent's max velocity.
  74. void SetMaxSpeed(float val);
  75. /// Set the agent's radius.
  76. void SetRadius(float val);
  77. /// Set the agent's height.
  78. void SetHeight(float val);
  79. /// Set the agent's navigation quality.
  80. void SetNavigationQuality(NavigationQuality val);
  81. /// Set the agent's navigation pushiness.
  82. void SetNavigationPushiness(NavigationPushiness val);
  83. /// Get the navigation filter type this agent is using.
  84. unsigned GetNavigationFilterType() const { return filterType_; }
  85. /// Return the agent's position.
  86. Vector3 GetPosition() const;
  87. /// Return the agent's desired velocity.
  88. Vector3 GetDesiredVelocity() const;
  89. /// Return the agent's actual velocity.
  90. Vector3 GetActualVelocity() const;
  91. /// Return the agent's target position.
  92. const Vector3& GetTargetPosition() const { return targetPosition_; }
  93. /// Return the agent's state.
  94. CrowdAgentState GetAgentState() const;
  95. /// Return the agent's target state.
  96. CrowdTargetState GetTargetState() const;
  97. /// Return true when the node's position should be updated by the CrowdManager.
  98. bool GetUpdateNodePosition() const { return updateNodePosition_; }
  99. /// Return the agent id.
  100. int GetAgentCrowdId() const { return agentCrowdId_; }
  101. /// Get the agent's max velocity.
  102. float GetMaxSpeed() const { return maxSpeed_; }
  103. /// Get the agent's max acceleration.
  104. float GetMaxAccel() const { return maxAccel_; }
  105. /// Get the agent's radius.
  106. float GetRadius() const { return radius_; }
  107. /// Get the agent's height.
  108. float GetHeight() const { return height_; }
  109. /// Get the agent's navigation quality.
  110. NavigationQuality GetNavigationQuality() const {return navQuality_; }
  111. /// Get the agent's navigation pushiness.
  112. NavigationPushiness GetNavigationPushiness() const {return navPushiness_; }
  113. /// Return true when the agent has arrived at its target.
  114. bool HasArrived() const;
  115. /// Get serialized data of internal state.
  116. PODVector<unsigned char> GetAgentDataAttr() const;
  117. /// Set serialized data of internal state.
  118. void SetAgentDataAttr(const PODVector<unsigned char>& value);
  119. protected:
  120. /// Update the nodes position if updateNodePosition is set. Is called in DetourCrowdManager::Update().
  121. virtual void OnCrowdAgentReposition(const Vector3& newPos, const Vector3& newVel);
  122. /// Handle node being assigned.
  123. virtual void OnNodeSet(Node* node);
  124. /// Handle node being assigned.
  125. virtual void OnSceneSet(Scene* scene);
  126. /// \todo Handle node transform being dirtied.
  127. virtual void OnMarkedDirty(Node* node);
  128. /// Get internal Detour crowd agent.
  129. const dtCrowdAgent* GetDetourCrowdAgent() const;
  130. private:
  131. /// Create or re-add.
  132. void AddAgentToCrowd();
  133. /// Remove.
  134. void RemoveAgentFromCrowd();
  135. /// Detour crowd manager.
  136. WeakPtr<DetourCrowdManager> crowdManager_;
  137. /// Flag indicating agent is in DetourCrowd.
  138. bool inCrowd_;
  139. /// DetourCrowd reference to this agent.
  140. int agentCrowdId_;
  141. /// Reference to poly closest to requested target position.
  142. unsigned targetRef_;
  143. /// Actual target position, closest to that requested.
  144. Vector3 targetPosition_;
  145. /// Flag indicating the node's position should be updated by Detour crowd manager.
  146. bool updateNodePosition_;
  147. /// Agent's max acceleration.
  148. float maxAccel_;
  149. /// Agent's max Velocity.
  150. float maxSpeed_;
  151. /// Agent's radius, if 0 the navigation mesh's setting will be used.
  152. float radius_;
  153. /// Agent's height, if 0 the navigation mesh's setting will be used.
  154. float height_;
  155. /// Agent's assigned navigation filter type, the actual filter is owned by the DetourCrowdManager the agent belongs to.
  156. unsigned filterType_;
  157. /// Agent's NavigationAvoidanceQuality.
  158. NavigationQuality navQuality_;
  159. /// Agent's Navigation Pushiness.
  160. NavigationPushiness navPushiness_;
  161. /// Agent's previous position used to check for position changes.
  162. Vector3 previousPosition_;
  163. /// Agent's previous target state used to check for state changes.
  164. CrowdTargetState previousTargetState_;
  165. /// Agent's previous agent state used to check for state changes.
  166. CrowdAgentState previousAgentState_;
  167. /// Internal flag to ignore transform changes because it came from us, used in OnCrowdAgentReposition().
  168. bool ignoreTransformChanges_;
  169. };
  170. }