navigation_agent_2d.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /**************************************************************************/
  2. /* navigation_agent_2d.cpp */
  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. #include "navigation_agent_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "scene/2d/navigation/navigation_link_2d.h"
  33. #include "scene/resources/world_2d.h"
  34. #include "servers/navigation_server_2d.h"
  35. void NavigationAgent2D::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent2D::get_rid);
  37. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent2D::set_avoidance_enabled);
  38. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent2D::get_avoidance_enabled);
  39. ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent2D::set_path_desired_distance);
  40. ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent2D::get_path_desired_distance);
  41. ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent2D::set_target_desired_distance);
  42. ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent2D::get_target_desired_distance);
  43. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent2D::set_radius);
  44. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent2D::get_radius);
  45. ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent2D::set_neighbor_distance);
  46. ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent2D::get_neighbor_distance);
  47. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent2D::set_max_neighbors);
  48. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent2D::get_max_neighbors);
  49. ClassDB::bind_method(D_METHOD("set_time_horizon_agents", "time_horizon"), &NavigationAgent2D::set_time_horizon_agents);
  50. ClassDB::bind_method(D_METHOD("get_time_horizon_agents"), &NavigationAgent2D::get_time_horizon_agents);
  51. ClassDB::bind_method(D_METHOD("set_time_horizon_obstacles", "time_horizon"), &NavigationAgent2D::set_time_horizon_obstacles);
  52. ClassDB::bind_method(D_METHOD("get_time_horizon_obstacles"), &NavigationAgent2D::get_time_horizon_obstacles);
  53. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent2D::set_max_speed);
  54. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent2D::get_max_speed);
  55. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent2D::set_path_max_distance);
  56. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent2D::get_path_max_distance);
  57. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent2D::set_navigation_layers);
  58. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent2D::get_navigation_layers);
  59. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent2D::set_navigation_layer_value);
  60. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent2D::get_navigation_layer_value);
  61. ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent2D::set_pathfinding_algorithm);
  62. ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent2D::get_pathfinding_algorithm);
  63. ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent2D::set_path_postprocessing);
  64. ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent2D::get_path_postprocessing);
  65. ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent2D::set_path_metadata_flags);
  66. ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent2D::get_path_metadata_flags);
  67. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent2D::set_navigation_map);
  68. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent2D::get_navigation_map);
  69. ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent2D::set_target_position);
  70. ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent2D::get_target_position);
  71. ClassDB::bind_method(D_METHOD("set_simplify_path", "enabled"), &NavigationAgent2D::set_simplify_path);
  72. ClassDB::bind_method(D_METHOD("get_simplify_path"), &NavigationAgent2D::get_simplify_path);
  73. ClassDB::bind_method(D_METHOD("set_simplify_epsilon", "epsilon"), &NavigationAgent2D::set_simplify_epsilon);
  74. ClassDB::bind_method(D_METHOD("get_simplify_epsilon"), &NavigationAgent2D::get_simplify_epsilon);
  75. ClassDB::bind_method(D_METHOD("set_path_return_max_length", "length"), &NavigationAgent2D::set_path_return_max_length);
  76. ClassDB::bind_method(D_METHOD("get_path_return_max_length"), &NavigationAgent2D::get_path_return_max_length);
  77. ClassDB::bind_method(D_METHOD("set_path_return_max_radius", "radius"), &NavigationAgent2D::set_path_return_max_radius);
  78. ClassDB::bind_method(D_METHOD("get_path_return_max_radius"), &NavigationAgent2D::get_path_return_max_radius);
  79. ClassDB::bind_method(D_METHOD("set_path_search_max_polygons", "max_polygons"), &NavigationAgent2D::set_path_search_max_polygons);
  80. ClassDB::bind_method(D_METHOD("get_path_search_max_polygons"), &NavigationAgent2D::get_path_search_max_polygons);
  81. ClassDB::bind_method(D_METHOD("set_path_search_max_distance", "distance"), &NavigationAgent2D::set_path_search_max_distance);
  82. ClassDB::bind_method(D_METHOD("get_path_search_max_distance"), &NavigationAgent2D::get_path_search_max_distance);
  83. ClassDB::bind_method(D_METHOD("get_path_length"), &NavigationAgent2D::get_path_length);
  84. ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent2D::get_next_path_position);
  85. ClassDB::bind_method(D_METHOD("set_velocity_forced", "velocity"), &NavigationAgent2D::set_velocity_forced);
  86. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity);
  87. ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationAgent2D::get_velocity);
  88. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target);
  89. ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent2D::get_current_navigation_result);
  90. ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent2D::get_current_navigation_path);
  91. ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent2D::get_current_navigation_path_index);
  92. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent2D::is_target_reached);
  93. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent2D::is_target_reachable);
  94. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent2D::is_navigation_finished);
  95. ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent2D::get_final_position);
  96. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done);
  97. ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationAgent2D::set_avoidance_layers);
  98. ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationAgent2D::get_avoidance_layers);
  99. ClassDB::bind_method(D_METHOD("set_avoidance_mask", "mask"), &NavigationAgent2D::set_avoidance_mask);
  100. ClassDB::bind_method(D_METHOD("get_avoidance_mask"), &NavigationAgent2D::get_avoidance_mask);
  101. ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationAgent2D::set_avoidance_layer_value);
  102. ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationAgent2D::get_avoidance_layer_value);
  103. ClassDB::bind_method(D_METHOD("set_avoidance_mask_value", "mask_number", "value"), &NavigationAgent2D::set_avoidance_mask_value);
  104. ClassDB::bind_method(D_METHOD("get_avoidance_mask_value", "mask_number"), &NavigationAgent2D::get_avoidance_mask_value);
  105. ClassDB::bind_method(D_METHOD("set_avoidance_priority", "priority"), &NavigationAgent2D::set_avoidance_priority);
  106. ClassDB::bind_method(D_METHOD("get_avoidance_priority"), &NavigationAgent2D::get_avoidance_priority);
  107. ADD_GROUP("Pathfinding", "");
  108. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
  109. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,1000,0.01,or_greater,suffix:px"), "set_path_desired_distance", "get_path_desired_distance");
  110. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,1000,0.01,or_greater,suffix:px"), "set_target_desired_distance", "get_target_desired_distance");
  111. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "10,1000,1,or_greater,suffix:px"), "set_path_max_distance", "get_path_max_distance");
  112. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  113. ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
  114. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered,None"), "set_path_postprocessing", "get_path_postprocessing");
  115. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_metadata_flags", PROPERTY_HINT_FLAGS, "Include Types,Include RIDs,Include Owners"), "set_path_metadata_flags", "get_path_metadata_flags");
  116. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "simplify_path"), "set_simplify_path", "get_simplify_path");
  117. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "simplify_epsilon", PROPERTY_HINT_RANGE, "0.0,10.0,0.001,or_greater,suffix:px"), "set_simplify_epsilon", "get_simplify_epsilon");
  118. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_return_max_length", PROPERTY_HINT_RANGE, "0.0,10240.0,0.001,or_greater,suffix:px"), "set_path_return_max_length", "get_path_return_max_length");
  119. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_return_max_radius", PROPERTY_HINT_RANGE, "0.0,10240.0,0.001,or_greater,suffix:px"), "set_path_return_max_radius", "get_path_return_max_radius");
  120. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_search_max_polygons", PROPERTY_HINT_RANGE, "0,4096,1,or_greater"), "set_path_search_max_polygons", "get_path_search_max_polygons");
  121. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_search_max_distance"), "set_path_search_max_distance", "get_path_search_max_distance");
  122. ADD_GROUP("Avoidance", "");
  123. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  124. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  125. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,500,0.01,or_greater,suffix:px"), "set_radius", "get_radius");
  126. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_distance", PROPERTY_HINT_RANGE, "0.1,100000,0.01,or_greater,suffix:px"), "set_neighbor_distance", "get_neighbor_distance");
  127. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");
  128. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_agents", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_agents", "get_time_horizon_agents");
  129. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_obstacles", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_obstacles", "get_time_horizon_obstacles");
  130. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.01,100000,0.01,or_greater,suffix:px/s"), "set_max_speed", "get_max_speed");
  131. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  132. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_mask", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_mask", "get_avoidance_mask");
  133. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "avoidance_priority", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_avoidance_priority", "get_avoidance_priority");
  134. ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent2D::set_debug_enabled);
  135. ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent2D::get_debug_enabled);
  136. ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent2D::set_debug_use_custom);
  137. ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent2D::get_debug_use_custom);
  138. ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent2D::set_debug_path_custom_color);
  139. ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent2D::get_debug_path_custom_color);
  140. ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent2D::set_debug_path_custom_point_size);
  141. ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent2D::get_debug_path_custom_point_size);
  142. ClassDB::bind_method(D_METHOD("set_debug_path_custom_line_width", "line_width"), &NavigationAgent2D::set_debug_path_custom_line_width);
  143. ClassDB::bind_method(D_METHOD("get_debug_path_custom_line_width"), &NavigationAgent2D::get_debug_path_custom_line_width);
  144. ADD_GROUP("Debug", "debug_");
  145. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
  146. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
  147. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
  148. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "debug_path_custom_point_size", PROPERTY_HINT_RANGE, "0,50,0.01,or_greater,suffix:px"), "set_debug_path_custom_point_size", "get_debug_path_custom_point_size");
  149. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "debug_path_custom_line_width", PROPERTY_HINT_RANGE, "-1,50,0.01,or_greater,suffix:px"), "set_debug_path_custom_line_width", "get_debug_path_custom_line_width");
  150. ADD_SIGNAL(MethodInfo("path_changed"));
  151. ADD_SIGNAL(MethodInfo("target_reached"));
  152. ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  153. ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  154. ADD_SIGNAL(MethodInfo("navigation_finished"));
  155. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR2, "safe_velocity")));
  156. }
  157. #ifndef DISABLE_DEPRECATED
  158. // Compatibility with Godot 4.0 beta 10 or below.
  159. // Functions in block below all renamed or replaced in 4.0 beta 1X avoidance rework.
  160. bool NavigationAgent2D::_set(const StringName &p_name, const Variant &p_value) {
  161. if (p_name == "time_horizon") {
  162. set_time_horizon_agents(p_value);
  163. return true;
  164. }
  165. if (p_name == "target_location") {
  166. set_target_position(p_value);
  167. return true;
  168. }
  169. return false;
  170. }
  171. bool NavigationAgent2D::_get(const StringName &p_name, Variant &r_ret) const {
  172. if (p_name == "time_horizon") {
  173. r_ret = get_time_horizon_agents();
  174. return true;
  175. }
  176. if (p_name == "target_location") {
  177. r_ret = get_target_position();
  178. return true;
  179. }
  180. return false;
  181. }
  182. #endif // DISABLE_DEPRECATED
  183. void NavigationAgent2D::_notification(int p_what) {
  184. switch (p_what) {
  185. case NOTIFICATION_POST_ENTER_TREE: {
  186. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  187. // cannot use READY as ready does not get called if Node is re-added to SceneTree
  188. set_agent_parent(get_parent());
  189. set_physics_process_internal(true);
  190. if (agent_parent && avoidance_enabled) {
  191. NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  192. }
  193. #ifdef DEBUG_ENABLED
  194. if (NavigationServer2D::get_singleton()->get_debug_enabled()) {
  195. debug_path_dirty = true;
  196. }
  197. #endif // DEBUG_ENABLED
  198. } break;
  199. case NOTIFICATION_PARENTED: {
  200. if (is_inside_tree() && (get_parent() != agent_parent)) {
  201. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  202. // PARENTED notification fires also when Node is added in scripts to a parent
  203. // this would spam transforms fails and world fails while Node is outside SceneTree
  204. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  205. set_agent_parent(get_parent());
  206. set_physics_process_internal(true);
  207. }
  208. } break;
  209. case NOTIFICATION_UNPARENTED: {
  210. // if agent has no parent no point in processing it until reparented
  211. set_agent_parent(nullptr);
  212. set_physics_process_internal(false);
  213. } break;
  214. case NOTIFICATION_EXIT_TREE: {
  215. set_agent_parent(nullptr);
  216. set_physics_process_internal(false);
  217. #ifdef DEBUG_ENABLED
  218. if (debug_path_instance.is_valid()) {
  219. RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, false);
  220. }
  221. #endif // DEBUG_ENABLED
  222. } break;
  223. case NOTIFICATION_SUSPENDED:
  224. case NOTIFICATION_PAUSED: {
  225. if (agent_parent) {
  226. NavigationServer2D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
  227. }
  228. } break;
  229. case NOTIFICATION_UNSUSPENDED: {
  230. if (get_tree()->is_paused()) {
  231. break;
  232. }
  233. [[fallthrough]];
  234. }
  235. case NOTIFICATION_UNPAUSED: {
  236. if (agent_parent) {
  237. NavigationServer2D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
  238. }
  239. } break;
  240. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  241. if (agent_parent && avoidance_enabled) {
  242. NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  243. }
  244. if (agent_parent && target_position_submitted) {
  245. if (velocity_submitted) {
  246. velocity_submitted = false;
  247. if (avoidance_enabled) {
  248. NavigationServer2D::get_singleton()->agent_set_velocity(agent, velocity);
  249. }
  250. }
  251. if (velocity_forced_submitted) {
  252. velocity_forced_submitted = false;
  253. if (avoidance_enabled) {
  254. NavigationServer2D::get_singleton()->agent_set_velocity_forced(agent, velocity_forced);
  255. }
  256. }
  257. }
  258. #ifdef DEBUG_ENABLED
  259. if (debug_path_dirty) {
  260. _update_debug_path();
  261. }
  262. #endif // DEBUG_ENABLED
  263. } break;
  264. }
  265. }
  266. NavigationAgent2D::NavigationAgent2D() {
  267. agent = NavigationServer2D::get_singleton()->agent_create();
  268. NavigationServer2D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  269. NavigationServer2D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  270. NavigationServer2D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
  271. NavigationServer2D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
  272. NavigationServer2D::get_singleton()->agent_set_radius(agent, radius);
  273. NavigationServer2D::get_singleton()->agent_set_max_speed(agent, max_speed);
  274. NavigationServer2D::get_singleton()->agent_set_avoidance_layers(agent, avoidance_layers);
  275. NavigationServer2D::get_singleton()->agent_set_avoidance_mask(agent, avoidance_mask);
  276. NavigationServer2D::get_singleton()->agent_set_avoidance_priority(agent, avoidance_priority);
  277. NavigationServer2D::get_singleton()->agent_set_avoidance_enabled(agent, avoidance_enabled);
  278. if (avoidance_enabled) {
  279. NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));
  280. }
  281. // Preallocate query and result objects to improve performance.
  282. navigation_query = Ref<NavigationPathQueryParameters2D>();
  283. navigation_query.instantiate();
  284. navigation_result = Ref<NavigationPathQueryResult2D>();
  285. navigation_result.instantiate();
  286. #ifdef DEBUG_ENABLED
  287. NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent2D::_navigation_debug_changed));
  288. #endif // DEBUG_ENABLED
  289. }
  290. NavigationAgent2D::~NavigationAgent2D() {
  291. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  292. NavigationServer2D::get_singleton()->free(agent);
  293. agent = RID(); // Pointless
  294. #ifdef DEBUG_ENABLED
  295. NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent2D::_navigation_debug_changed));
  296. ERR_FAIL_NULL(RenderingServer::get_singleton());
  297. if (debug_path_instance.is_valid()) {
  298. RenderingServer::get_singleton()->free(debug_path_instance);
  299. }
  300. #endif // DEBUG_ENABLED
  301. }
  302. void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) {
  303. if (avoidance_enabled == p_enabled) {
  304. return;
  305. }
  306. avoidance_enabled = p_enabled;
  307. if (avoidance_enabled) {
  308. NavigationServer2D::get_singleton()->agent_set_avoidance_enabled(agent, true);
  309. NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));
  310. } else {
  311. NavigationServer2D::get_singleton()->agent_set_avoidance_enabled(agent, false);
  312. NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
  313. }
  314. }
  315. bool NavigationAgent2D::get_avoidance_enabled() const {
  316. return avoidance_enabled;
  317. }
  318. void NavigationAgent2D::set_agent_parent(Node *p_agent_parent) {
  319. if (agent_parent == p_agent_parent) {
  320. return;
  321. }
  322. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  323. NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
  324. if (Object::cast_to<Node2D>(p_agent_parent) != nullptr) {
  325. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  326. agent_parent = Object::cast_to<Node2D>(p_agent_parent);
  327. if (map_override.is_valid()) {
  328. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), map_override);
  329. } else {
  330. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_2d()->get_navigation_map());
  331. }
  332. // create new avoidance callback if enabled
  333. if (avoidance_enabled) {
  334. NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));
  335. }
  336. } else {
  337. agent_parent = nullptr;
  338. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), RID());
  339. }
  340. }
  341. void NavigationAgent2D::set_navigation_layers(uint32_t p_navigation_layers) {
  342. if (navigation_layers == p_navigation_layers) {
  343. return;
  344. }
  345. navigation_layers = p_navigation_layers;
  346. if (target_position_submitted) {
  347. _request_repath();
  348. }
  349. }
  350. uint32_t NavigationAgent2D::get_navigation_layers() const {
  351. return navigation_layers;
  352. }
  353. void NavigationAgent2D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  354. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  355. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  356. uint32_t _navigation_layers = get_navigation_layers();
  357. if (p_value) {
  358. _navigation_layers |= 1 << (p_layer_number - 1);
  359. } else {
  360. _navigation_layers &= ~(1 << (p_layer_number - 1));
  361. }
  362. set_navigation_layers(_navigation_layers);
  363. }
  364. bool NavigationAgent2D::get_navigation_layer_value(int p_layer_number) const {
  365. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  366. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  367. return get_navigation_layers() & (1 << (p_layer_number - 1));
  368. }
  369. void NavigationAgent2D::set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm) {
  370. if (pathfinding_algorithm == p_pathfinding_algorithm) {
  371. return;
  372. }
  373. pathfinding_algorithm = p_pathfinding_algorithm;
  374. navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
  375. }
  376. void NavigationAgent2D::set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing) {
  377. if (path_postprocessing == p_path_postprocessing) {
  378. return;
  379. }
  380. path_postprocessing = p_path_postprocessing;
  381. navigation_query->set_path_postprocessing(path_postprocessing);
  382. }
  383. void NavigationAgent2D::set_simplify_path(bool p_enabled) {
  384. simplify_path = p_enabled;
  385. navigation_query->set_simplify_path(simplify_path);
  386. }
  387. bool NavigationAgent2D::get_simplify_path() const {
  388. return simplify_path;
  389. }
  390. void NavigationAgent2D::set_simplify_epsilon(real_t p_epsilon) {
  391. simplify_epsilon = MAX(0.0, p_epsilon);
  392. navigation_query->set_simplify_epsilon(simplify_epsilon);
  393. }
  394. real_t NavigationAgent2D::get_simplify_epsilon() const {
  395. return simplify_epsilon;
  396. }
  397. void NavigationAgent2D::set_path_return_max_length(float p_length) {
  398. path_return_max_length = MAX(0.0, p_length);
  399. navigation_query->set_path_return_max_length(path_return_max_length);
  400. }
  401. float NavigationAgent2D::get_path_return_max_length() const {
  402. return path_return_max_length;
  403. }
  404. void NavigationAgent2D::set_path_return_max_radius(float p_radius) {
  405. path_return_max_radius = MAX(0.0, p_radius);
  406. navigation_query->set_path_return_max_radius(path_return_max_radius);
  407. }
  408. float NavigationAgent2D::get_path_return_max_radius() const {
  409. return path_return_max_radius;
  410. }
  411. void NavigationAgent2D::set_path_search_max_polygons(int p_max_polygons) {
  412. path_search_max_polygons = p_max_polygons;
  413. navigation_query->set_path_search_max_polygons(path_search_max_polygons);
  414. }
  415. int NavigationAgent2D::get_path_search_max_polygons() const {
  416. return path_search_max_polygons;
  417. }
  418. void NavigationAgent2D::set_path_search_max_distance(float p_distance) {
  419. path_search_max_distance = MAX(0.0, p_distance);
  420. navigation_query->set_path_search_max_distance(path_search_max_distance);
  421. }
  422. float NavigationAgent2D::get_path_search_max_distance() const {
  423. return path_search_max_distance;
  424. }
  425. float NavigationAgent2D::get_path_length() const {
  426. return navigation_result->get_path_length();
  427. }
  428. void NavigationAgent2D::set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_path_metadata_flags) {
  429. if (path_metadata_flags == p_path_metadata_flags) {
  430. return;
  431. }
  432. path_metadata_flags = p_path_metadata_flags;
  433. }
  434. void NavigationAgent2D::set_navigation_map(RID p_navigation_map) {
  435. if (map_override == p_navigation_map) {
  436. return;
  437. }
  438. map_override = p_navigation_map;
  439. NavigationServer2D::get_singleton()->agent_set_map(agent, map_override);
  440. if (target_position_submitted) {
  441. _request_repath();
  442. }
  443. }
  444. RID NavigationAgent2D::get_navigation_map() const {
  445. if (map_override.is_valid()) {
  446. return map_override;
  447. } else if (agent_parent != nullptr) {
  448. return agent_parent->get_world_2d()->get_navigation_map();
  449. }
  450. return RID();
  451. }
  452. void NavigationAgent2D::set_path_desired_distance(real_t p_path_desired_distance) {
  453. if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
  454. return;
  455. }
  456. path_desired_distance = p_path_desired_distance;
  457. }
  458. void NavigationAgent2D::set_target_desired_distance(real_t p_target_desired_distance) {
  459. if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
  460. return;
  461. }
  462. target_desired_distance = p_target_desired_distance;
  463. }
  464. void NavigationAgent2D::set_radius(real_t p_radius) {
  465. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  466. if (Math::is_equal_approx(radius, p_radius)) {
  467. return;
  468. }
  469. radius = p_radius;
  470. NavigationServer2D::get_singleton()->agent_set_radius(agent, radius);
  471. }
  472. void NavigationAgent2D::set_neighbor_distance(real_t p_distance) {
  473. if (Math::is_equal_approx(neighbor_distance, p_distance)) {
  474. return;
  475. }
  476. neighbor_distance = p_distance;
  477. NavigationServer2D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  478. }
  479. void NavigationAgent2D::set_max_neighbors(int p_count) {
  480. if (max_neighbors == p_count) {
  481. return;
  482. }
  483. max_neighbors = p_count;
  484. NavigationServer2D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  485. }
  486. void NavigationAgent2D::set_time_horizon_agents(real_t p_time_horizon) {
  487. ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
  488. if (Math::is_equal_approx(time_horizon_agents, p_time_horizon)) {
  489. return;
  490. }
  491. time_horizon_agents = p_time_horizon;
  492. NavigationServer2D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
  493. }
  494. void NavigationAgent2D::set_time_horizon_obstacles(real_t p_time_horizon) {
  495. ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
  496. if (Math::is_equal_approx(time_horizon_obstacles, p_time_horizon)) {
  497. return;
  498. }
  499. time_horizon_obstacles = p_time_horizon;
  500. NavigationServer2D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
  501. }
  502. void NavigationAgent2D::set_max_speed(real_t p_max_speed) {
  503. ERR_FAIL_COND_MSG(p_max_speed < 0.0, "Max speed must be positive.");
  504. if (Math::is_equal_approx(max_speed, p_max_speed)) {
  505. return;
  506. }
  507. max_speed = p_max_speed;
  508. NavigationServer2D::get_singleton()->agent_set_max_speed(agent, max_speed);
  509. }
  510. void NavigationAgent2D::set_path_max_distance(real_t p_path_max_distance) {
  511. if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
  512. return;
  513. }
  514. path_max_distance = p_path_max_distance;
  515. }
  516. real_t NavigationAgent2D::get_path_max_distance() {
  517. return path_max_distance;
  518. }
  519. void NavigationAgent2D::set_target_position(Vector2 p_position) {
  520. // Intentionally not checking for equality of the parameter, as we want to update the path even if the target position is the same in case the world changed.
  521. // Revisit later when the navigation server can update the path without requesting a new path.
  522. target_position = p_position;
  523. target_position_submitted = true;
  524. _request_repath();
  525. }
  526. Vector2 NavigationAgent2D::get_target_position() const {
  527. return target_position;
  528. }
  529. Vector2 NavigationAgent2D::get_next_path_position() {
  530. _update_navigation();
  531. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  532. if (navigation_path.is_empty()) {
  533. ERR_FAIL_NULL_V_MSG(agent_parent, Vector2(), "The agent has no parent.");
  534. return agent_parent->get_global_position();
  535. } else {
  536. return navigation_path[navigation_path_index];
  537. }
  538. }
  539. real_t NavigationAgent2D::distance_to_target() const {
  540. ERR_FAIL_NULL_V_MSG(agent_parent, 0.0, "The agent has no parent.");
  541. return agent_parent->get_global_position().distance_to(target_position);
  542. }
  543. bool NavigationAgent2D::is_target_reached() const {
  544. return target_reached;
  545. }
  546. bool NavigationAgent2D::is_target_reachable() {
  547. _update_navigation();
  548. return _is_target_reachable();
  549. }
  550. bool NavigationAgent2D::_is_target_reachable() const {
  551. return target_desired_distance >= _get_final_position().distance_to(target_position);
  552. }
  553. bool NavigationAgent2D::is_navigation_finished() {
  554. _update_navigation();
  555. return navigation_finished;
  556. }
  557. Vector2 NavigationAgent2D::get_final_position() {
  558. _update_navigation();
  559. return _get_final_position();
  560. }
  561. Vector2 NavigationAgent2D::_get_final_position() const {
  562. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  563. if (navigation_path.is_empty()) {
  564. return Vector2();
  565. }
  566. return navigation_path[navigation_path.size() - 1];
  567. }
  568. void NavigationAgent2D::set_velocity_forced(Vector2 p_velocity) {
  569. // Intentionally not checking for equality of the parameter.
  570. // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
  571. // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
  572. velocity_forced = p_velocity;
  573. velocity_forced_submitted = true;
  574. }
  575. void NavigationAgent2D::set_velocity(const Vector2 p_velocity) {
  576. velocity = p_velocity;
  577. velocity_submitted = true;
  578. }
  579. void NavigationAgent2D::_avoidance_done(Vector2 p_new_velocity) {
  580. safe_velocity = p_new_velocity;
  581. emit_signal(SNAME("velocity_computed"), safe_velocity);
  582. }
  583. PackedStringArray NavigationAgent2D::get_configuration_warnings() const {
  584. PackedStringArray warnings = Node::get_configuration_warnings();
  585. if (!Object::cast_to<Node2D>(get_parent())) {
  586. warnings.push_back(RTR("The NavigationAgent2D can be used only under a Node2D inheriting parent node."));
  587. }
  588. return warnings;
  589. }
  590. void NavigationAgent2D::_update_navigation() {
  591. if (agent_parent == nullptr) {
  592. return;
  593. }
  594. if (!agent_parent->is_inside_tree()) {
  595. return;
  596. }
  597. if (!target_position_submitted) {
  598. return;
  599. }
  600. Vector2 origin = agent_parent->get_global_position();
  601. bool reload_path = false;
  602. if (NavigationServer2D::get_singleton()->agent_is_map_changed(agent)) {
  603. reload_path = true;
  604. } else if (navigation_result->get_path().is_empty()) {
  605. reload_path = true;
  606. } else {
  607. // Check if too far from the navigation path
  608. if (navigation_path_index > 0) {
  609. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  610. const Vector2 segment_a = navigation_path[navigation_path_index - 1];
  611. const Vector2 segment_b = navigation_path[navigation_path_index];
  612. Vector2 p = Geometry2D::get_closest_point_to_segment(origin, segment_a, segment_b);
  613. if (origin.distance_to(p) >= path_max_distance) {
  614. // To faraway, reload path
  615. reload_path = true;
  616. }
  617. }
  618. }
  619. if (reload_path) {
  620. navigation_query->set_start_position(origin);
  621. navigation_query->set_target_position(target_position);
  622. navigation_query->set_navigation_layers(navigation_layers);
  623. navigation_query->set_metadata_flags(path_metadata_flags);
  624. if (map_override.is_valid()) {
  625. navigation_query->set_map(map_override);
  626. } else {
  627. navigation_query->set_map(agent_parent->get_world_2d()->get_navigation_map());
  628. }
  629. NavigationServer2D::get_singleton()->query_path(navigation_query, navigation_result);
  630. #ifdef DEBUG_ENABLED
  631. debug_path_dirty = true;
  632. #endif // DEBUG_ENABLED
  633. navigation_finished = false;
  634. last_waypoint_reached = false;
  635. navigation_path_index = 0;
  636. emit_signal(SNAME("path_changed"));
  637. }
  638. if (navigation_result->get_path().is_empty()) {
  639. return;
  640. }
  641. // Check if the navigation has already finished.
  642. if (navigation_finished) {
  643. return;
  644. }
  645. // Check if we reached the target.
  646. if (_is_within_target_distance(origin)) {
  647. // Emit waypoint_reached in case we also moved within distance of a waypoint.
  648. _advance_waypoints(origin);
  649. _transition_to_target_reached();
  650. _transition_to_navigation_finished();
  651. } else {
  652. // Advance waypoints if possible.
  653. _advance_waypoints(origin);
  654. // Keep navigation running even after reaching the last waypoint if the target is reachable.
  655. if (last_waypoint_reached && !_is_target_reachable()) {
  656. _transition_to_navigation_finished();
  657. }
  658. }
  659. }
  660. void NavigationAgent2D::_advance_waypoints(const Vector2 &p_origin) {
  661. if (last_waypoint_reached) {
  662. return;
  663. }
  664. // Advance to the farthest possible waypoint.
  665. while (_is_within_waypoint_distance(p_origin)) {
  666. _trigger_waypoint_reached();
  667. if (_is_last_waypoint()) {
  668. last_waypoint_reached = true;
  669. break;
  670. }
  671. _move_to_next_waypoint();
  672. }
  673. }
  674. void NavigationAgent2D::_request_repath() {
  675. navigation_result->reset();
  676. target_reached = false;
  677. navigation_finished = false;
  678. last_waypoint_reached = false;
  679. }
  680. bool NavigationAgent2D::_is_last_waypoint() const {
  681. return navigation_path_index == navigation_result->get_path().size() - 1;
  682. }
  683. void NavigationAgent2D::_move_to_next_waypoint() {
  684. navigation_path_index += 1;
  685. }
  686. bool NavigationAgent2D::_is_within_waypoint_distance(const Vector2 &p_origin) const {
  687. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  688. return p_origin.distance_to(navigation_path[navigation_path_index]) < path_desired_distance;
  689. }
  690. bool NavigationAgent2D::_is_within_target_distance(const Vector2 &p_origin) const {
  691. return p_origin.distance_to(target_position) < target_desired_distance;
  692. }
  693. void NavigationAgent2D::_trigger_waypoint_reached() {
  694. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  695. const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
  696. const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
  697. const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
  698. Dictionary details;
  699. const Vector2 waypoint = navigation_path[navigation_path_index];
  700. details[CoreStringName(position)] = waypoint;
  701. int waypoint_type = -1;
  702. if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
  703. const NavigationPathQueryResult2D::PathSegmentType type = NavigationPathQueryResult2D::PathSegmentType(navigation_path_types[navigation_path_index]);
  704. details[SNAME("type")] = type;
  705. waypoint_type = type;
  706. }
  707. if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
  708. details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
  709. }
  710. if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
  711. const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
  712. // Get a reference to the owning object.
  713. Object *owner = nullptr;
  714. if (waypoint_owner_id.is_valid()) {
  715. owner = ObjectDB::get_instance(waypoint_owner_id);
  716. }
  717. details[SNAME("owner")] = owner;
  718. if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {
  719. const NavigationLink2D *navlink = Object::cast_to<NavigationLink2D>(owner);
  720. if (navlink) {
  721. Vector2 link_global_start_position = navlink->get_global_start_position();
  722. Vector2 link_global_end_position = navlink->get_global_end_position();
  723. if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
  724. details[SNAME("link_entry_position")] = link_global_start_position;
  725. details[SNAME("link_exit_position")] = link_global_end_position;
  726. } else {
  727. details[SNAME("link_entry_position")] = link_global_end_position;
  728. details[SNAME("link_exit_position")] = link_global_start_position;
  729. }
  730. }
  731. }
  732. }
  733. // Emit a signal for the waypoint.
  734. emit_signal(SNAME("waypoint_reached"), details);
  735. // Emit a signal if we've reached a navigation link.
  736. if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {
  737. emit_signal(SNAME("link_reached"), details);
  738. }
  739. }
  740. void NavigationAgent2D::_transition_to_navigation_finished() {
  741. navigation_finished = true;
  742. target_position_submitted = false;
  743. if (avoidance_enabled) {
  744. NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  745. NavigationServer2D::get_singleton()->agent_set_velocity(agent, Vector2(0.0, 0.0));
  746. NavigationServer2D::get_singleton()->agent_set_velocity_forced(agent, Vector2(0.0, 0.0));
  747. }
  748. emit_signal(SNAME("navigation_finished"));
  749. }
  750. void NavigationAgent2D::_transition_to_target_reached() {
  751. target_reached = true;
  752. emit_signal(SNAME("target_reached"));
  753. }
  754. void NavigationAgent2D::set_avoidance_layers(uint32_t p_layers) {
  755. avoidance_layers = p_layers;
  756. NavigationServer2D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);
  757. }
  758. uint32_t NavigationAgent2D::get_avoidance_layers() const {
  759. return avoidance_layers;
  760. }
  761. void NavigationAgent2D::set_avoidance_mask(uint32_t p_mask) {
  762. avoidance_mask = p_mask;
  763. NavigationServer2D::get_singleton()->agent_set_avoidance_mask(get_rid(), p_mask);
  764. }
  765. uint32_t NavigationAgent2D::get_avoidance_mask() const {
  766. return avoidance_mask;
  767. }
  768. void NavigationAgent2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  769. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  770. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  771. uint32_t avoidance_layers_new = get_avoidance_layers();
  772. if (p_value) {
  773. avoidance_layers_new |= 1 << (p_layer_number - 1);
  774. } else {
  775. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  776. }
  777. set_avoidance_layers(avoidance_layers_new);
  778. }
  779. bool NavigationAgent2D::get_avoidance_layer_value(int p_layer_number) const {
  780. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  781. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  782. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  783. }
  784. void NavigationAgent2D::set_avoidance_mask_value(int p_mask_number, bool p_value) {
  785. ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");
  786. ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");
  787. uint32_t mask = get_avoidance_mask();
  788. if (p_value) {
  789. mask |= 1 << (p_mask_number - 1);
  790. } else {
  791. mask &= ~(1 << (p_mask_number - 1));
  792. }
  793. set_avoidance_mask(mask);
  794. }
  795. bool NavigationAgent2D::get_avoidance_mask_value(int p_mask_number) const {
  796. ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  797. ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  798. return get_avoidance_mask() & (1 << (p_mask_number - 1));
  799. }
  800. void NavigationAgent2D::set_avoidance_priority(real_t p_priority) {
  801. ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  802. ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  803. avoidance_priority = p_priority;
  804. NavigationServer2D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);
  805. }
  806. real_t NavigationAgent2D::get_avoidance_priority() const {
  807. return avoidance_priority;
  808. }
  809. ////////DEBUG////////////////////////////////////////////////////////////
  810. void NavigationAgent2D::set_debug_enabled(bool p_enabled) {
  811. #ifdef DEBUG_ENABLED
  812. if (debug_enabled == p_enabled) {
  813. return;
  814. }
  815. debug_enabled = p_enabled;
  816. debug_path_dirty = true;
  817. #endif // DEBUG_ENABLED
  818. }
  819. bool NavigationAgent2D::get_debug_enabled() const {
  820. return debug_enabled;
  821. }
  822. void NavigationAgent2D::set_debug_use_custom(bool p_enabled) {
  823. #ifdef DEBUG_ENABLED
  824. if (debug_use_custom == p_enabled) {
  825. return;
  826. }
  827. debug_use_custom = p_enabled;
  828. debug_path_dirty = true;
  829. #endif // DEBUG_ENABLED
  830. }
  831. bool NavigationAgent2D::get_debug_use_custom() const {
  832. return debug_use_custom;
  833. }
  834. void NavigationAgent2D::set_debug_path_custom_color(Color p_color) {
  835. #ifdef DEBUG_ENABLED
  836. if (debug_path_custom_color == p_color) {
  837. return;
  838. }
  839. debug_path_custom_color = p_color;
  840. debug_path_dirty = true;
  841. #endif // DEBUG_ENABLED
  842. }
  843. Color NavigationAgent2D::get_debug_path_custom_color() const {
  844. return debug_path_custom_color;
  845. }
  846. void NavigationAgent2D::set_debug_path_custom_point_size(float p_point_size) {
  847. #ifdef DEBUG_ENABLED
  848. if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
  849. return;
  850. }
  851. debug_path_custom_point_size = MAX(0.0, p_point_size);
  852. debug_path_dirty = true;
  853. #endif // DEBUG_ENABLED
  854. }
  855. float NavigationAgent2D::get_debug_path_custom_point_size() const {
  856. return debug_path_custom_point_size;
  857. }
  858. void NavigationAgent2D::set_debug_path_custom_line_width(float p_line_width) {
  859. #ifdef DEBUG_ENABLED
  860. if (Math::is_equal_approx(debug_path_custom_line_width, p_line_width)) {
  861. return;
  862. }
  863. debug_path_custom_line_width = p_line_width;
  864. debug_path_dirty = true;
  865. #endif // DEBUG_ENABLED
  866. }
  867. float NavigationAgent2D::get_debug_path_custom_line_width() const {
  868. return debug_path_custom_line_width;
  869. }
  870. #ifdef DEBUG_ENABLED
  871. void NavigationAgent2D::_navigation_debug_changed() {
  872. debug_path_dirty = true;
  873. }
  874. void NavigationAgent2D::_update_debug_path() {
  875. if (!debug_path_dirty) {
  876. return;
  877. }
  878. debug_path_dirty = false;
  879. if (!debug_path_instance.is_valid()) {
  880. debug_path_instance = RenderingServer::get_singleton()->canvas_item_create();
  881. }
  882. RenderingServer::get_singleton()->canvas_item_clear(debug_path_instance);
  883. if (!(debug_enabled && NavigationServer2D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
  884. return;
  885. }
  886. if (!(agent_parent && agent_parent->is_inside_tree())) {
  887. return;
  888. }
  889. RenderingServer::get_singleton()->canvas_item_set_parent(debug_path_instance, agent_parent->get_canvas());
  890. RenderingServer::get_singleton()->canvas_item_set_z_index(debug_path_instance, RS::CANVAS_ITEM_Z_MAX - 1);
  891. RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
  892. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  893. if (navigation_path.size() <= 1) {
  894. return;
  895. }
  896. Color debug_path_color = NavigationServer2D::get_singleton()->get_debug_navigation_agent_path_color();
  897. if (debug_use_custom) {
  898. debug_path_color = debug_path_custom_color;
  899. }
  900. Vector<Color> debug_path_colors;
  901. debug_path_colors.resize(navigation_path.size());
  902. debug_path_colors.fill(debug_path_color);
  903. RenderingServer::get_singleton()->canvas_item_add_polyline(debug_path_instance, navigation_path, debug_path_colors, debug_path_custom_line_width, false);
  904. if (debug_path_custom_point_size <= 0.0) {
  905. return;
  906. }
  907. float point_size = NavigationServer2D::get_singleton()->get_debug_navigation_agent_path_point_size();
  908. float half_point_size = point_size * 0.5;
  909. if (debug_use_custom) {
  910. point_size = debug_path_custom_point_size;
  911. half_point_size = debug_path_custom_point_size * 0.5;
  912. }
  913. for (int i = 0; i < navigation_path.size(); i++) {
  914. const Vector2 &vert = navigation_path[i];
  915. Rect2 path_point_rect = Rect2(vert.x - half_point_size, vert.y - half_point_size, point_size, point_size);
  916. RenderingServer::get_singleton()->canvas_item_add_rect(debug_path_instance, path_point_rect, debug_path_color);
  917. }
  918. }
  919. #endif // DEBUG_ENABLED