From 11d47d8fdc22005899fa904264330dbcd9857685 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 5 Sep 2023 11:54:49 +0200 Subject: [PATCH] Simplifications to NavigationAgent and NavigationAgent2D. --- scene/2d/navigation_agent_2d.cpp | 25 ++++++++++--------------- scene/3d/navigation_agent.cpp | 16 ++++++++-------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index 1f2583057..01fba450a 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -611,7 +611,7 @@ Vector2 NavigationAgent2D::get_target_position() const { Vector2 NavigationAgent2D::get_next_position() { update_navigation(); - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); if (navigation_path.size() == 0) { ERR_FAIL_COND_V(agent_parent == nullptr, Vector2()); return agent_parent->get_global_transform().get_origin(); @@ -649,7 +649,7 @@ bool NavigationAgent2D::is_navigation_finished() { Vector2 NavigationAgent2D::get_final_position() { update_navigation(); - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); if (navigation_path.size() == 0) { return Vector2(); } @@ -687,6 +687,7 @@ void NavigationAgent2D::update_navigation() { if (agent_parent == nullptr) { return; } + if (!agent_parent->is_inside_tree()) { return; } @@ -708,7 +709,7 @@ void NavigationAgent2D::update_navigation() { } else { // Check if too far from the navigation path if (nav_path_index > 0) { - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); Vector2 segment[2]; segment[0] = navigation_path[nav_path_index - 1]; @@ -727,13 +728,7 @@ void NavigationAgent2D::update_navigation() { navigation_query->set_navigation_layers(navigation_layers); navigation_query->set_metadata_flags(path_metadata_flags); - if (map_override.is_valid()) { - navigation_query->set_map(map_override); - } else if (navigation != nullptr) { - navigation_query->set_map(navigation->get_rid()); - } else { - navigation_query->set_map(agent_parent->get_world_2d()->get_navigation_map()); - } + navigation_query->set_map(get_navigation_map()); Navigation2DServer::get_singleton()->query_path(navigation_query, navigation_result); @@ -753,10 +748,10 @@ void NavigationAgent2D::update_navigation() { // Check if we can advance the navigation path if (navigation_finished == false) { // Advances to the next far away position. - const Vector &navigation_path = navigation_result->get_path(); - const Vector &navigation_path_types = navigation_result->get_path_types(); - const Array &navigation_path_rids = navigation_result->get_path_rids(); - const Vector &navigation_path_owners = navigation_result->get_path_owner_ids(); + const Vector navigation_path = navigation_result->get_path(); + const Vector navigation_path_types = navigation_result->get_path_types(); + const Array navigation_path_rids = navigation_result->get_path_rids(); + const Vector navigation_path_owners = navigation_result->get_path_owner_ids(); while (origin.distance_to(navigation_path[nav_path_index]) < path_desired_distance) { Dictionary details; @@ -924,7 +919,7 @@ void NavigationAgent2D::_update_debug_path() { RenderingServer::get_singleton()->canvas_item_set_parent(debug_path_instance, agent_parent->get_canvas()); RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, agent_parent->is_visible_in_tree()); - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); if (navigation_path.size() <= 1) { return; diff --git a/scene/3d/navigation_agent.cpp b/scene/3d/navigation_agent.cpp index 1c2758abf..f34edd51c 100644 --- a/scene/3d/navigation_agent.cpp +++ b/scene/3d/navigation_agent.cpp @@ -590,7 +590,7 @@ Vector3 NavigationAgent::get_target_position() const { Vector3 NavigationAgent::get_next_position() { update_navigation(); - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); if (navigation_path.size() == 0) { ERR_FAIL_COND_V_MSG(agent_parent == nullptr, Vector3(), "The agent has no parent."); return agent_parent->get_global_transform().origin; @@ -628,7 +628,7 @@ bool NavigationAgent::is_navigation_finished() { Vector3 NavigationAgent::get_final_position() { update_navigation(); - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); if (navigation_path.size() == 0) { return Vector3(); @@ -699,7 +699,7 @@ void NavigationAgent::update_navigation() { } else { // Check if too far from the navigation path if (nav_path_index > 0) { - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); Vector3 segment[2]; segment[0] = navigation_path[nav_path_index - 1]; @@ -740,10 +740,10 @@ void NavigationAgent::update_navigation() { // Check if we can advance the navigation path if (navigation_finished == false) { // Advances to the next far away position. - const Vector &navigation_path = navigation_result->get_path(); - const Vector &navigation_path_types = navigation_result->get_path_types(); - const Array &navigation_path_rids = navigation_result->get_path_rids(); - const Vector &navigation_path_owners = navigation_result->get_path_owner_ids(); + const Vector navigation_path = navigation_result->get_path(); + const Vector navigation_path_types = navigation_result->get_path_types(); + const Array navigation_path_rids = navigation_result->get_path_rids(); + const Vector navigation_path_owners = navigation_result->get_path_owner_ids(); while (origin.distance_to(navigation_path[nav_path_index] - Vector3(0, path_height_offset, 0)) < path_desired_distance) { Dictionary details; @@ -969,7 +969,7 @@ void NavigationAgent::_update_debug_path() { return; } - const Vector &navigation_path = navigation_result->get_path(); + const Vector navigation_path = navigation_result->get_path(); if (navigation_path.size() <= 1) { return;