CrowdNavigation.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2008-2017 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 "Sample.h"
  24. namespace Urho3D
  25. {
  26. class Drawable;
  27. class Node;
  28. class Scene;
  29. }
  30. /// CrowdNavigation example.
  31. /// This sample demonstrates:
  32. /// - Generating a dynamic navigation mesh into the scene
  33. /// - Performing path queries to the navigation mesh
  34. /// - Adding and removing obstacles/agents at runtime
  35. /// - Raycasting drawable components
  36. /// - Crowd movement management
  37. /// - Accessing crowd agents with the crowd manager
  38. /// - Using off-mesh connections to make boxes climbable
  39. /// - Using agents to simulate moving obstacles
  40. class CrowdNavigation : public Sample
  41. {
  42. URHO3D_OBJECT(CrowdNavigation, Sample);
  43. public:
  44. /// Construct.
  45. CrowdNavigation(Context* context);
  46. /// Setup after engine initialization and before running the main loop.
  47. virtual void Start();
  48. protected:
  49. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  50. virtual String GetScreenJoystickPatchString() const { return
  51. "<patch>"
  52. " <add sel=\"/element\">"
  53. " <element type=\"Button\">"
  54. " <attribute name=\"Name\" value=\"Button3\" />"
  55. " <attribute name=\"Position\" value=\"-120 -120\" />"
  56. " <attribute name=\"Size\" value=\"96 96\" />"
  57. " <attribute name=\"Horiz Alignment\" value=\"Right\" />"
  58. " <attribute name=\"Vert Alignment\" value=\"Bottom\" />"
  59. " <attribute name=\"Texture\" value=\"Texture2D;Textures/TouchInput.png\" />"
  60. " <attribute name=\"Image Rect\" value=\"96 0 192 96\" />"
  61. " <attribute name=\"Hover Image Offset\" value=\"0 0\" />"
  62. " <attribute name=\"Pressed Image Offset\" value=\"0 0\" />"
  63. " <element type=\"Text\">"
  64. " <attribute name=\"Name\" value=\"Label\" />"
  65. " <attribute name=\"Horiz Alignment\" value=\"Center\" />"
  66. " <attribute name=\"Vert Alignment\" value=\"Center\" />"
  67. " <attribute name=\"Color\" value=\"0 0 0 1\" />"
  68. " <attribute name=\"Text\" value=\"Spawn\" />"
  69. " </element>"
  70. " <element type=\"Text\">"
  71. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  72. " <attribute name=\"Text\" value=\"LSHIFT\" />"
  73. " </element>"
  74. " <element type=\"Text\">"
  75. " <attribute name=\"Name\" value=\"MouseButtonBinding\" />"
  76. " <attribute name=\"Text\" value=\"LEFT\" />"
  77. " </element>"
  78. " </element>"
  79. " <element type=\"Button\">"
  80. " <attribute name=\"Name\" value=\"Button4\" />"
  81. " <attribute name=\"Position\" value=\"-120 -12\" />"
  82. " <attribute name=\"Size\" value=\"96 96\" />"
  83. " <attribute name=\"Horiz Alignment\" value=\"Right\" />"
  84. " <attribute name=\"Vert Alignment\" value=\"Bottom\" />"
  85. " <attribute name=\"Texture\" value=\"Texture2D;Textures/TouchInput.png\" />"
  86. " <attribute name=\"Image Rect\" value=\"96 0 192 96\" />"
  87. " <attribute name=\"Hover Image Offset\" value=\"0 0\" />"
  88. " <attribute name=\"Pressed Image Offset\" value=\"0 0\" />"
  89. " <element type=\"Text\">"
  90. " <attribute name=\"Name\" value=\"Label\" />"
  91. " <attribute name=\"Horiz Alignment\" value=\"Center\" />"
  92. " <attribute name=\"Vert Alignment\" value=\"Center\" />"
  93. " <attribute name=\"Color\" value=\"0 0 0 1\" />"
  94. " <attribute name=\"Text\" value=\"Obstacles\" />"
  95. " </element>"
  96. " <element type=\"Text\">"
  97. " <attribute name=\"Name\" value=\"MouseButtonBinding\" />"
  98. " <attribute name=\"Text\" value=\"MIDDLE\" />"
  99. " </element>"
  100. " </element>"
  101. " </add>"
  102. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  103. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Set</replace>"
  104. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  105. " <element type=\"Text\">"
  106. " <attribute name=\"Name\" value=\"MouseButtonBinding\" />"
  107. " <attribute name=\"Text\" value=\"LEFT\" />"
  108. " </element>"
  109. " </add>"
  110. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />"
  111. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Debug</replace>"
  112. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">"
  113. " <element type=\"Text\">"
  114. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  115. " <attribute name=\"Text\" value=\"SPACE\" />"
  116. " </element>"
  117. " </add>"
  118. "</patch>";
  119. }
  120. private:
  121. /// Construct the scene content.
  122. void CreateScene();
  123. /// Construct user interface elements.
  124. void CreateUI();
  125. /// Set up a viewport for displaying the scene.
  126. void SetupViewport();
  127. /// Subscribe to application-wide logic update and post-render update events.
  128. void SubscribeToEvents();
  129. /// Read input and moves the camera.
  130. void MoveCamera(float timeStep);
  131. /// Set crowd agents target or spawn another jack.
  132. void SetPathPoint(bool spawning);
  133. /// Add new obstacle or remove existing obstacle/agent.
  134. void AddOrRemoveObject();
  135. /// Create a "Jack" object at position.
  136. void SpawnJack(const Vector3& pos, Node* jackGroup);
  137. /// Create a mushroom object at position.
  138. void CreateMushroom(const Vector3& pos);
  139. /// Create an off-mesh connection for each box to make it climbable.
  140. void CreateBoxOffMeshConnections(DynamicNavigationMesh* navMesh, Node* boxGroup);
  141. /// Create some movable barrels as crowd agents.
  142. void CreateMovingBarrels(DynamicNavigationMesh* navMesh);
  143. /// Utility function to raycast to the cursor position. Return true if hit.
  144. bool Raycast(float maxDistance, Vector3& hitPos, Drawable*& hitDrawable);
  145. /// Handle the logic update event.
  146. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  147. /// Handle the post-render update event.
  148. void HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData);
  149. /// Handle problems with crowd agent placement.
  150. void HandleCrowdAgentFailure(StringHash eventType, VariantMap& eventData);
  151. /// Handle crowd agent reposition.
  152. void HandleCrowdAgentReposition(StringHash eventType, VariantMap& eventData);
  153. /// Handle crowd agent formation.
  154. void HandleCrowdAgentFormation(StringHash eventType, VariantMap& eventData);
  155. /// Flag for drawing debug geometry.
  156. bool drawDebug_;
  157. };