diff --git a/scene/2d/navigation_polygon_instance.cpp b/scene/2d/navigation_polygon_instance.cpp index f6a4e5d5c..4f831d444 100644 --- a/scene/2d/navigation_polygon_instance.cpp +++ b/scene/2d/navigation_polygon_instance.cpp @@ -56,27 +56,6 @@ void NavigationPolygonInstance::set_enabled(bool p_enabled) { Navigation2DServer::get_singleton()->region_set_enabled(region, enabled); - if (!enabled) { - Navigation2DServer::get_singleton()->region_set_map(region, RID()); -#ifdef DEBUG_ENABLED - Navigation2DServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed"); -#endif - } else { - if (navigation != nullptr) { - Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid()); - } else { - if (map_override.is_valid()) { - Navigation2DServer::get_singleton()->region_set_map(region, map_override); - } else { - Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map()); - } - } - -#ifdef DEBUG_ENABLED - Navigation2DServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed"); -#endif - } - #ifdef DEBUG_ENABLED if (Engine::get_singleton()->is_editor_hint() || Navigation2DServer::get_singleton()->get_debug_navigation_enabled()) { update(); @@ -182,77 +161,26 @@ void NavigationPolygonInstance::_notification(int p_what) { navigation = Object::cast_to(c); if (navigation) { - if (enabled) { - Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid()); - } break; } c = Object::cast_to(c->get_parent()); } - if (enabled) { - RID map = get_navigation_map(); - - Navigation2DServer::get_singleton()->region_set_map(region, map); -#ifdef DEBUG_ENABLED - Navigation2DServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed"); -#endif - - for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { - if (constrain_avoidance_obstacles[i].is_valid()) { - Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map); - Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position()); - } - } - } - - current_global_transform = get_global_transform(); - Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform); + _region_enter_navigation_map(); } break; case NOTIFICATION_TRANSFORM_CHANGED: { set_physics_process_internal(true); - - for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { - if (constrain_avoidance_obstacles[i].is_valid()) { - Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position()); - } - } } break; case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { set_physics_process_internal(false); - if (is_inside_tree()) { - Transform2D new_global_transform = get_global_transform(); - if (current_global_transform != new_global_transform) { - current_global_transform = new_global_transform; - Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform); - update(); - - //TODO reenable! - //for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { - // if (constrain_avoidance_obstacles[i].is_valid()) { - // Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position()); - // } - //} - } - } + + _region_update_transform(); } break; case NOTIFICATION_EXIT_TREE: { - Navigation2DServer::get_singleton()->region_set_map(region, RID()); + _region_exit_navigation_map(); navigation = nullptr; - -#ifdef DEBUG_ENABLED - if (enabled) { - Navigation2DServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed"); - } -#endif - - for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { - if (constrain_avoidance_obstacles[i].is_valid()) { - Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], RID()); - } - } } break; case NOTIFICATION_DRAW: { #ifdef DEBUG_ENABLED @@ -299,10 +227,10 @@ void NavigationPolygonInstance::set_navigation_map(RID p_navigation_map) { } RID NavigationPolygonInstance::get_navigation_map() const { - if (navigation) { - return navigation->get_rid(); - } else if (map_override.is_valid()) { + if (map_override.is_valid()) { return map_override; + } else if (navigation) { + return navigation->get_rid(); } else if (is_inside_tree()) { return get_world_2d()->get_navigation_map(); } @@ -465,6 +393,7 @@ NavigationPolygonInstance::NavigationPolygonInstance() { Navigation2DServer::get_singleton()->region_set_travel_cost(region, get_travel_cost()); #ifdef DEBUG_ENABLED + Navigation2DServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed"); NavigationServer::get_singleton()->connect("navigation_debug_changed", this, "_navigation_debug_changed"); #endif // DEBUG_ENABLED } @@ -482,6 +411,7 @@ NavigationPolygonInstance::~NavigationPolygonInstance() { constrain_avoidance_obstacles.clear(); #ifdef DEBUG_ENABLED + Navigation2DServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed"); NavigationServer::get_singleton()->disconnect("navigation_debug_changed", this, "_navigation_debug_changed"); #endif // DEBUG_ENABLED } @@ -601,6 +531,61 @@ bool NavigationPolygonInstance::get_avoidance_layer_value(int p_layer_number) co return get_avoidance_layers() & (1 << (p_layer_number - 1)); } +void NavigationPolygonInstance::_region_enter_navigation_map() { + if (!is_inside_tree()) { + return; + } + + if (enabled) { + RID map = get_navigation_map(); + + Navigation2DServer::get_singleton()->region_set_map(region, map); + for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { + if (constrain_avoidance_obstacles[i].is_valid()) { + Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map); + } + } + } + + current_global_transform = get_global_transform(); + Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform); + for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { + if (constrain_avoidance_obstacles[i].is_valid()) { + Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position()); + } + } + + update(); +} + +void NavigationPolygonInstance::_region_exit_navigation_map() { + Navigation2DServer::get_singleton()->region_set_map(region, RID()); + for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { + if (constrain_avoidance_obstacles[i].is_valid()) { + Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], RID()); + } + } +} + +void NavigationPolygonInstance::_region_update_transform() { + if (!is_inside_tree()) { + return; + } + + Transform2D new_global_transform = get_global_transform(); + if (current_global_transform != new_global_transform) { + current_global_transform = new_global_transform; + Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform); + for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) { + if (constrain_avoidance_obstacles[i].is_valid()) { + Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position()); + } + } + } + + update(); +} + #ifdef DEBUG_ENABLED void NavigationPolygonInstance::_update_debug_mesh() { PoolVector navigation_polygon_vertices = navigation_polygon->get_vertices(); diff --git a/scene/2d/navigation_polygon_instance.h b/scene/2d/navigation_polygon_instance.h index db704f039..15ee63d80 100644 --- a/scene/2d/navigation_polygon_instance.h +++ b/scene/2d/navigation_polygon_instance.h @@ -126,6 +126,9 @@ public: private: void _update_avoidance_constrain(); + void _region_enter_navigation_map(); + void _region_exit_navigation_map(); + void _region_update_transform(); }; #endif // NAVIGATIONPOLYGON_H diff --git a/scene/3d/navigation_mesh_instance.cpp b/scene/3d/navigation_mesh_instance.cpp index 309576c55..8a1647188 100644 --- a/scene/3d/navigation_mesh_instance.cpp +++ b/scene/3d/navigation_mesh_instance.cpp @@ -163,33 +163,13 @@ void NavigationMeshInstance::_notification(int p_what) { while (c) { navigation = Object::cast_to(c); if (navigation) { - if (enabled) { - NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid()); - } break; } c = c->get_parent_spatial(); } - if (enabled && navigation == nullptr) { - // did not find a valid navigation node parent, fallback to default navigation map on world resource - if (map_override.is_valid()) { - NavigationServer::get_singleton()->region_set_map(region, map_override); - } else { - NavigationServer::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map()); - } - } - - current_global_transform = get_global_transform(); - NavigationServer::get_singleton()->region_set_transform(region, current_global_transform); - -#ifdef DEBUG_ENABLED - if (NavigationServer::get_singleton()->get_debug_navigation_enabled()) { - _update_debug_mesh(); - } -#endif // DEBUG_ENABLED - + _region_enter_navigation_map(); } break; case NOTIFICATION_TRANSFORM_CHANGED: { set_physics_process_internal(true); @@ -197,32 +177,11 @@ void NavigationMeshInstance::_notification(int p_what) { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { set_physics_process_internal(false); - if (is_inside_tree()) { - Transform new_global_transform = get_global_transform(); - if (current_global_transform != new_global_transform) { - current_global_transform = new_global_transform; - NavigationServer::get_singleton()->region_set_transform(region, current_global_transform); -#ifdef DEBUG_ENABLED - if (debug_instance.is_valid()) { - RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform); - } -#endif // DEBUG_ENABLED - } - } + + _region_update_transform(); } break; case NOTIFICATION_EXIT_TREE: { - NavigationServer::get_singleton()->region_set_map(region, RID()); - - navigation = nullptr; - -#ifdef DEBUG_ENABLED - if (debug_instance.is_valid()) { - RS::get_singleton()->instance_set_visible(debug_instance, false); - } - if (debug_edge_connections_instance.is_valid()) { - RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); - } -#endif // DEBUG_ENABLED + _region_exit_navigation_map(); } break; } @@ -259,6 +218,8 @@ void NavigationMeshInstance::set_navigation_map(RID p_navigation_map) { RID NavigationMeshInstance::get_navigation_map() const { if (map_override.is_valid()) { return map_override; + } else if (navigation) { + return navigation->get_rid(); } else if (is_inside_tree()) { return get_world_3d()->get_navigation_map(); } @@ -449,6 +410,61 @@ NavigationMeshInstance::~NavigationMeshInstance() { #endif // DEBUG_ENABLED } +void NavigationMeshInstance::_region_enter_navigation_map() { + if (!is_inside_tree()) { + return; + } + + if (enabled) { + RID map = get_navigation_map(); + + NavigationServer::get_singleton()->region_set_map(region, map); + } + + current_global_transform = get_global_transform(); + NavigationServer::get_singleton()->region_set_transform(region, current_global_transform); + +#ifdef DEBUG_ENABLED + if (NavigationServer::get_singleton()->get_debug_navigation_enabled()) { + _update_debug_mesh(); + } +#endif // DEBUG_ENABLED +} + +void NavigationMeshInstance::_region_exit_navigation_map() { + NavigationServer::get_singleton()->region_set_map(region, RID()); + +#ifdef DEBUG_ENABLED + if (debug_instance.is_valid()) { + RS::get_singleton()->instance_set_visible(debug_instance, false); + } + + if (debug_edge_connections_instance.is_valid()) { + RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); + } +#endif // DEBUG_ENABLED +} + +void NavigationMeshInstance::_region_update_transform() { + if (!is_inside_tree()) { + return; + } + + Transform new_global_transform = get_global_transform(); + + if (current_global_transform != new_global_transform) { + current_global_transform = new_global_transform; + + NavigationServer::get_singleton()->region_set_transform(region, current_global_transform); + +#ifdef DEBUG_ENABLED + if (debug_instance.is_valid()) { + RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform); + } +#endif // DEBUG_ENABLED + } +} + #ifdef DEBUG_ENABLED void NavigationMeshInstance::_update_debug_mesh() { if (Engine::get_singleton()->is_editor_hint()) { diff --git a/scene/3d/navigation_mesh_instance.h b/scene/3d/navigation_mesh_instance.h index 8289571fc..3e699644a 100644 --- a/scene/3d/navigation_mesh_instance.h +++ b/scene/3d/navigation_mesh_instance.h @@ -112,6 +112,11 @@ public: NavigationMeshInstance(); ~NavigationMeshInstance(); + +private: + void _region_enter_navigation_map(); + void _region_exit_navigation_map(); + void _region_update_transform(); }; #endif // NAVIGATION_MESH_INSTANCE_H