diff options
Diffstat (limited to 'scene/2d/navigation_agent_2d.cpp')
-rw-r--r-- | scene/2d/navigation_agent_2d.cpp | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp index 32da46e8a8..e5cdade4a4 100644 --- a/scene/2d/navigation_agent_2d.cpp +++ b/scene/2d/navigation_agent_2d.cpp @@ -31,11 +31,11 @@ #include "navigation_agent_2d.h" #include "core/engine.h" +#include "core/math/geometry_2d.h" #include "scene/2d/navigation_2d.h" #include "servers/navigation_server_2d.h" void NavigationAgent2D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent2D::set_target_desired_distance); ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent2D::get_target_desired_distance); @@ -91,7 +91,6 @@ void NavigationAgent2D::_bind_methods() { void NavigationAgent2D::_notification(int p_what) { switch (p_what) { case NOTIFICATION_READY: { - agent_parent = Object::cast_to<Node2D>(get_parent()); NavigationServer2D::get_singleton()->agent_set_callback(agent, this, "_avoidance_done"); @@ -102,10 +101,11 @@ void NavigationAgent2D::_notification(int p_what) { Node *p = get_parent(); while (p != nullptr) { nav = Object::cast_to<Navigation2D>(p); - if (nav != nullptr) + if (nav != nullptr) { p = nullptr; - else + } else { p = p->get_parent(); + } } set_navigation(nav); @@ -120,7 +120,6 @@ void NavigationAgent2D::_notification(int p_what) { } break; case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { if (agent_parent) { - NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().get_origin()); if (!target_reached) { if (distance_to_target() < target_desired_distance) { @@ -133,15 +132,7 @@ void NavigationAgent2D::_notification(int p_what) { } } -NavigationAgent2D::NavigationAgent2D() : - agent_parent(nullptr), - navigation(nullptr), - agent(RID()), - target_desired_distance(1.0), - path_max_distance(3.0), - velocity_submitted(false), - target_reached(false), - navigation_finished(true) { +NavigationAgent2D::NavigationAgent2D() { agent = NavigationServer2D::get_singleton()->agent_create(); set_neighbor_dist(500.0); set_max_neighbors(10); @@ -156,8 +147,9 @@ NavigationAgent2D::~NavigationAgent2D() { } void NavigationAgent2D::set_navigation(Navigation2D *p_nav) { - if (navigation == p_nav) + if (navigation == p_nav) { return; // Pointless + } navigation = p_nav; NavigationServer2D::get_singleton()->agent_set_map(agent, navigation == nullptr ? RID() : navigation->get_rid()); @@ -287,10 +279,15 @@ String NavigationAgent2D::get_configuration_warning() const { } void NavigationAgent2D::update_navigation() { - - if (agent_parent == nullptr) return; - if (navigation == nullptr) return; - if (update_frame_id == Engine::get_singleton()->get_physics_frames()) return; + if (agent_parent == nullptr) { + return; + } + if (navigation == nullptr) { + return; + } + if (update_frame_id == Engine::get_singleton()->get_physics_frames()) { + return; + } update_frame_id = Engine::get_singleton()->get_physics_frames(); @@ -308,7 +305,7 @@ void NavigationAgent2D::update_navigation() { Vector2 segment[2]; segment[0] = navigation_path[nav_path_index - 1]; segment[1] = navigation_path[nav_path_index]; - Vector2 p = Geometry::get_closest_point_to_segment_2d(o, segment); + Vector2 p = Geometry2D::get_closest_point_to_segment(o, segment); if (o.distance_to(p) >= path_max_distance) { // To faraway, reload path reload_path = true; @@ -323,8 +320,9 @@ void NavigationAgent2D::update_navigation() { emit_signal("path_changed"); } - if (navigation_path.size() == 0) + if (navigation_path.size() == 0) { return; + } // Check if we can advance the navigation path if (navigation_finished == false) { |