Navigation.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Copyright (c) 2008-2021 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. /// Navigation example.
  31. /// This sample demonstrates:
  32. /// - Generating a navigation mesh into the scene
  33. /// - Performing path queries to the navigation mesh
  34. /// - Rebuilding the navigation mesh partially when adding or removing objects
  35. /// - Visualizing custom debug geometry
  36. /// - Raycasting drawable components
  37. /// - Making a node follow the Detour path
  38. class Navigation : public Sample
  39. {
  40. URHO3D_OBJECT(Navigation, Sample);
  41. public:
  42. /// Construct.
  43. explicit Navigation(Context* context);
  44. /// Setup after engine initialization and before running the main loop.
  45. void Start() override;
  46. protected:
  47. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  48. String GetScreenJoystickPatchString() const override { return
  49. "<patch>"
  50. " <add sel=\"/element\">"
  51. " <element type=\"Button\">"
  52. " <attribute name=\"Name\" value=\"Button3\" />"
  53. " <attribute name=\"Position\" value=\"-120 -120\" />"
  54. " <attribute name=\"Size\" value=\"96 96\" />"
  55. " <attribute name=\"Horiz Alignment\" value=\"Right\" />"
  56. " <attribute name=\"Vert Alignment\" value=\"Bottom\" />"
  57. " <attribute name=\"Texture\" value=\"Texture2D;Textures/TouchInput.png\" />"
  58. " <attribute name=\"Image Rect\" value=\"96 0 192 96\" />"
  59. " <attribute name=\"Hover Image Offset\" value=\"0 0\" />"
  60. " <attribute name=\"Pressed Image Offset\" value=\"0 0\" />"
  61. " <element type=\"Text\">"
  62. " <attribute name=\"Name\" value=\"Label\" />"
  63. " <attribute name=\"Horiz Alignment\" value=\"Center\" />"
  64. " <attribute name=\"Vert Alignment\" value=\"Center\" />"
  65. " <attribute name=\"Color\" value=\"0 0 0 1\" />"
  66. " <attribute name=\"Text\" value=\"Teleport\" />"
  67. " </element>"
  68. " <element type=\"Text\">"
  69. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  70. " <attribute name=\"Text\" value=\"LSHIFT\" />"
  71. " </element>"
  72. " <element type=\"Text\">"
  73. " <attribute name=\"Name\" value=\"MouseButtonBinding\" />"
  74. " <attribute name=\"Text\" value=\"LEFT\" />"
  75. " </element>"
  76. " </element>"
  77. " <element type=\"Button\">"
  78. " <attribute name=\"Name\" value=\"Button4\" />"
  79. " <attribute name=\"Position\" value=\"-120 -12\" />"
  80. " <attribute name=\"Size\" value=\"96 96\" />"
  81. " <attribute name=\"Horiz Alignment\" value=\"Right\" />"
  82. " <attribute name=\"Vert Alignment\" value=\"Bottom\" />"
  83. " <attribute name=\"Texture\" value=\"Texture2D;Textures/TouchInput.png\" />"
  84. " <attribute name=\"Image Rect\" value=\"96 0 192 96\" />"
  85. " <attribute name=\"Hover Image Offset\" value=\"0 0\" />"
  86. " <attribute name=\"Pressed Image Offset\" value=\"0 0\" />"
  87. " <element type=\"Text\">"
  88. " <attribute name=\"Name\" value=\"Label\" />"
  89. " <attribute name=\"Horiz Alignment\" value=\"Center\" />"
  90. " <attribute name=\"Vert Alignment\" value=\"Center\" />"
  91. " <attribute name=\"Color\" value=\"0 0 0 1\" />"
  92. " <attribute name=\"Text\" value=\"Obstacles\" />"
  93. " </element>"
  94. " <element type=\"Text\">"
  95. " <attribute name=\"Name\" value=\"MouseButtonBinding\" />"
  96. " <attribute name=\"Text\" value=\"MIDDLE\" />"
  97. " </element>"
  98. " </element>"
  99. " </add>"
  100. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  101. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Set</replace>"
  102. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  103. " <element type=\"Text\">"
  104. " <attribute name=\"Name\" value=\"MouseButtonBinding\" />"
  105. " <attribute name=\"Text\" value=\"LEFT\" />"
  106. " </element>"
  107. " </add>"
  108. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />"
  109. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Debug</replace>"
  110. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">"
  111. " <element type=\"Text\">"
  112. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  113. " <attribute name=\"Text\" value=\"SPACE\" />"
  114. " </element>"
  115. " </add>"
  116. "</patch>";
  117. }
  118. private:
  119. /// Construct the scene content.
  120. void CreateScene();
  121. /// Construct user interface elements.
  122. void CreateUI();
  123. /// Set up a viewport for displaying the scene.
  124. void SetupViewport();
  125. /// Subscribe to application-wide logic update and post-render update events.
  126. void SubscribeToEvents();
  127. /// Read input and moves the camera.
  128. void MoveCamera(float timeStep);
  129. /// Set path start or end point.
  130. void SetPathPoint();
  131. /// Add or remove object.
  132. void AddOrRemoveObject();
  133. /// Create a mushroom object at position.
  134. Node* CreateMushroom(const Vector3& pos);
  135. /// Utility function to raycast to the cursor position. Return true if hit
  136. bool Raycast(float maxDistance, Vector3& hitPos, Drawable*& hitDrawable);
  137. /// Make Jack follow the Detour path.
  138. void FollowPath(float timeStep);
  139. /// Toggle navigation mesh streaming.
  140. void ToggleStreaming(bool enabled);
  141. /// Update navigation mesh streaming.
  142. void UpdateStreaming();
  143. /// Save navigation data for streaming.
  144. void SaveNavigationData();
  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. /// Last calculated path.
  150. PODVector<Vector3> currentPath_;
  151. /// Path end position.
  152. Vector3 endPos_;
  153. /// Jack scene node.
  154. SharedPtr<Node> jackNode_;
  155. /// Flag for drawing debug geometry.
  156. bool drawDebug_;
  157. /// Flag for using navigation mesh streaming.
  158. bool useStreaming_;
  159. /// Streaming distance.
  160. int streamingDistance_;
  161. /// Tile data.
  162. HashMap<IntVector2, PODVector<unsigned char> > tileData_;
  163. /// Added tiles.
  164. HashSet<IntVector2> addedTiles_;
  165. };