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