Path: blob/master/scene/2d/navigation/navigation_agent_2d.cpp
9904 views
/**************************************************************************/1/* navigation_agent_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "navigation_agent_2d.h"3132#include "core/math/geometry_2d.h"33#include "scene/2d/navigation/navigation_link_2d.h"34#include "scene/resources/world_2d.h"35#include "servers/navigation_server_2d.h"3637void NavigationAgent2D::_bind_methods() {38ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent2D::get_rid);3940ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent2D::set_avoidance_enabled);41ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent2D::get_avoidance_enabled);4243ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent2D::set_path_desired_distance);44ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent2D::get_path_desired_distance);4546ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent2D::set_target_desired_distance);47ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent2D::get_target_desired_distance);4849ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent2D::set_radius);50ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent2D::get_radius);5152ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent2D::set_neighbor_distance);53ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent2D::get_neighbor_distance);5455ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent2D::set_max_neighbors);56ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent2D::get_max_neighbors);5758ClassDB::bind_method(D_METHOD("set_time_horizon_agents", "time_horizon"), &NavigationAgent2D::set_time_horizon_agents);59ClassDB::bind_method(D_METHOD("get_time_horizon_agents"), &NavigationAgent2D::get_time_horizon_agents);6061ClassDB::bind_method(D_METHOD("set_time_horizon_obstacles", "time_horizon"), &NavigationAgent2D::set_time_horizon_obstacles);62ClassDB::bind_method(D_METHOD("get_time_horizon_obstacles"), &NavigationAgent2D::get_time_horizon_obstacles);6364ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent2D::set_max_speed);65ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent2D::get_max_speed);6667ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent2D::set_path_max_distance);68ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent2D::get_path_max_distance);6970ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent2D::set_navigation_layers);71ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent2D::get_navigation_layers);7273ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent2D::set_navigation_layer_value);74ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent2D::get_navigation_layer_value);7576ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent2D::set_pathfinding_algorithm);77ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent2D::get_pathfinding_algorithm);7879ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent2D::set_path_postprocessing);80ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent2D::get_path_postprocessing);8182ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent2D::set_path_metadata_flags);83ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent2D::get_path_metadata_flags);8485ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent2D::set_navigation_map);86ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent2D::get_navigation_map);8788ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent2D::set_target_position);89ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent2D::get_target_position);9091ClassDB::bind_method(D_METHOD("set_simplify_path", "enabled"), &NavigationAgent2D::set_simplify_path);92ClassDB::bind_method(D_METHOD("get_simplify_path"), &NavigationAgent2D::get_simplify_path);9394ClassDB::bind_method(D_METHOD("set_simplify_epsilon", "epsilon"), &NavigationAgent2D::set_simplify_epsilon);95ClassDB::bind_method(D_METHOD("get_simplify_epsilon"), &NavigationAgent2D::get_simplify_epsilon);9697ClassDB::bind_method(D_METHOD("set_path_return_max_length", "length"), &NavigationAgent2D::set_path_return_max_length);98ClassDB::bind_method(D_METHOD("get_path_return_max_length"), &NavigationAgent2D::get_path_return_max_length);99100ClassDB::bind_method(D_METHOD("set_path_return_max_radius", "radius"), &NavigationAgent2D::set_path_return_max_radius);101ClassDB::bind_method(D_METHOD("get_path_return_max_radius"), &NavigationAgent2D::get_path_return_max_radius);102103ClassDB::bind_method(D_METHOD("set_path_search_max_polygons", "max_polygons"), &NavigationAgent2D::set_path_search_max_polygons);104ClassDB::bind_method(D_METHOD("get_path_search_max_polygons"), &NavigationAgent2D::get_path_search_max_polygons);105106ClassDB::bind_method(D_METHOD("set_path_search_max_distance", "distance"), &NavigationAgent2D::set_path_search_max_distance);107ClassDB::bind_method(D_METHOD("get_path_search_max_distance"), &NavigationAgent2D::get_path_search_max_distance);108109ClassDB::bind_method(D_METHOD("get_path_length"), &NavigationAgent2D::get_path_length);110111ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent2D::get_next_path_position);112113ClassDB::bind_method(D_METHOD("set_velocity_forced", "velocity"), &NavigationAgent2D::set_velocity_forced);114115ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity);116ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationAgent2D::get_velocity);117118ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target);119120ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent2D::get_current_navigation_result);121122ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent2D::get_current_navigation_path);123ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent2D::get_current_navigation_path_index);124ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent2D::is_target_reached);125ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent2D::is_target_reachable);126ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent2D::is_navigation_finished);127ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent2D::get_final_position);128129ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done);130131ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationAgent2D::set_avoidance_layers);132ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationAgent2D::get_avoidance_layers);133ClassDB::bind_method(D_METHOD("set_avoidance_mask", "mask"), &NavigationAgent2D::set_avoidance_mask);134ClassDB::bind_method(D_METHOD("get_avoidance_mask"), &NavigationAgent2D::get_avoidance_mask);135ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationAgent2D::set_avoidance_layer_value);136ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationAgent2D::get_avoidance_layer_value);137ClassDB::bind_method(D_METHOD("set_avoidance_mask_value", "mask_number", "value"), &NavigationAgent2D::set_avoidance_mask_value);138ClassDB::bind_method(D_METHOD("get_avoidance_mask_value", "mask_number"), &NavigationAgent2D::get_avoidance_mask_value);139ClassDB::bind_method(D_METHOD("set_avoidance_priority", "priority"), &NavigationAgent2D::set_avoidance_priority);140ClassDB::bind_method(D_METHOD("get_avoidance_priority"), &NavigationAgent2D::get_avoidance_priority);141142ADD_GROUP("Pathfinding", "");143ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");144ADD_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");145ADD_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");146ADD_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");147ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");148ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");149ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered,None"), "set_path_postprocessing", "get_path_postprocessing");150ADD_PROPERTY(PropertyInfo(Variant::INT, "path_metadata_flags", PROPERTY_HINT_FLAGS, "Include Types,Include RIDs,Include Owners"), "set_path_metadata_flags", "get_path_metadata_flags");151ADD_PROPERTY(PropertyInfo(Variant::BOOL, "simplify_path"), "set_simplify_path", "get_simplify_path");152ADD_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");153ADD_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");154ADD_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");155ADD_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");156ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_search_max_distance"), "set_path_search_max_distance", "get_path_search_max_distance");157158ADD_GROUP("Avoidance", "");159ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");160ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");161ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,500,0.01,or_greater,suffix:px"), "set_radius", "get_radius");162ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_distance", PROPERTY_HINT_RANGE, "0.1,100000,0.01,or_greater,suffix:px"), "set_neighbor_distance", "get_neighbor_distance");163ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");164ADD_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");165ADD_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");166ADD_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");167ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");168ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_mask", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_mask", "get_avoidance_mask");169ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "avoidance_priority", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_avoidance_priority", "get_avoidance_priority");170171ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent2D::set_debug_enabled);172ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent2D::get_debug_enabled);173ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent2D::set_debug_use_custom);174ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent2D::get_debug_use_custom);175ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent2D::set_debug_path_custom_color);176ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent2D::get_debug_path_custom_color);177ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent2D::set_debug_path_custom_point_size);178ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent2D::get_debug_path_custom_point_size);179ClassDB::bind_method(D_METHOD("set_debug_path_custom_line_width", "line_width"), &NavigationAgent2D::set_debug_path_custom_line_width);180ClassDB::bind_method(D_METHOD("get_debug_path_custom_line_width"), &NavigationAgent2D::get_debug_path_custom_line_width);181182ADD_GROUP("Debug", "debug_");183ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");184ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");185ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");186ADD_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");187ADD_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");188189ADD_SIGNAL(MethodInfo("path_changed"));190ADD_SIGNAL(MethodInfo("target_reached"));191ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));192ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));193ADD_SIGNAL(MethodInfo("navigation_finished"));194ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR2, "safe_velocity")));195}196197#ifndef DISABLE_DEPRECATED198// Compatibility with Godot 4.0 beta 10 or below.199// Functions in block below all renamed or replaced in 4.0 beta 1X avoidance rework.200bool NavigationAgent2D::_set(const StringName &p_name, const Variant &p_value) {201if (p_name == "time_horizon") {202set_time_horizon_agents(p_value);203return true;204}205if (p_name == "target_location") {206set_target_position(p_value);207return true;208}209return false;210}211212bool NavigationAgent2D::_get(const StringName &p_name, Variant &r_ret) const {213if (p_name == "time_horizon") {214r_ret = get_time_horizon_agents();215return true;216}217if (p_name == "target_location") {218r_ret = get_target_position();219return true;220}221return false;222}223#endif // DISABLE_DEPRECATED224225void NavigationAgent2D::_notification(int p_what) {226switch (p_what) {227case NOTIFICATION_POST_ENTER_TREE: {228// need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.229// cannot use READY as ready does not get called if Node is re-added to SceneTree230set_agent_parent(get_parent());231set_physics_process_internal(true);232233if (agent_parent && avoidance_enabled) {234NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());235}236237#ifdef DEBUG_ENABLED238if (NavigationServer2D::get_singleton()->get_debug_enabled()) {239debug_path_dirty = true;240}241#endif // DEBUG_ENABLED242243} break;244245case NOTIFICATION_PARENTED: {246if (is_inside_tree() && (get_parent() != agent_parent)) {247// only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around248// PARENTED notification fires also when Node is added in scripts to a parent249// this would spam transforms fails and world fails while Node is outside SceneTree250// when node gets reparented when joining the tree POST_ENTER_TREE takes care of this251set_agent_parent(get_parent());252set_physics_process_internal(true);253}254} break;255256case NOTIFICATION_UNPARENTED: {257// if agent has no parent no point in processing it until reparented258set_agent_parent(nullptr);259set_physics_process_internal(false);260} break;261262case NOTIFICATION_EXIT_TREE: {263set_agent_parent(nullptr);264set_physics_process_internal(false);265266#ifdef DEBUG_ENABLED267if (debug_path_instance.is_valid()) {268RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, false);269}270#endif // DEBUG_ENABLED271} break;272273case NOTIFICATION_SUSPENDED:274case NOTIFICATION_PAUSED: {275if (agent_parent) {276NavigationServer2D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());277}278} break;279280case NOTIFICATION_UNSUSPENDED: {281if (get_tree()->is_paused()) {282break;283}284[[fallthrough]];285}286287case NOTIFICATION_UNPAUSED: {288if (agent_parent) {289NavigationServer2D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());290}291} break;292293case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {294if (agent_parent && avoidance_enabled) {295NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());296}297if (agent_parent && target_position_submitted) {298if (velocity_submitted) {299velocity_submitted = false;300if (avoidance_enabled) {301NavigationServer2D::get_singleton()->agent_set_velocity(agent, velocity);302}303}304if (velocity_forced_submitted) {305velocity_forced_submitted = false;306if (avoidance_enabled) {307NavigationServer2D::get_singleton()->agent_set_velocity_forced(agent, velocity_forced);308}309}310}311#ifdef DEBUG_ENABLED312if (debug_path_dirty) {313_update_debug_path();314}315#endif // DEBUG_ENABLED316} break;317}318}319320NavigationAgent2D::NavigationAgent2D() {321agent = NavigationServer2D::get_singleton()->agent_create();322323NavigationServer2D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);324NavigationServer2D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);325NavigationServer2D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);326NavigationServer2D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);327NavigationServer2D::get_singleton()->agent_set_radius(agent, radius);328NavigationServer2D::get_singleton()->agent_set_max_speed(agent, max_speed);329NavigationServer2D::get_singleton()->agent_set_avoidance_layers(agent, avoidance_layers);330NavigationServer2D::get_singleton()->agent_set_avoidance_mask(agent, avoidance_mask);331NavigationServer2D::get_singleton()->agent_set_avoidance_priority(agent, avoidance_priority);332NavigationServer2D::get_singleton()->agent_set_avoidance_enabled(agent, avoidance_enabled);333if (avoidance_enabled) {334NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));335}336337// Preallocate query and result objects to improve performance.338navigation_query = Ref<NavigationPathQueryParameters2D>();339navigation_query.instantiate();340341navigation_result = Ref<NavigationPathQueryResult2D>();342navigation_result.instantiate();343344#ifdef DEBUG_ENABLED345NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent2D::_navigation_debug_changed));346#endif // DEBUG_ENABLED347}348349NavigationAgent2D::~NavigationAgent2D() {350ERR_FAIL_NULL(NavigationServer2D::get_singleton());351NavigationServer2D::get_singleton()->free(agent);352agent = RID(); // Pointless353354#ifdef DEBUG_ENABLED355NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent2D::_navigation_debug_changed));356357ERR_FAIL_NULL(RenderingServer::get_singleton());358if (debug_path_instance.is_valid()) {359RenderingServer::get_singleton()->free(debug_path_instance);360}361#endif // DEBUG_ENABLED362}363364void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) {365if (avoidance_enabled == p_enabled) {366return;367}368369avoidance_enabled = p_enabled;370371if (avoidance_enabled) {372NavigationServer2D::get_singleton()->agent_set_avoidance_enabled(agent, true);373NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));374} else {375NavigationServer2D::get_singleton()->agent_set_avoidance_enabled(agent, false);376NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, Callable());377}378}379380bool NavigationAgent2D::get_avoidance_enabled() const {381return avoidance_enabled;382}383384void NavigationAgent2D::set_agent_parent(Node *p_agent_parent) {385if (agent_parent == p_agent_parent) {386return;387}388389// remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map390NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, Callable());391392if (Object::cast_to<Node2D>(p_agent_parent) != nullptr) {393// place agent on navigation map first or else the RVO agent callback creation fails silently later394agent_parent = Object::cast_to<Node2D>(p_agent_parent);395if (map_override.is_valid()) {396NavigationServer2D::get_singleton()->agent_set_map(get_rid(), map_override);397} else {398NavigationServer2D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_2d()->get_navigation_map());399}400401// create new avoidance callback if enabled402if (avoidance_enabled) {403NavigationServer2D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));404}405} else {406agent_parent = nullptr;407NavigationServer2D::get_singleton()->agent_set_map(get_rid(), RID());408}409}410411void NavigationAgent2D::set_navigation_layers(uint32_t p_navigation_layers) {412if (navigation_layers == p_navigation_layers) {413return;414}415416navigation_layers = p_navigation_layers;417418if (target_position_submitted) {419_request_repath();420}421}422423uint32_t NavigationAgent2D::get_navigation_layers() const {424return navigation_layers;425}426427void NavigationAgent2D::set_navigation_layer_value(int p_layer_number, bool p_value) {428ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");429ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");430uint32_t _navigation_layers = get_navigation_layers();431if (p_value) {432_navigation_layers |= 1 << (p_layer_number - 1);433} else {434_navigation_layers &= ~(1 << (p_layer_number - 1));435}436set_navigation_layers(_navigation_layers);437}438439bool NavigationAgent2D::get_navigation_layer_value(int p_layer_number) const {440ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");441ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");442return get_navigation_layers() & (1 << (p_layer_number - 1));443}444445void NavigationAgent2D::set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm) {446if (pathfinding_algorithm == p_pathfinding_algorithm) {447return;448}449450pathfinding_algorithm = p_pathfinding_algorithm;451452navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);453}454455void NavigationAgent2D::set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing) {456if (path_postprocessing == p_path_postprocessing) {457return;458}459460path_postprocessing = p_path_postprocessing;461462navigation_query->set_path_postprocessing(path_postprocessing);463}464465void NavigationAgent2D::set_simplify_path(bool p_enabled) {466simplify_path = p_enabled;467navigation_query->set_simplify_path(simplify_path);468}469470bool NavigationAgent2D::get_simplify_path() const {471return simplify_path;472}473474void NavigationAgent2D::set_simplify_epsilon(real_t p_epsilon) {475simplify_epsilon = MAX(0.0, p_epsilon);476navigation_query->set_simplify_epsilon(simplify_epsilon);477}478479real_t NavigationAgent2D::get_simplify_epsilon() const {480return simplify_epsilon;481}482483void NavigationAgent2D::set_path_return_max_length(float p_length) {484path_return_max_length = MAX(0.0, p_length);485navigation_query->set_path_return_max_length(path_return_max_length);486}487488float NavigationAgent2D::get_path_return_max_length() const {489return path_return_max_length;490}491492void NavigationAgent2D::set_path_return_max_radius(float p_radius) {493path_return_max_radius = MAX(0.0, p_radius);494navigation_query->set_path_return_max_radius(path_return_max_radius);495}496497float NavigationAgent2D::get_path_return_max_radius() const {498return path_return_max_radius;499}500501void NavigationAgent2D::set_path_search_max_polygons(int p_max_polygons) {502path_search_max_polygons = p_max_polygons;503navigation_query->set_path_search_max_polygons(path_search_max_polygons);504}505506int NavigationAgent2D::get_path_search_max_polygons() const {507return path_search_max_polygons;508}509510void NavigationAgent2D::set_path_search_max_distance(float p_distance) {511path_search_max_distance = MAX(0.0, p_distance);512navigation_query->set_path_search_max_distance(path_search_max_distance);513}514515float NavigationAgent2D::get_path_search_max_distance() const {516return path_search_max_distance;517}518519float NavigationAgent2D::get_path_length() const {520return navigation_result->get_path_length();521}522523void NavigationAgent2D::set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_path_metadata_flags) {524if (path_metadata_flags == p_path_metadata_flags) {525return;526}527528path_metadata_flags = p_path_metadata_flags;529}530531void NavigationAgent2D::set_navigation_map(RID p_navigation_map) {532if (map_override == p_navigation_map) {533return;534}535536map_override = p_navigation_map;537538NavigationServer2D::get_singleton()->agent_set_map(agent, map_override);539if (target_position_submitted) {540_request_repath();541}542}543544RID NavigationAgent2D::get_navigation_map() const {545if (map_override.is_valid()) {546return map_override;547} else if (agent_parent != nullptr) {548return agent_parent->get_world_2d()->get_navigation_map();549}550return RID();551}552553void NavigationAgent2D::set_path_desired_distance(real_t p_path_desired_distance) {554if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {555return;556}557558path_desired_distance = p_path_desired_distance;559}560561void NavigationAgent2D::set_target_desired_distance(real_t p_target_desired_distance) {562if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {563return;564}565566target_desired_distance = p_target_desired_distance;567}568569void NavigationAgent2D::set_radius(real_t p_radius) {570ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");571if (Math::is_equal_approx(radius, p_radius)) {572return;573}574575radius = p_radius;576577NavigationServer2D::get_singleton()->agent_set_radius(agent, radius);578}579580void NavigationAgent2D::set_neighbor_distance(real_t p_distance) {581if (Math::is_equal_approx(neighbor_distance, p_distance)) {582return;583}584585neighbor_distance = p_distance;586587NavigationServer2D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);588}589590void NavigationAgent2D::set_max_neighbors(int p_count) {591if (max_neighbors == p_count) {592return;593}594595max_neighbors = p_count;596597NavigationServer2D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);598}599600void NavigationAgent2D::set_time_horizon_agents(real_t p_time_horizon) {601ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");602if (Math::is_equal_approx(time_horizon_agents, p_time_horizon)) {603return;604}605time_horizon_agents = p_time_horizon;606NavigationServer2D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);607}608609void NavigationAgent2D::set_time_horizon_obstacles(real_t p_time_horizon) {610ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");611if (Math::is_equal_approx(time_horizon_obstacles, p_time_horizon)) {612return;613}614time_horizon_obstacles = p_time_horizon;615NavigationServer2D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);616}617618void NavigationAgent2D::set_max_speed(real_t p_max_speed) {619ERR_FAIL_COND_MSG(p_max_speed < 0.0, "Max speed must be positive.");620if (Math::is_equal_approx(max_speed, p_max_speed)) {621return;622}623max_speed = p_max_speed;624625NavigationServer2D::get_singleton()->agent_set_max_speed(agent, max_speed);626}627628void NavigationAgent2D::set_path_max_distance(real_t p_path_max_distance) {629if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {630return;631}632633path_max_distance = p_path_max_distance;634}635636real_t NavigationAgent2D::get_path_max_distance() {637return path_max_distance;638}639640void NavigationAgent2D::set_target_position(Vector2 p_position) {641// 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.642// Revisit later when the navigation server can update the path without requesting a new path.643644target_position = p_position;645target_position_submitted = true;646647_request_repath();648}649650Vector2 NavigationAgent2D::get_target_position() const {651return target_position;652}653654Vector2 NavigationAgent2D::get_next_path_position() {655_update_navigation();656657const Vector<Vector2> &navigation_path = navigation_result->get_path();658if (navigation_path.is_empty()) {659ERR_FAIL_NULL_V_MSG(agent_parent, Vector2(), "The agent has no parent.");660return agent_parent->get_global_position();661} else {662return navigation_path[navigation_path_index];663}664}665666real_t NavigationAgent2D::distance_to_target() const {667ERR_FAIL_NULL_V_MSG(agent_parent, 0.0, "The agent has no parent.");668return agent_parent->get_global_position().distance_to(target_position);669}670671bool NavigationAgent2D::is_target_reached() const {672return target_reached;673}674675bool NavigationAgent2D::is_target_reachable() {676_update_navigation();677return _is_target_reachable();678}679680bool NavigationAgent2D::_is_target_reachable() const {681return target_desired_distance >= _get_final_position().distance_to(target_position);682}683684bool NavigationAgent2D::is_navigation_finished() {685_update_navigation();686return navigation_finished;687}688689Vector2 NavigationAgent2D::get_final_position() {690_update_navigation();691return _get_final_position();692}693694Vector2 NavigationAgent2D::_get_final_position() const {695const Vector<Vector2> &navigation_path = navigation_result->get_path();696if (navigation_path.is_empty()) {697return Vector2();698}699return navigation_path[navigation_path.size() - 1];700}701702void NavigationAgent2D::set_velocity_forced(Vector2 p_velocity) {703// Intentionally not checking for equality of the parameter.704// We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.705// Revisit later when the navigation server can update avoidance without users resubmitting the velocity.706707velocity_forced = p_velocity;708velocity_forced_submitted = true;709}710711void NavigationAgent2D::set_velocity(const Vector2 p_velocity) {712velocity = p_velocity;713velocity_submitted = true;714}715716void NavigationAgent2D::_avoidance_done(Vector2 p_new_velocity) {717safe_velocity = p_new_velocity;718emit_signal(SNAME("velocity_computed"), safe_velocity);719}720721PackedStringArray NavigationAgent2D::get_configuration_warnings() const {722PackedStringArray warnings = Node::get_configuration_warnings();723724if (!Object::cast_to<Node2D>(get_parent())) {725warnings.push_back(RTR("The NavigationAgent2D can be used only under a Node2D inheriting parent node."));726}727728return warnings;729}730731void NavigationAgent2D::_update_navigation() {732if (agent_parent == nullptr) {733return;734}735if (!agent_parent->is_inside_tree()) {736return;737}738if (!target_position_submitted) {739return;740}741742Vector2 origin = agent_parent->get_global_position();743744bool reload_path = false;745746if (NavigationServer2D::get_singleton()->agent_is_map_changed(agent)) {747reload_path = true;748} else if (navigation_result->get_path().is_empty()) {749reload_path = true;750} else {751// Check if too far from the navigation path752if (navigation_path_index > 0) {753const Vector<Vector2> &navigation_path = navigation_result->get_path();754755const Vector2 segment_a = navigation_path[navigation_path_index - 1];756const Vector2 segment_b = navigation_path[navigation_path_index];757Vector2 p = Geometry2D::get_closest_point_to_segment(origin, segment_a, segment_b);758if (origin.distance_to(p) >= path_max_distance) {759// To faraway, reload path760reload_path = true;761}762}763}764765if (reload_path) {766navigation_query->set_start_position(origin);767navigation_query->set_target_position(target_position);768navigation_query->set_navigation_layers(navigation_layers);769navigation_query->set_metadata_flags(path_metadata_flags);770771if (map_override.is_valid()) {772navigation_query->set_map(map_override);773} else {774navigation_query->set_map(agent_parent->get_world_2d()->get_navigation_map());775}776777NavigationServer2D::get_singleton()->query_path(navigation_query, navigation_result);778#ifdef DEBUG_ENABLED779debug_path_dirty = true;780#endif // DEBUG_ENABLED781navigation_finished = false;782last_waypoint_reached = false;783navigation_path_index = 0;784emit_signal(SNAME("path_changed"));785}786787if (navigation_result->get_path().is_empty()) {788return;789}790791// Check if the navigation has already finished.792if (navigation_finished) {793return;794}795796// Check if we reached the target.797if (_is_within_target_distance(origin)) {798// Emit waypoint_reached in case we also moved within distance of a waypoint.799_advance_waypoints(origin);800_transition_to_target_reached();801_transition_to_navigation_finished();802} else {803// Advance waypoints if possible.804_advance_waypoints(origin);805// Keep navigation running even after reaching the last waypoint if the target is reachable.806if (last_waypoint_reached && !_is_target_reachable()) {807_transition_to_navigation_finished();808}809}810}811812void NavigationAgent2D::_advance_waypoints(const Vector2 &p_origin) {813if (last_waypoint_reached) {814return;815}816817// Advance to the farthest possible waypoint.818while (_is_within_waypoint_distance(p_origin)) {819_trigger_waypoint_reached();820821if (_is_last_waypoint()) {822last_waypoint_reached = true;823break;824}825826_move_to_next_waypoint();827}828}829830void NavigationAgent2D::_request_repath() {831navigation_result->reset();832target_reached = false;833navigation_finished = false;834last_waypoint_reached = false;835}836837bool NavigationAgent2D::_is_last_waypoint() const {838return navigation_path_index == navigation_result->get_path().size() - 1;839}840841void NavigationAgent2D::_move_to_next_waypoint() {842navigation_path_index += 1;843}844845bool NavigationAgent2D::_is_within_waypoint_distance(const Vector2 &p_origin) const {846const Vector<Vector2> &navigation_path = navigation_result->get_path();847return p_origin.distance_to(navigation_path[navigation_path_index]) < path_desired_distance;848}849850bool NavigationAgent2D::_is_within_target_distance(const Vector2 &p_origin) const {851return p_origin.distance_to(target_position) < target_desired_distance;852}853854void NavigationAgent2D::_trigger_waypoint_reached() {855const Vector<Vector2> &navigation_path = navigation_result->get_path();856const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();857const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();858const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();859860Dictionary details;861862const Vector2 waypoint = navigation_path[navigation_path_index];863details[CoreStringName(position)] = waypoint;864865int waypoint_type = -1;866if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {867const NavigationPathQueryResult2D::PathSegmentType type = NavigationPathQueryResult2D::PathSegmentType(navigation_path_types[navigation_path_index]);868869details[SNAME("type")] = type;870waypoint_type = type;871}872873if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {874details[SNAME("rid")] = navigation_path_rids[navigation_path_index];875}876877if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {878const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);879880// Get a reference to the owning object.881Object *owner = nullptr;882if (waypoint_owner_id.is_valid()) {883owner = ObjectDB::get_instance(waypoint_owner_id);884}885886details[SNAME("owner")] = owner;887888if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {889const NavigationLink2D *navlink = Object::cast_to<NavigationLink2D>(owner);890if (navlink) {891Vector2 link_global_start_position = navlink->get_global_start_position();892Vector2 link_global_end_position = navlink->get_global_end_position();893if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {894details[SNAME("link_entry_position")] = link_global_start_position;895details[SNAME("link_exit_position")] = link_global_end_position;896} else {897details[SNAME("link_entry_position")] = link_global_end_position;898details[SNAME("link_exit_position")] = link_global_start_position;899}900}901}902}903904// Emit a signal for the waypoint.905emit_signal(SNAME("waypoint_reached"), details);906907// Emit a signal if we've reached a navigation link.908if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {909emit_signal(SNAME("link_reached"), details);910}911}912913void NavigationAgent2D::_transition_to_navigation_finished() {914navigation_finished = true;915target_position_submitted = false;916917if (avoidance_enabled) {918NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());919NavigationServer2D::get_singleton()->agent_set_velocity(agent, Vector2(0.0, 0.0));920NavigationServer2D::get_singleton()->agent_set_velocity_forced(agent, Vector2(0.0, 0.0));921}922923emit_signal(SNAME("navigation_finished"));924}925926void NavigationAgent2D::_transition_to_target_reached() {927target_reached = true;928emit_signal(SNAME("target_reached"));929}930931void NavigationAgent2D::set_avoidance_layers(uint32_t p_layers) {932avoidance_layers = p_layers;933NavigationServer2D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);934}935936uint32_t NavigationAgent2D::get_avoidance_layers() const {937return avoidance_layers;938}939940void NavigationAgent2D::set_avoidance_mask(uint32_t p_mask) {941avoidance_mask = p_mask;942NavigationServer2D::get_singleton()->agent_set_avoidance_mask(get_rid(), p_mask);943}944945uint32_t NavigationAgent2D::get_avoidance_mask() const {946return avoidance_mask;947}948949void NavigationAgent2D::set_avoidance_layer_value(int p_layer_number, bool p_value) {950ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");951ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");952uint32_t avoidance_layers_new = get_avoidance_layers();953if (p_value) {954avoidance_layers_new |= 1 << (p_layer_number - 1);955} else {956avoidance_layers_new &= ~(1 << (p_layer_number - 1));957}958set_avoidance_layers(avoidance_layers_new);959}960961bool NavigationAgent2D::get_avoidance_layer_value(int p_layer_number) const {962ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");963ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");964return get_avoidance_layers() & (1 << (p_layer_number - 1));965}966967void NavigationAgent2D::set_avoidance_mask_value(int p_mask_number, bool p_value) {968ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");969ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");970uint32_t mask = get_avoidance_mask();971if (p_value) {972mask |= 1 << (p_mask_number - 1);973} else {974mask &= ~(1 << (p_mask_number - 1));975}976set_avoidance_mask(mask);977}978979bool NavigationAgent2D::get_avoidance_mask_value(int p_mask_number) const {980ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");981ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");982return get_avoidance_mask() & (1 << (p_mask_number - 1));983}984985void NavigationAgent2D::set_avoidance_priority(real_t p_priority) {986ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");987ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");988avoidance_priority = p_priority;989NavigationServer2D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);990}991992real_t NavigationAgent2D::get_avoidance_priority() const {993return avoidance_priority;994}995996////////DEBUG////////////////////////////////////////////////////////////997998void NavigationAgent2D::set_debug_enabled(bool p_enabled) {999#ifdef DEBUG_ENABLED1000if (debug_enabled == p_enabled) {1001return;1002}10031004debug_enabled = p_enabled;1005debug_path_dirty = true;1006#endif // DEBUG_ENABLED1007}10081009bool NavigationAgent2D::get_debug_enabled() const {1010return debug_enabled;1011}10121013void NavigationAgent2D::set_debug_use_custom(bool p_enabled) {1014#ifdef DEBUG_ENABLED1015if (debug_use_custom == p_enabled) {1016return;1017}10181019debug_use_custom = p_enabled;1020debug_path_dirty = true;1021#endif // DEBUG_ENABLED1022}10231024bool NavigationAgent2D::get_debug_use_custom() const {1025return debug_use_custom;1026}10271028void NavigationAgent2D::set_debug_path_custom_color(Color p_color) {1029#ifdef DEBUG_ENABLED1030if (debug_path_custom_color == p_color) {1031return;1032}10331034debug_path_custom_color = p_color;1035debug_path_dirty = true;1036#endif // DEBUG_ENABLED1037}10381039Color NavigationAgent2D::get_debug_path_custom_color() const {1040return debug_path_custom_color;1041}10421043void NavigationAgent2D::set_debug_path_custom_point_size(float p_point_size) {1044#ifdef DEBUG_ENABLED1045if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {1046return;1047}10481049debug_path_custom_point_size = MAX(0.0, p_point_size);1050debug_path_dirty = true;1051#endif // DEBUG_ENABLED1052}10531054float NavigationAgent2D::get_debug_path_custom_point_size() const {1055return debug_path_custom_point_size;1056}10571058void NavigationAgent2D::set_debug_path_custom_line_width(float p_line_width) {1059#ifdef DEBUG_ENABLED1060if (Math::is_equal_approx(debug_path_custom_line_width, p_line_width)) {1061return;1062}10631064debug_path_custom_line_width = p_line_width;1065debug_path_dirty = true;1066#endif // DEBUG_ENABLED1067}10681069float NavigationAgent2D::get_debug_path_custom_line_width() const {1070return debug_path_custom_line_width;1071}10721073#ifdef DEBUG_ENABLED1074void NavigationAgent2D::_navigation_debug_changed() {1075debug_path_dirty = true;1076}10771078void NavigationAgent2D::_update_debug_path() {1079if (!debug_path_dirty) {1080return;1081}1082debug_path_dirty = false;10831084if (!debug_path_instance.is_valid()) {1085debug_path_instance = RenderingServer::get_singleton()->canvas_item_create();1086}10871088RenderingServer::get_singleton()->canvas_item_clear(debug_path_instance);10891090if (!(debug_enabled && NavigationServer2D::get_singleton()->get_debug_navigation_enable_agent_paths())) {1091return;1092}10931094if (!(agent_parent && agent_parent->is_inside_tree())) {1095return;1096}10971098RenderingServer::get_singleton()->canvas_item_set_parent(debug_path_instance, agent_parent->get_canvas());1099RenderingServer::get_singleton()->canvas_item_set_z_index(debug_path_instance, RS::CANVAS_ITEM_Z_MAX - 1);1100RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());11011102const Vector<Vector2> &navigation_path = navigation_result->get_path();11031104if (navigation_path.size() <= 1) {1105return;1106}11071108Color debug_path_color = NavigationServer2D::get_singleton()->get_debug_navigation_agent_path_color();1109if (debug_use_custom) {1110debug_path_color = debug_path_custom_color;1111}11121113Vector<Color> debug_path_colors;1114debug_path_colors.resize(navigation_path.size());1115debug_path_colors.fill(debug_path_color);11161117RenderingServer::get_singleton()->canvas_item_add_polyline(debug_path_instance, navigation_path, debug_path_colors, debug_path_custom_line_width, false);11181119if (debug_path_custom_point_size <= 0.0) {1120return;1121}11221123float point_size = NavigationServer2D::get_singleton()->get_debug_navigation_agent_path_point_size();1124float half_point_size = point_size * 0.5;11251126if (debug_use_custom) {1127point_size = debug_path_custom_point_size;1128half_point_size = debug_path_custom_point_size * 0.5;1129}11301131for (int i = 0; i < navigation_path.size(); i++) {1132const Vector2 &vert = navigation_path[i];1133Rect2 path_point_rect = Rect2(vert.x - half_point_size, vert.y - half_point_size, point_size, point_size);1134RenderingServer::get_singleton()->canvas_item_add_rect(debug_path_instance, path_point_rect, debug_path_color);1135}1136}1137#endif // DEBUG_ENABLED113811391140