navigation_agent_2d.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**************************************************************************/
  2. /* navigation_agent_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef NAVIGATION_AGENT_2D_H
  31. #define NAVIGATION_AGENT_2D_H
  32. #include "scene/main/node.h"
  33. #include "servers/navigation/navigation_path_query_parameters_2d.h"
  34. #include "servers/navigation/navigation_path_query_result_2d.h"
  35. class Node2D;
  36. class NavigationAgent2D : public Node {
  37. GDCLASS(NavigationAgent2D, Node);
  38. Node2D *agent_parent = nullptr;
  39. RID agent;
  40. RID map_before_pause;
  41. RID map_override;
  42. bool avoidance_enabled = false;
  43. uint32_t navigation_layers = 1;
  44. NavigationPathQueryParameters2D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters2D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  45. NavigationPathQueryParameters2D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters2D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  46. BitField<NavigationPathQueryParameters2D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
  47. real_t path_desired_distance = 20.0;
  48. real_t target_desired_distance = 10.0;
  49. real_t radius = 10.0;
  50. real_t neighbor_distance = 500.0;
  51. int max_neighbors = 10;
  52. real_t time_horizon = 1.0;
  53. real_t max_speed = 100.0;
  54. real_t path_max_distance = 100.0;
  55. Vector2 target_position;
  56. bool target_position_submitted = false;
  57. Ref<NavigationPathQueryParameters2D> navigation_query;
  58. Ref<NavigationPathQueryResult2D> navigation_result;
  59. int navigation_path_index = 0;
  60. bool velocity_submitted = false;
  61. Vector2 prev_safe_velocity;
  62. /// The submitted target velocity
  63. Vector2 target_velocity;
  64. bool target_reached = false;
  65. bool navigation_finished = true;
  66. // No initialized on purpose
  67. uint32_t update_frame_id = 0;
  68. // Debug properties for exposed bindings
  69. bool debug_enabled = false;
  70. float debug_path_custom_point_size = 4.0;
  71. float debug_path_custom_line_width = -1.0;
  72. bool debug_use_custom = false;
  73. Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
  74. #ifdef DEBUG_ENABLED
  75. // Debug properties internal only
  76. bool debug_path_dirty = true;
  77. RID debug_path_instance;
  78. private:
  79. void _navigation_debug_changed();
  80. void _update_debug_path();
  81. #endif // DEBUG_ENABLED
  82. protected:
  83. static void _bind_methods();
  84. void _notification(int p_what);
  85. public:
  86. NavigationAgent2D();
  87. virtual ~NavigationAgent2D();
  88. RID get_rid() const {
  89. return agent;
  90. }
  91. void set_avoidance_enabled(bool p_enabled);
  92. bool get_avoidance_enabled() const;
  93. void set_agent_parent(Node *p_agent_parent);
  94. void set_navigation_layers(uint32_t p_navigation_layers);
  95. uint32_t get_navigation_layers() const;
  96. void set_navigation_layer_value(int p_layer_number, bool p_value);
  97. bool get_navigation_layer_value(int p_layer_number) const;
  98. void set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm);
  99. NavigationPathQueryParameters2D::PathfindingAlgorithm get_pathfinding_algorithm() const {
  100. return pathfinding_algorithm;
  101. }
  102. void set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing);
  103. NavigationPathQueryParameters2D::PathPostProcessing get_path_postprocessing() const {
  104. return path_postprocessing;
  105. }
  106. void set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_flags);
  107. BitField<NavigationPathQueryParameters2D::PathMetadataFlags> get_path_metadata_flags() const {
  108. return path_metadata_flags;
  109. }
  110. void set_navigation_map(RID p_navigation_map);
  111. RID get_navigation_map() const;
  112. void set_path_desired_distance(real_t p_dd);
  113. real_t get_path_desired_distance() const {
  114. return path_desired_distance;
  115. }
  116. void set_target_desired_distance(real_t p_dd);
  117. real_t get_target_desired_distance() const {
  118. return target_desired_distance;
  119. }
  120. void set_radius(real_t p_radius);
  121. real_t get_radius() const {
  122. return radius;
  123. }
  124. void set_neighbor_distance(real_t p_distance);
  125. real_t get_neighbor_distance() const {
  126. return neighbor_distance;
  127. }
  128. void set_max_neighbors(int p_count);
  129. int get_max_neighbors() const {
  130. return max_neighbors;
  131. }
  132. void set_time_horizon(real_t p_time);
  133. real_t get_time_horizon() const {
  134. return time_horizon;
  135. }
  136. void set_max_speed(real_t p_max_speed);
  137. real_t get_max_speed() const {
  138. return max_speed;
  139. }
  140. void set_path_max_distance(real_t p_pmd);
  141. real_t get_path_max_distance();
  142. void set_target_position(Vector2 p_position);
  143. Vector2 get_target_position() const;
  144. Vector2 get_next_path_position();
  145. Ref<NavigationPathQueryResult2D> get_current_navigation_result() const {
  146. return navigation_result;
  147. }
  148. const Vector<Vector2> &get_current_navigation_path() const {
  149. return navigation_result->get_path();
  150. }
  151. int get_current_navigation_path_index() const {
  152. return navigation_path_index;
  153. }
  154. real_t distance_to_target() const;
  155. bool is_target_reached() const;
  156. bool is_target_reachable();
  157. bool is_navigation_finished();
  158. Vector2 get_final_position();
  159. void set_velocity(Vector2 p_velocity);
  160. void _avoidance_done(Vector3 p_new_velocity);
  161. PackedStringArray get_configuration_warnings() const override;
  162. void set_debug_enabled(bool p_enabled);
  163. bool get_debug_enabled() const;
  164. void set_debug_use_custom(bool p_enabled);
  165. bool get_debug_use_custom() const;
  166. void set_debug_path_custom_color(Color p_color);
  167. Color get_debug_path_custom_color() const;
  168. void set_debug_path_custom_point_size(float p_point_size);
  169. float get_debug_path_custom_point_size() const;
  170. void set_debug_path_custom_line_width(float p_line_width);
  171. float get_debug_path_custom_line_width() const;
  172. private:
  173. void update_navigation();
  174. void _request_repath();
  175. void _check_distance_to_target();
  176. };
  177. #endif // NAVIGATION_AGENT_2D_H