From 9c8626bfd8b945787aa258c5706a6a2e0578e089 Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Tue, 13 Jun 2023 10:35:52 +0200 Subject: [PATCH] Add NavigationPolygon cell_size property Adds NavigationPolygon cell_size property. --- doc/classes/NavigationPolygon.xml | 5 +++++ scene/resources/navigation_polygon.cpp | 14 ++++++++++++++ scene/resources/navigation_polygon.h | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/doc/classes/NavigationPolygon.xml b/doc/classes/NavigationPolygon.xml index 73725117bd52..a40f3d3d3857 100644 --- a/doc/classes/NavigationPolygon.xml +++ b/doc/classes/NavigationPolygon.xml @@ -148,4 +148,9 @@ + + + The cell size used to rasterize the navigation mesh vertices. Must match with the cell size on the navigation map. + + diff --git a/scene/resources/navigation_polygon.cpp b/scene/resources/navigation_polygon.cpp index 8ba257907581..0b60d46d4070 100644 --- a/scene/resources/navigation_polygon.cpp +++ b/scene/resources/navigation_polygon.cpp @@ -183,6 +183,7 @@ Ref NavigationPolygon::get_navigation_mesh() { for (int i(0); i < get_polygon_count(); i++) { navigation_mesh->add_polygon(get_polygon(i)); } + navigation_mesh->set_cell_size(cell_size); // Needed to not fail the cell size check on the server } return navigation_mesh; @@ -322,6 +323,15 @@ void NavigationPolygon::make_polygons_from_outlines() { emit_changed(); } +void NavigationPolygon::set_cell_size(real_t p_cell_size) { + cell_size = p_cell_size; + get_navigation_mesh()->set_cell_size(cell_size); +} + +real_t NavigationPolygon::get_cell_size() const { + return cell_size; +} + void NavigationPolygon::_bind_methods() { ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationPolygon::set_vertices); ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationPolygon::get_vertices); @@ -347,7 +357,11 @@ void NavigationPolygon::_bind_methods() { ClassDB::bind_method(D_METHOD("_set_outlines", "outlines"), &NavigationPolygon::_set_outlines); ClassDB::bind_method(D_METHOD("_get_outlines"), &NavigationPolygon::_get_outlines); + ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &NavigationPolygon::set_cell_size); + ClassDB::bind_method(D_METHOD("get_cell_size"), &NavigationPolygon::get_cell_size); + ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_polygons", "_get_polygons"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_outlines", "_get_outlines"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_size", PROPERTY_HINT_RANGE, "0.01,500.0,0.01,or_greater,suffix:px"), "set_cell_size", "get_cell_size"); } diff --git a/scene/resources/navigation_polygon.h b/scene/resources/navigation_polygon.h index 015127aaeb75..01780c187c82 100644 --- a/scene/resources/navigation_polygon.h +++ b/scene/resources/navigation_polygon.h @@ -51,6 +51,8 @@ class NavigationPolygon : public Resource { // Navigation mesh Ref navigation_mesh; + real_t cell_size = 1.0f; + protected: static void _bind_methods(); @@ -87,6 +89,9 @@ class NavigationPolygon : public Resource { Ref get_navigation_mesh(); + void set_cell_size(real_t p_cell_size); + real_t get_cell_size() const; + NavigationPolygon() {} ~NavigationPolygon() {} };