navigation_agent_2d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/engine.h"
  32. #include "scene/2d/navigation_2d.h"
  33. #include "servers/navigation_2d_server.h"
  34. void NavigationAgent2D::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent2D::get_rid);
  36. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent2D::set_avoidance_enabled);
  37. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent2D::get_avoidance_enabled);
  38. ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent2D::set_path_desired_distance);
  39. ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent2D::get_path_desired_distance);
  40. ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent2D::set_target_desired_distance);
  41. ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent2D::get_target_desired_distance);
  42. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent2D::set_radius);
  43. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent2D::get_radius);
  44. ClassDB::bind_method(D_METHOD("set_navigation", "navigation"), &NavigationAgent2D::set_navigation_node);
  45. ClassDB::bind_method(D_METHOD("get_navigation"), &NavigationAgent2D::get_navigation_node);
  46. ClassDB::bind_method(D_METHOD("set_neighbor_dist", "neighbor_dist"), &NavigationAgent2D::set_neighbor_dist);
  47. ClassDB::bind_method(D_METHOD("get_neighbor_dist"), &NavigationAgent2D::get_neighbor_dist);
  48. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent2D::set_max_neighbors);
  49. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent2D::get_max_neighbors);
  50. ClassDB::bind_method(D_METHOD("set_time_horizon", "time_horizon"), &NavigationAgent2D::set_time_horizon);
  51. ClassDB::bind_method(D_METHOD("get_time_horizon"), &NavigationAgent2D::get_time_horizon);
  52. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent2D::set_max_speed);
  53. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent2D::get_max_speed);
  54. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent2D::set_path_max_distance);
  55. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent2D::get_path_max_distance);
  56. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent2D::set_navigation_layers);
  57. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent2D::get_navigation_layers);
  58. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent2D::set_navigation_map);
  59. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent2D::get_navigation_map);
  60. ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent2D::set_target_location);
  61. ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent2D::get_target_location);
  62. ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent2D::get_next_location);
  63. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target);
  64. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity);
  65. ClassDB::bind_method(D_METHOD("get_nav_path"), &NavigationAgent2D::get_nav_path);
  66. ClassDB::bind_method(D_METHOD("get_nav_path_index"), &NavigationAgent2D::get_nav_path_index);
  67. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent2D::is_target_reached);
  68. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent2D::is_target_reachable);
  69. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent2D::is_navigation_finished);
  70. ClassDB::bind_method(D_METHOD("get_final_location"), &NavigationAgent2D::get_final_location);
  71. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done);
  72. ADD_GROUP("Pathfinding", "");
  73. ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_path_desired_distance", "get_path_desired_distance");
  74. ADD_PROPERTY(PropertyInfo(Variant::REAL, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01"), "set_target_desired_distance", "get_target_desired_distance");
  75. ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_max_distance", PROPERTY_HINT_RANGE, "10,100,1"), "set_path_max_distance", "get_path_max_distance");
  76. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  77. ADD_GROUP("Avoidance", "");
  78. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  79. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.1,500,0.01"), "set_radius", "get_radius");
  80. ADD_PROPERTY(PropertyInfo(Variant::REAL, "neighbor_dist", PROPERTY_HINT_RANGE, "0.1,100000,0.01"), "set_neighbor_dist", "get_neighbor_dist");
  81. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1"), "set_max_neighbors", "get_max_neighbors");
  82. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_horizon", PROPERTY_HINT_RANGE, "0.1,10000,0.01"), "set_time_horizon", "get_time_horizon");
  83. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_speed", PROPERTY_HINT_RANGE, "0.1,100000,0.01"), "set_max_speed", "get_max_speed");
  84. ADD_SIGNAL(MethodInfo("path_changed"));
  85. ADD_SIGNAL(MethodInfo("target_reached"));
  86. ADD_SIGNAL(MethodInfo("navigation_finished"));
  87. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR2, "safe_velocity")));
  88. }
  89. void NavigationAgent2D::_notification(int p_what) {
  90. switch (p_what) {
  91. case NOTIFICATION_POST_ENTER_TREE: {
  92. // Search the navigation node and set it
  93. {
  94. Navigation2D *nav = nullptr;
  95. Node *p = get_parent();
  96. while (p != nullptr) {
  97. nav = Object::cast_to<Navigation2D>(p);
  98. if (nav != nullptr) {
  99. p = nullptr;
  100. } else {
  101. p = p->get_parent();
  102. }
  103. }
  104. set_navigation(nav);
  105. }
  106. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  107. // cannot use READY as ready does not get called if Node is readded to SceneTree
  108. set_agent_parent(get_parent());
  109. set_physics_process_internal(true);
  110. } break;
  111. case NOTIFICATION_PARENTED: {
  112. if (is_inside_tree() && (get_parent() != agent_parent)) {
  113. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  114. // PARENTED notification fires also when Node is added in scripts to a parent
  115. // this would spam transforms fails and world fails while Node is outside SceneTree
  116. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  117. set_agent_parent(get_parent());
  118. set_physics_process_internal(true);
  119. }
  120. } break;
  121. case NOTIFICATION_UNPARENTED: {
  122. // if agent has no parent no point in processing it until reparented
  123. set_agent_parent(nullptr);
  124. set_physics_process_internal(false);
  125. } break;
  126. case NOTIFICATION_PAUSED: {
  127. if (agent_parent && !agent_parent->can_process()) {
  128. map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
  129. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
  130. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  131. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  132. map_before_pause = RID();
  133. }
  134. } break;
  135. case NOTIFICATION_UNPAUSED: {
  136. if (agent_parent && !agent_parent->can_process()) {
  137. map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
  138. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
  139. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  140. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  141. map_before_pause = RID();
  142. }
  143. } break;
  144. case NOTIFICATION_EXIT_TREE: {
  145. set_agent_parent(nullptr);
  146. set_navigation(nullptr);
  147. set_physics_process_internal(false);
  148. } break;
  149. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  150. if (agent_parent) {
  151. if (avoidance_enabled) {
  152. // agent_position on NavigationServer is avoidance only and has nothing to do with pathfinding
  153. // no point in flooding NavigationServer queue with agent position updates that get send to the void if avoidance is not used
  154. Navigation2DServer::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  155. }
  156. _check_distance_to_target();
  157. }
  158. } break;
  159. }
  160. }
  161. NavigationAgent2D::NavigationAgent2D() :
  162. agent_parent(nullptr),
  163. navigation(nullptr),
  164. agent(RID()),
  165. avoidance_enabled(false),
  166. target_desired_distance(1.0),
  167. path_max_distance(3.0),
  168. velocity_submitted(false),
  169. target_reached(false),
  170. navigation_finished(true) {
  171. agent = Navigation2DServer::get_singleton()->agent_create();
  172. set_neighbor_dist(500.0);
  173. set_max_neighbors(10);
  174. set_time_horizon(20.0);
  175. set_radius(10.0);
  176. set_max_speed(200.0);
  177. }
  178. NavigationAgent2D::~NavigationAgent2D() {
  179. Navigation2DServer::get_singleton()->free(agent);
  180. agent = RID(); // Pointless
  181. }
  182. void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) {
  183. avoidance_enabled = p_enabled;
  184. if (avoidance_enabled) {
  185. Navigation2DServer::get_singleton()->agent_set_callback(agent, this, "_avoidance_done");
  186. } else {
  187. Navigation2DServer::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done");
  188. }
  189. }
  190. bool NavigationAgent2D::get_avoidance_enabled() const {
  191. return avoidance_enabled;
  192. }
  193. void NavigationAgent2D::set_agent_parent(Node *p_agent_parent) {
  194. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  195. Navigation2DServer::get_singleton()->agent_set_callback(agent, nullptr, "_avoidance_done");
  196. if (Object::cast_to<Node2D>(p_agent_parent) != nullptr) {
  197. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  198. agent_parent = Object::cast_to<Node2D>(p_agent_parent);
  199. if (map_override.is_valid()) {
  200. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_override);
  201. } else if (navigation != nullptr) {
  202. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), navigation->get_rid());
  203. } else {
  204. // no navigation node found in parent nodes, use default navigation map from world resource
  205. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_2d()->get_navigation_map());
  206. }
  207. // create new avoidance callback if enabled
  208. set_avoidance_enabled(avoidance_enabled);
  209. } else {
  210. agent_parent = nullptr;
  211. Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
  212. }
  213. }
  214. void NavigationAgent2D::set_navigation(Navigation2D *p_nav) {
  215. if (navigation == p_nav) {
  216. return; // Pointless
  217. }
  218. navigation = p_nav;
  219. Navigation2DServer::get_singleton()->agent_set_map(agent, navigation == nullptr ? RID() : navigation->get_rid());
  220. }
  221. void NavigationAgent2D::set_navigation_node(Node *p_nav) {
  222. Navigation2D *nav = Object::cast_to<Navigation2D>(p_nav);
  223. ERR_FAIL_NULL(nav);
  224. set_navigation(nav);
  225. }
  226. Node *NavigationAgent2D::get_navigation_node() const {
  227. return Object::cast_to<Node>(navigation);
  228. }
  229. void NavigationAgent2D::set_navigation_layers(uint32_t p_navigation_layers) {
  230. bool _navigation_layers_changed = navigation_layers != p_navigation_layers;
  231. navigation_layers = p_navigation_layers;
  232. if (_navigation_layers_changed) {
  233. _request_repath();
  234. }
  235. }
  236. uint32_t NavigationAgent2D::get_navigation_layers() const {
  237. return navigation_layers;
  238. }
  239. void NavigationAgent2D::set_navigation_map(RID p_navigation_map) {
  240. map_override = p_navigation_map;
  241. Navigation2DServer::get_singleton()->agent_set_map(agent, map_override);
  242. _request_repath();
  243. }
  244. RID NavigationAgent2D::get_navigation_map() const {
  245. if (map_override.is_valid()) {
  246. return map_override;
  247. } else if (navigation != nullptr) {
  248. return navigation->get_rid();
  249. } else if (agent_parent != nullptr) {
  250. return agent_parent->get_world_2d()->get_navigation_map();
  251. }
  252. return RID();
  253. }
  254. void NavigationAgent2D::set_path_desired_distance(real_t p_dd) {
  255. path_desired_distance = p_dd;
  256. }
  257. void NavigationAgent2D::set_target_desired_distance(real_t p_dd) {
  258. target_desired_distance = p_dd;
  259. }
  260. void NavigationAgent2D::set_radius(real_t p_radius) {
  261. radius = p_radius;
  262. Navigation2DServer::get_singleton()->agent_set_radius(agent, radius);
  263. }
  264. void NavigationAgent2D::set_neighbor_dist(real_t p_dist) {
  265. neighbor_dist = p_dist;
  266. Navigation2DServer::get_singleton()->agent_set_neighbor_dist(agent, neighbor_dist);
  267. }
  268. void NavigationAgent2D::set_max_neighbors(int p_count) {
  269. max_neighbors = p_count;
  270. Navigation2DServer::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  271. }
  272. void NavigationAgent2D::set_time_horizon(real_t p_time) {
  273. time_horizon = p_time;
  274. Navigation2DServer::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  275. }
  276. void NavigationAgent2D::set_max_speed(real_t p_max_speed) {
  277. max_speed = p_max_speed;
  278. Navigation2DServer::get_singleton()->agent_set_max_speed(agent, max_speed);
  279. }
  280. void NavigationAgent2D::set_path_max_distance(real_t p_pmd) {
  281. path_max_distance = p_pmd;
  282. }
  283. real_t NavigationAgent2D::get_path_max_distance() {
  284. return path_max_distance;
  285. }
  286. void NavigationAgent2D::set_target_location(Vector2 p_location) {
  287. target_location = p_location;
  288. _request_repath();
  289. }
  290. Vector2 NavigationAgent2D::get_target_location() const {
  291. return target_location;
  292. }
  293. Vector2 NavigationAgent2D::get_next_location() {
  294. update_navigation();
  295. if (navigation_path.size() == 0) {
  296. ERR_FAIL_COND_V(agent_parent == nullptr, Vector2());
  297. return agent_parent->get_global_transform().get_origin();
  298. } else {
  299. return navigation_path[nav_path_index];
  300. }
  301. }
  302. real_t NavigationAgent2D::distance_to_target() const {
  303. ERR_FAIL_COND_V(agent_parent == nullptr, 0.0);
  304. return agent_parent->get_global_transform().get_origin().distance_to(target_location);
  305. }
  306. bool NavigationAgent2D::is_target_reached() const {
  307. return target_reached;
  308. }
  309. bool NavigationAgent2D::is_target_reachable() {
  310. return target_desired_distance >= get_final_location().distance_to(target_location);
  311. }
  312. bool NavigationAgent2D::is_navigation_finished() {
  313. update_navigation();
  314. return navigation_finished;
  315. }
  316. Vector2 NavigationAgent2D::get_final_location() {
  317. update_navigation();
  318. if (navigation_path.size() == 0) {
  319. return Vector2();
  320. }
  321. return navigation_path[navigation_path.size() - 1];
  322. }
  323. void NavigationAgent2D::set_velocity(Vector2 p_velocity) {
  324. target_velocity = p_velocity;
  325. Navigation2DServer::get_singleton()->agent_set_target_velocity(agent, target_velocity);
  326. Navigation2DServer::get_singleton()->agent_set_velocity(agent, prev_safe_velocity);
  327. velocity_submitted = true;
  328. }
  329. void NavigationAgent2D::_avoidance_done(Vector3 p_new_velocity) {
  330. const Vector2 velocity = Vector2(p_new_velocity.x, p_new_velocity.z);
  331. prev_safe_velocity = velocity;
  332. if (!velocity_submitted) {
  333. target_velocity = Vector2();
  334. return;
  335. }
  336. velocity_submitted = false;
  337. emit_signal("velocity_computed", velocity);
  338. }
  339. String NavigationAgent2D::get_configuration_warning() const {
  340. String warning = Node::get_configuration_warning();
  341. if (!Object::cast_to<Node2D>(get_parent())) {
  342. if (warning != String()) {
  343. warning += "\n\n";
  344. }
  345. warning += TTR("The NavigationAgent2D can be used only under a Node2D inheriting parent node.");
  346. }
  347. return warning;
  348. }
  349. void NavigationAgent2D::update_navigation() {
  350. if (agent_parent == nullptr) {
  351. return;
  352. }
  353. if (!agent_parent->is_inside_tree()) {
  354. return;
  355. }
  356. if (update_frame_id == Engine::get_singleton()->get_physics_frames()) {
  357. return;
  358. }
  359. update_frame_id = Engine::get_singleton()->get_physics_frames();
  360. Vector2 o = agent_parent->get_global_transform().get_origin();
  361. bool reload_path = false;
  362. if (Navigation2DServer::get_singleton()->agent_is_map_changed(agent)) {
  363. reload_path = true;
  364. } else if (navigation_path.size() == 0) {
  365. reload_path = true;
  366. } else {
  367. // Check if too far from the navigation path
  368. if (nav_path_index > 0) {
  369. Vector2 segment[2];
  370. segment[0] = navigation_path[nav_path_index - 1];
  371. segment[1] = navigation_path[nav_path_index];
  372. Vector2 p = Geometry::get_closest_point_to_segment_2d(o, segment);
  373. if (o.distance_to(p) >= path_max_distance) {
  374. // To faraway, reload path
  375. reload_path = true;
  376. }
  377. }
  378. }
  379. if (reload_path) {
  380. if (map_override.is_valid()) {
  381. navigation_path = Navigation2DServer::get_singleton()->map_get_path(map_override, o, target_location, true, navigation_layers);
  382. } else if (navigation != nullptr) {
  383. navigation_path = Navigation2DServer::get_singleton()->map_get_path(navigation->get_rid(), o, target_location, true, navigation_layers);
  384. } else {
  385. navigation_path = Navigation2DServer::get_singleton()->map_get_path(agent_parent->get_world_2d()->get_navigation_map(), o, target_location, true, navigation_layers);
  386. }
  387. navigation_finished = false;
  388. nav_path_index = 0;
  389. emit_signal("path_changed");
  390. }
  391. if (navigation_path.size() == 0) {
  392. return;
  393. }
  394. // Check if we can advance the navigation path
  395. if (navigation_finished == false) {
  396. // Advances to the next far away location.
  397. while (o.distance_to(navigation_path[nav_path_index]) < path_desired_distance) {
  398. nav_path_index += 1;
  399. if (nav_path_index == navigation_path.size()) {
  400. _check_distance_to_target();
  401. nav_path_index -= 1;
  402. navigation_finished = true;
  403. emit_signal("navigation_finished");
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. void NavigationAgent2D::_request_repath() {
  410. navigation_path.clear();
  411. target_reached = false;
  412. navigation_finished = false;
  413. update_frame_id = 0;
  414. }
  415. void NavigationAgent2D::_check_distance_to_target() {
  416. if (!target_reached) {
  417. if (distance_to_target() < target_desired_distance) {
  418. target_reached = true;
  419. emit_signal("target_reached");
  420. }
  421. }
  422. }