CrowdAgent.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_WAITINGFORPATH,
  34. CROWD_AGENT_TARGET_WAITINGFORQUEUE,
  35. CROWD_AGENT_TARGET_VELOCITY,
  36. CROWD_AGENT_TARGET_ARRIVED
  37. };
  38. enum CrowdAgentState
  39. {
  40. CROWD_AGENT_INVALID = 0, ///< The agent is not in a valid state.
  41. CROWD_AGENT_READY, ///< The agent is traversing a normal navigation mesh polygon.
  42. CROWD_AGENT_TRAVERSINGLINK ///< The agent is traversing an off-mesh connection.
  43. };
  44. /// DetourCrowd Agent, requires a DetourCrowdManager in the scene. Agent's radius and height is set through the navigation mesh.
  45. class ATOMIC_API CrowdAgent : public Component
  46. {
  47. OBJECT(CrowdAgent);
  48. friend class DetourCrowdManager;
  49. public:
  50. /// Construct.
  51. CrowdAgent(Context* context);
  52. /// Destruct.
  53. virtual ~CrowdAgent();
  54. /// Register object factory.
  55. static void RegisterObject(Context* context);
  56. /// Handle enabled/disabled state change.
  57. virtual void OnSetEnabled();
  58. /// Draw debug geometry.
  59. void DrawDebugGeometry(bool);
  60. /// Draw debug feelers.
  61. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  62. /// Set the navigation filter type the agent will use.
  63. void SetNavigationFilterType(unsigned filterTypeID);
  64. /// Submit a new move request for this agent.
  65. bool SetMoveTarget(const Vector3& position);
  66. /// Submit a new move velocity request for this agent.
  67. bool SetMoveVelocity(const Vector3& velocity);
  68. /// 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.
  69. void SetUpdateNodePosition(bool unodepos);
  70. /// Set the agent's max acceleration.
  71. void SetMaxAccel(float val);
  72. /// Set the agent's max velocity.
  73. void SetMaxSpeed(float val);
  74. /// Set the agent's radius.
  75. void SetRadius(float val);
  76. /// Set the agent's height.
  77. void SetHeight(float val);
  78. /// Set the agent's navigation quality.
  79. void SetNavigationQuality(NavigationQuality val);
  80. /// Set the agent's navigation pushiness.
  81. void SetNavigationPushiness(NavigationPushiness val);
  82. /// Get the navigation filter type this agent is using.
  83. unsigned GetNavigationFilterType() const { return filterType_; }
  84. /// Return the agent's position.
  85. Vector3 GetPosition() const;
  86. /// Return the agent's desired velocity.
  87. Vector3 GetDesiredVelocity() const;
  88. /// Return the agent's actual velocity.
  89. Vector3 GetActualVelocity() const;
  90. /// Return the agent's target position.
  91. const Vector3& GetTargetPosition() const { return targetPosition_; }
  92. /// Return the agent's state.
  93. CrowdAgentState GetAgentState() const;
  94. /// Return the agent's target state.
  95. CrowdTargetState GetTargetState() const;
  96. /// Return true when the node's position should be updated by the CrowdManager.
  97. bool GetUpdateNodePosition() const { return updateNodePosition_; }
  98. /// Return the agent id.
  99. int GetAgentCrowdId() const { return agentCrowdId_; }
  100. /// Get the agent's max velocity.
  101. float GetMaxSpeed() const { return maxSpeed_; }
  102. /// Get the agent's max acceleration.
  103. float GetMaxAccel() const { return maxAccel_; }
  104. /// Get the agent's radius.
  105. float GetRadius() const { return radius_; }
  106. /// Get the agent's height.
  107. float GetHeight() const { return height_; }
  108. /// Get the agent's navigation quality.
  109. NavigationQuality GetNavigationQuality() const {return navQuality_; }
  110. /// Get the agent's navigation pushiness.
  111. NavigationPushiness GetNavigationPushiness() const {return navPushiness_; }
  112. /// Get serialized data of internal state.
  113. PODVector<unsigned char> GetAgentDataAttr() const;
  114. /// Set serialized data of internal state.
  115. void SetAgentDataAttr(const PODVector<unsigned char>& value);
  116. protected:
  117. /// Update the nodes position if updateNodePosition is set. Is called in DetourCrowdManager::Update().
  118. virtual void OnCrowdAgentReposition(const Vector3& newPos, const Vector3& newDirection);
  119. /// Handle node being assigned.
  120. virtual void OnNodeSet(Node* node);
  121. /// \todo Handle node transform being dirtied.
  122. virtual void OnMarkedDirty(Node* node);
  123. private:
  124. /// Create or re-add.
  125. void AddAgentToCrowd();
  126. /// Remove.
  127. void RemoveAgentFromCrowd();
  128. /// Detour crowd manager.
  129. WeakPtr<DetourCrowdManager> crowdManager_;
  130. /// Flag indicating agent is in DetourCrowd.
  131. bool inCrowd_;
  132. /// DetourCrowd reference to this agent.
  133. int agentCrowdId_;
  134. /// Reference to poly closest to requested target position.
  135. unsigned int targetRef_;
  136. /// Actual target position, closest to that requested.
  137. Vector3 targetPosition_;
  138. /// Flag indicating the node's position should be updated by Detour crowd manager.
  139. bool updateNodePosition_;
  140. /// Agent's max acceleration.
  141. float maxAccel_;
  142. /// Agent's max Velocity.
  143. float maxSpeed_;
  144. /// Agent's radius, if 0 the navigation mesh's setting will be used.
  145. float radius_;
  146. /// Agent's height, if 0 the navigation mesh's setting will be used.
  147. float height_;
  148. /// Agent's assigned navigation filter type, the actual filter is owned by the DetourCrowdManager the agent belongs to.
  149. unsigned filterType_;
  150. /// Agent's NavigationAvoidanceQuality.
  151. NavigationQuality navQuality_;
  152. /// Agent's Navigation Pushiness.
  153. NavigationPushiness navPushiness_;
  154. /// Agent's previous target state used to check for state changes.
  155. CrowdTargetState previousTargetState_;
  156. /// Agent's previous agent state used to check for state changes.
  157. CrowdAgentState previousAgentState_;
  158. /// Internal flag to ignore transform changes because it came from us, used in OnCrowdAgentReposition().
  159. bool ignoreTransformChanges_;
  160. };
  161. }