Navigation.h 7.2 KB

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