Split NavigationPolygon and NavigationPolygonInstance into 2 files. Also renamedNavigationPolygonInstance to NavigationRegion2D.

This commit is contained in:
Relintai 2023-06-03 16:54:48 +02:00
parent 79538b4392
commit 2114b2239b
18 changed files with 474 additions and 387 deletions

View File

@ -4,7 +4,7 @@
2D navigation and pathfinding node.
</brief_description>
<description>
Navigation2D provides navigation and pathfinding within a 2D area, specified as a collection of [NavigationPolygon] resources. By default, these are automatically collected from child [NavigationPolygonInstance] nodes.
Navigation2D provides navigation and pathfinding within a 2D area, specified as a collection of [NavigationPolygon] resources. By default, these are automatically collected from child [NavigationRegion2D] nodes.
</description>
<tutorials>
<link title="2D Navigation Demo">https://godotengine.org/asset-library/asset/117</link>
@ -21,7 +21,7 @@
<return type="RID" />
<argument index="0" name="to_point" type="Vector2" />
<description>
Returns the owner of the [NavigationPolygon] which contains the navigation point closest to the point given. This is usually a [NavigationPolygonInstance].
Returns the owner of the [NavigationPolygon] which contains the navigation point closest to the point given. This is usually a [NavigationRegion2D].
</description>
</method>
<method name="get_rid" qualifiers="const">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="NavigationPolygonInstance" inherits="Node2D" version="3.11">
<class name="NavigationRegion2D" inherits="Node2D" version="3.11">
<brief_description>
A region of the 2D navigation map.
</brief_description>
@ -18,13 +18,13 @@
<method name="get_region_rid" qualifiers="const">
<return type="RID" />
<description>
Returns the [RID] of this region on the [Navigation2DServer]. Combined with [method Navigation2DServer.map_get_closest_point_owner] can be used to identify the [NavigationPolygonInstance] closest to a point on the merged navigation map.
Returns the [RID] of this region on the [Navigation2DServer]. Combined with [method Navigation2DServer.map_get_closest_point_owner] can be used to identify the [NavigationRegion2D] closest to a point on the merged navigation map.
</description>
</method>
</methods>
<members>
<member name="enabled" type="bool" setter="set_enabled" getter="is_enabled" default="true">
Determines if the [NavigationPolygonInstance] is enabled or disabled.
Determines if the [NavigationRegion2D] is enabled or disabled.
</member>
<member name="enter_cost" type="float" setter="set_enter_cost" getter="get_enter_cost" default="0.0">
When pathfinding enters this regions navmesh from another regions navmesh the [code]enter_cost[/code] value is added to the path distance for determining the shortest path.

View File

@ -11,7 +11,7 @@
var outline = PoolVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
polygon.add_outline(outline)
polygon.make_polygons_from_outlines()
$NavigationPolygonInstance.navpoly = polygon
$NavigationRegion2D.navpoly = polygon
[/codeblock]
Using [method add_polygon] and indices of the vertices array.
[codeblock]
@ -20,7 +20,7 @@
polygon.set_vertices(vertices)
var indices = PoolIntArray([0, 1, 2, 3])
polygon.add_polygon(indices)
$NavigationPolygonInstance.navpoly = polygon
$NavigationRegion2D.navpoly = polygon
[/codeblock]
</description>
<tutorials>

View File

@ -35748,7 +35748,7 @@ msgid ""
"Navigation2DServer.map_get_path] instead.\n"
"Navigation2D provides navigation and pathfinding within a 2D area, specified "
"as a collection of [NavigationPolygon] resources. By default, these are "
"automatically collected from child [NavigationPolygonInstance] nodes."
"automatically collected from child [NavigationRegion2D] nodes."
msgstr ""
#: doc/classes/Navigation2D.xml doc/classes/Navigation2DServer.xml
@ -35760,7 +35760,7 @@ msgstr ""
msgid ""
"Returns the owner of the [NavigationPolygon] which contains the navigation "
"point closest to the point given. This is usually a "
"[NavigationPolygonInstance]."
"[NavigationRegion2D]."
msgstr ""
#: doc/classes/Navigation2D.xml
@ -36781,7 +36781,7 @@ msgid "Determines if the [NavigationMeshInstance] is enabled or disabled."
msgstr ""
#: doc/classes/NavigationMeshInstance.xml
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"When pathfinding enters this region's navmesh from another regions navmesh "
"the [code]enter_cost[/code] value is added to the path distance for "
@ -36801,7 +36801,7 @@ msgid "The [NavigationMesh] resource to use."
msgstr ""
#: doc/classes/NavigationMeshInstance.xml
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"When pathfinding moves inside this region's navmesh the traveled distances "
"are multiplied with [code]travel_cost[/code] for determining the shortest "
@ -36905,7 +36905,7 @@ msgid ""
"50), Vector2(50, 0)])\n"
"polygon.add_outline(outline)\n"
"polygon.make_polygons_from_outlines()\n"
"$NavigationPolygonInstance.navpoly = polygon\n"
"$NavigationRegion2D.navpoly = polygon\n"
"[/codeblock]\n"
"Using [method add_polygon] and indices of the vertices array.\n"
"[codeblock]\n"
@ -36915,7 +36915,7 @@ msgid ""
"polygon.set_vertices(vertices)\n"
"var indices = PoolIntArray([0, 1, 2, 3])\n"
"polygon.add_polygon(indices)\n"
"$NavigationPolygonInstance.navpoly = polygon\n"
"$NavigationRegion2D.navpoly = polygon\n"
"[/codeblock]"
msgstr ""
@ -36992,11 +36992,11 @@ msgid ""
"[method make_polygons_from_outlines] for the polygons to update."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid "A region of the 2D navigation map."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"A region of the navigation map. It tells the [Navigation2DServer] what can "
"be navigated and what cannot, based on its [NavigationPolygon] resource.\n"
@ -37016,19 +37016,19 @@ msgid ""
"controlled with the [member travel_cost] multiplier."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"Returns the [RID] of this region on the [Navigation2DServer]. Combined with "
"[method Navigation2DServer.map_get_closest_point_owner] can be used to "
"identify the [NavigationPolygonInstance] closest to a point on the merged "
"identify the [NavigationRegion2D] closest to a point on the merged "
"navigation map."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
msgid "Determines if the [NavigationPolygonInstance] is enabled or disabled."
#: doc/classes/NavigationRegion2D.xml
msgid "Determines if the [NavigationRegion2D] is enabled or disabled."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"A bitfield determining all navigation map layers the [NavigationPolygon] "
"belongs to. On path requests with [method Navigation2DServer.map_get_path] "
@ -37036,7 +37036,7 @@ msgid ""
"will only proximity merge different navmeshes with matching layers."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid "The [NavigationPolygon] resource to use."
msgstr ""

View File

@ -35825,7 +35825,7 @@ msgid ""
"Navigation2DServer.map_get_path] instead.\n"
"Navigation2D provides navigation and pathfinding within a 2D area, specified "
"as a collection of [NavigationPolygon] resources. By default, these are "
"automatically collected from child [NavigationPolygonInstance] nodes."
"automatically collected from child [NavigationRegion2D] nodes."
msgstr ""
#: doc/classes/Navigation2D.xml doc/classes/Navigation2DServer.xml
@ -35837,7 +35837,7 @@ msgstr ""
msgid ""
"Returns the owner of the [NavigationPolygon] which contains the navigation "
"point closest to the point given. This is usually a "
"[NavigationPolygonInstance]."
"[NavigationRegion2D]."
msgstr ""
#: doc/classes/Navigation2D.xml
@ -36864,7 +36864,7 @@ msgid "Determines if the [NavigationMeshInstance] is enabled or disabled."
msgstr ""
#: doc/classes/NavigationMeshInstance.xml
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"When pathfinding enters this region's navmesh from another regions navmesh "
"the [code]enter_cost[/code] value is added to the path distance for "
@ -36884,7 +36884,7 @@ msgid "The [NavigationMesh] resource to use."
msgstr ""
#: doc/classes/NavigationMeshInstance.xml
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"When pathfinding moves inside this region's navmesh the traveled distances "
"are multiplied with [code]travel_cost[/code] for determining the shortest "
@ -36988,7 +36988,7 @@ msgid ""
"50), Vector2(50, 0)])\n"
"polygon.add_outline(outline)\n"
"polygon.make_polygons_from_outlines()\n"
"$NavigationPolygonInstance.navpoly = polygon\n"
"$NavigationRegion2D.navpoly = polygon\n"
"[/codeblock]\n"
"Using [method add_polygon] and indices of the vertices array.\n"
"[codeblock]\n"
@ -36998,7 +36998,7 @@ msgid ""
"polygon.set_vertices(vertices)\n"
"var indices = PoolIntArray([0, 1, 2, 3])\n"
"polygon.add_polygon(indices)\n"
"$NavigationPolygonInstance.navpoly = polygon\n"
"$NavigationRegion2D.navpoly = polygon\n"
"[/codeblock]"
msgstr ""
@ -37075,11 +37075,11 @@ msgid ""
"[method make_polygons_from_outlines] for the polygons to update."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid "A region of the 2D navigation map."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"A region of the navigation map. It tells the [Navigation2DServer] what can "
"be navigated and what cannot, based on its [NavigationPolygon] resource.\n"
@ -37099,19 +37099,19 @@ msgid ""
"controlled with the [member travel_cost] multiplier."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"Returns the [RID] of this region on the [Navigation2DServer]. Combined with "
"[method Navigation2DServer.map_get_closest_point_owner] can be used to "
"identify the [NavigationPolygonInstance] closest to a point on the merged "
"identify the [NavigationRegion2D] closest to a point on the merged "
"navigation map."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
msgid "Determines if the [NavigationPolygonInstance] is enabled or disabled."
#: doc/classes/NavigationRegion2D.xml
msgid "Determines if the [NavigationRegion2D] is enabled or disabled."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid ""
"A bitfield determining all navigation map layers the [NavigationPolygon] "
"belongs to. On path requests with [method Navigation2DServer.map_get_path] "
@ -37119,7 +37119,7 @@ msgid ""
"will only proximity merge different navmeshes with matching layers."
msgstr ""
#: doc/classes/NavigationPolygonInstance.xml
#: doc/classes/NavigationRegion2D.xml
msgid "The [NavigationPolygon] resource to use."
msgstr ""

View File

@ -35,7 +35,8 @@
#include "core/object/undo_redo.h"
#include "core/string/ustring.h"
#include "core/containers/vector.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/2d/navigation_region_2d.h"
#include "scene/resources/navigation_polygon.h"
#include "scene/main/node.h"
class EditorNode;
@ -55,7 +56,7 @@ Node2D *NavigationPolygonEditor::_get_node() const {
}
void NavigationPolygonEditor::_set_node(Node *p_polygon) {
node = Object::cast_to<NavigationPolygonInstance>(p_polygon);
node = Object::cast_to<NavigationRegion2D>(p_polygon);
}
int NavigationPolygonEditor::_get_polygon_count() const {
@ -129,5 +130,5 @@ NavigationPolygonEditor::NavigationPolygonEditor(EditorNode *p_editor) :
}
NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin(EditorNode *p_node) :
AbstractPolygon2DEditorPlugin(p_node, memnew(NavigationPolygonEditor(p_node)), "NavigationPolygonInstance") {
AbstractPolygon2DEditorPlugin(p_node, memnew(NavigationPolygonEditor(p_node)), "NavigationRegion2D") {
}

View File

@ -38,14 +38,14 @@
class EditorNode;
class NavigationPolygon;
class NavigationPolygonInstance;
class NavigationRegion2D;
class Node2D;
class Node;
class NavigationPolygonEditor : public AbstractPolygon2DEditor {
GDCLASS(NavigationPolygonEditor, AbstractPolygon2DEditor);
NavigationPolygonInstance *node;
NavigationRegion2D *node;
Ref<NavigationPolygon> _ensure_navpoly() const;

View File

@ -33,7 +33,7 @@
#include "core/object/object.h"
#include "core/containers/rid.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/resources/navigation_polygon.h"
#include "servers/navigation_2d_server.h"
// This server exposes the 3D `NavigationServer` features in the 2D world.

View File

@ -30,7 +30,7 @@
#include "dummy_navigation_2d_server.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/resources/navigation_polygon.h"
void DummyNavigation2DServer::region_set_navpoly(RID p_region, Ref<NavigationPolygon> p_nav_mesh) const {
}

View File

@ -35,7 +35,8 @@
#include "core/variant/array.h"
#include "core/object/resource.h"
#include "scene/2d/light_occluder_2d.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/2d/navigation_region_2d.h"
#include "scene/resources/navigation_polygon.h"
#include "scene/resources/convex_polygon_shape_2d.h"
#include "scene/resources/shape_2d.h"
#include "scene/resources/texture.h"

View File

@ -123,8 +123,8 @@ void TileSetEditor::_import_node(Node *p_node, Ref<TileSet> p_library) {
for (int j = 0; j < mi->get_child_count(); j++) {
Node *child2 = mi->get_child(j);
if (Object::cast_to<NavigationPolygonInstance>(child2)) {
nav_poly = Object::cast_to<NavigationPolygonInstance>(child2)->get_navigation_polygon();
if (Object::cast_to<NavigationRegion2D>(child2)) {
nav_poly = Object::cast_to<NavigationRegion2D>(child2)->get_navigation_polygon();
}
if (Object::cast_to<LightOccluder2D>(child2)) {

View File

@ -30,7 +30,8 @@
#include "navigation_2d.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/2d/navigation_region_2d.h"
#include "scene/resources/navigation_polygon.h"
#include "servers/navigation_2d_server.h"
void Navigation2D::_bind_methods() {

View File

@ -0,0 +1,328 @@
/*************************************************************************/
/* navigation_polygon.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "navigation_region_2d.h"
#include "core/core_string_names.h"
#include "core/config/engine.h"
#include "core/os/mutex.h"
#include "navigation_2d.h"
#include "scene/resources/navigation_mesh.h"
#include "scene/resources/world_2d.h"
#include "servers/navigation_2d_server.h"
#include "scene/resources/navigation_polygon.h"
#include "thirdparty/misc/triangulator.h"
void NavigationRegion2D::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;
if (!is_inside_tree()) {
return;
}
if (!enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, RID());
Navigation2DServer::get_singleton_mut()->disconnect("map_changed", this, "_map_changed");
} else {
if (navigation != nullptr) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {
Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
}
Navigation2DServer::get_singleton_mut()->connect("map_changed", this, "_map_changed");
}
if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()) {
update();
}
}
bool NavigationRegion2D::is_enabled() const {
return enabled;
}
void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
navigation_layers = p_navigation_layers;
Navigation2DServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
}
uint32_t NavigationRegion2D::get_navigation_layers() const {
return navigation_layers;
}
void NavigationRegion2D::set_enter_cost(real_t p_enter_cost) {
ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
enter_cost = MAX(p_enter_cost, 0.0);
Navigation2DServer::get_singleton()->region_set_enter_cost(region, p_enter_cost);
}
real_t NavigationRegion2D::get_enter_cost() const {
return enter_cost;
}
void NavigationRegion2D::set_travel_cost(real_t p_travel_cost) {
ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
travel_cost = MAX(p_travel_cost, 0.0);
Navigation2DServer::get_singleton()->region_set_enter_cost(region, travel_cost);
}
real_t NavigationRegion2D::get_travel_cost() const {
return travel_cost;
}
RID NavigationRegion2D::get_region_rid() const {
return region;
}
/////////////////////////////
#ifdef TOOLS_ENABLED
Rect2 NavigationRegion2D::_edit_get_rect() const {
return navpoly.is_valid() ? navpoly->_edit_get_rect() : Rect2();
}
bool NavigationRegion2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
return navpoly.is_valid() ? navpoly->_edit_is_selected_on_click(p_point, p_tolerance) : false;
}
#endif
void NavigationRegion2D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
Node2D *c = this;
while (c) {
navigation = Object::cast_to<Navigation2D>(c);
if (navigation) {
if (enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
}
break;
}
c = Object::cast_to<Node2D>(c->get_parent());
}
if (enabled && navigation == nullptr) {
// did not find a valid navigation node parent, fallback to default navigation map on world resource
Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
}
if (enabled) {
Navigation2DServer::get_singleton_mut()->connect("map_changed", this, "_map_changed");
}
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
Navigation2DServer::get_singleton()->region_set_transform(region, get_global_transform());
} break;
case NOTIFICATION_EXIT_TREE: {
if (navigation) {
Navigation2DServer::get_singleton()->region_set_map(region, RID());
}
navigation = nullptr;
if (enabled) {
Navigation2DServer::get_singleton_mut()->disconnect("map_changed", this, "_map_changed");
}
} break;
case NOTIFICATION_DRAW: {
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()) && navpoly.is_valid()) {
PoolVector<Vector2> verts = navpoly->get_vertices();
if (verts.size() < 3) {
return;
}
Color color;
if (enabled) {
color = get_tree()->get_debug_navigation_color();
} else {
color = get_tree()->get_debug_navigation_disabled_color();
}
Color doors_color = color.lightened(0.2);
RandomPCG rand;
for (int i = 0; i < navpoly->get_polygon_count(); i++) {
// An array of vertices for this polygon.
Vector<int> polygon = navpoly->get_polygon(i);
Vector<Vector2> vertices;
vertices.resize(polygon.size());
for (int j = 0; j < polygon.size(); j++) {
ERR_FAIL_INDEX(polygon[j], verts.size());
vertices.write[j] = verts[polygon[j]];
}
// Generate the polygon color, slightly randomly modified from the settings one.
Color random_variation_color;
random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.05, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.1);
random_variation_color.a = color.a;
Vector<Color> colors;
colors.push_back(random_variation_color);
RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), vertices, colors);
}
// Draw the region
Transform2D xform = get_global_transform();
const Navigation2DServer *ns = Navigation2DServer::get_singleton();
real_t radius = 1.0;
if (navigation != nullptr) {
radius = Navigation2DServer::get_singleton()->map_get_edge_connection_margin(navigation->get_rid());
} else {
radius = Navigation2DServer::get_singleton()->map_get_edge_connection_margin(get_world_2d()->get_navigation_map());
}
radius = radius * 0.5;
for (int i = 0; i < ns->region_get_connections_count(region); i++) {
// Two main points
Vector2 a = ns->region_get_connection_pathway_start(region, i);
a = xform.affine_inverse().xform(a);
Vector2 b = ns->region_get_connection_pathway_end(region, i);
b = xform.affine_inverse().xform(b);
draw_line(a, b, doors_color);
// Draw a circle to illustrate the margins.
real_t angle = a.angle_to_point(b);
draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color);
draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color);
}
}
} break;
}
}
void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly) {
if (p_navpoly == navpoly) {
return;
}
if (navpoly.is_valid()) {
navpoly->disconnect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
}
navpoly = p_navpoly;
Navigation2DServer::get_singleton()->region_set_navpoly(region, p_navpoly);
if (navpoly.is_valid()) {
navpoly->connect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
}
_navpoly_changed();
_change_notify("navpoly");
update_configuration_warning();
}
Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {
return navpoly;
}
void NavigationRegion2D::_navpoly_changed() {
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
update();
}
if (navpoly.is_valid()) {
Navigation2DServer::get_singleton()->region_set_navpoly(region, navpoly);
}
}
void NavigationRegion2D::_map_changed(RID p_map) {
if (navigation != nullptr && enabled && (navigation->get_rid() == p_map)) {
update();
} else if (is_inside_tree() && enabled && (get_world_2d()->get_navigation_map() == p_map)) {
update();
}
}
String NavigationRegion2D::get_configuration_warning() const {
if (!is_visible_in_tree() || !is_inside_tree()) {
return String();
}
String warning = Node2D::get_configuration_warning();
if (!navpoly.is_valid()) {
if (warning != String()) {
warning += "\n\n";
}
warning += TTR("A NavigationPolygon resource must be set or created for this node to work. Please set a property or draw a polygon.");
}
return warning;
}
void NavigationRegion2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navpoly"), &NavigationRegion2D::set_navigation_polygon);
ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);
ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);
ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);
ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);
ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
ClassDB::bind_method(D_METHOD("_navpoly_changed"), &NavigationRegion2D::_navpoly_changed);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navpoly", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "enter_cost"), "set_enter_cost", "get_enter_cost");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "travel_cost"), "set_travel_cost", "get_travel_cost");
ClassDB::bind_method(D_METHOD("_map_changed"), &NavigationRegion2D::_map_changed);
}
NavigationRegion2D::NavigationRegion2D() {
enabled = true;
set_notify_transform(true);
region = Navigation2DServer::get_singleton()->region_create();
navigation = nullptr;
enter_cost = 0.0;
travel_cost = 1.0;
navigation_layers = 1;
Navigation2DServer::get_singleton()->region_set_enter_cost(region, get_enter_cost());
Navigation2DServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());
}
NavigationRegion2D::~NavigationRegion2D() {
Navigation2DServer::get_singleton()->free(region);
}

View File

@ -0,0 +1,90 @@
#ifndef NAVIGATION_REGION_2D_H
#define NAVIGATION_REGION_2D_H
/*************************************************************************/
/* navigation_polygon.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "core/object/resource.h"
#include "scene/2d/node_2d.h"
class NavigationMesh;
class Navigation2D;
class NavigationPolygon;
class NavigationRegion2D : public Node2D {
GDCLASS(NavigationRegion2D, Node2D);
bool enabled;
RID region;
Navigation2D *navigation;
Ref<NavigationPolygon> navpoly;
real_t enter_cost;
real_t travel_cost;
uint32_t navigation_layers;
void _navpoly_changed();
void _map_changed(RID p_map);
protected:
void _notification(int p_what);
static void _bind_methods();
public:
#ifdef TOOLS_ENABLED
virtual Rect2 _edit_get_rect() const;
virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
#endif
void set_enabled(bool p_enabled);
bool is_enabled() const;
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;
RID get_region_rid() const;
void set_enter_cost(real_t p_enter_cost);
real_t get_enter_cost() const;
void set_travel_cost(real_t p_travel_cost);
real_t get_travel_cost() const;
void set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly);
Ref<NavigationPolygon> get_navigation_polygon() const;
String get_configuration_warning() const;
NavigationRegion2D();
~NavigationRegion2D();
};
#endif // NAVIGATIONPOLYGON_H

View File

@ -54,7 +54,8 @@
#include "scene/2d/navigation_2d.h"
#include "scene/2d/navigation_agent_2d.h"
#include "scene/2d/navigation_obstacle_2d.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/2d/navigation_region_2d.h"
#include "scene/resources/navigation_polygon.h"
#include "scene/2d/parallax_background.h"
#include "scene/2d/parallax_layer.h"
#include "scene/2d/path_2d.h"
@ -681,7 +682,7 @@ void register_scene_types() {
ClassDB::register_class<Navigation2D>();
ClassDB::register_class<NavigationPolygon>();
ClassDB::register_class<NavigationPolygonInstance>();
ClassDB::register_class<NavigationRegion2D>();
ClassDB::register_class<NavigationAgent2D>();
ClassDB::register_class<NavigationObstacle2D>();

View File

@ -33,7 +33,7 @@
#include "core/core_string_names.h"
#include "core/config/engine.h"
#include "core/os/mutex.h"
#include "navigation_2d.h"
#include "scene/2d/navigation_2d.h"
#include "scene/resources/navigation_mesh.h"
#include "scene/resources/world_2d.h"
#include "servers/navigation_2d_server.h"
@ -362,289 +362,3 @@ NavigationPolygon::NavigationPolygon() {
NavigationPolygon::~NavigationPolygon() {
}
void NavigationPolygonInstance::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;
if (!is_inside_tree()) {
return;
}
if (!enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, RID());
Navigation2DServer::get_singleton_mut()->disconnect("map_changed", this, "_map_changed");
} else {
if (navigation != nullptr) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {
Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
}
Navigation2DServer::get_singleton_mut()->connect("map_changed", this, "_map_changed");
}
if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()) {
update();
}
}
bool NavigationPolygonInstance::is_enabled() const {
return enabled;
}
void NavigationPolygonInstance::set_navigation_layers(uint32_t p_navigation_layers) {
navigation_layers = p_navigation_layers;
Navigation2DServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
}
uint32_t NavigationPolygonInstance::get_navigation_layers() const {
return navigation_layers;
}
void NavigationPolygonInstance::set_enter_cost(real_t p_enter_cost) {
ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
enter_cost = MAX(p_enter_cost, 0.0);
Navigation2DServer::get_singleton()->region_set_enter_cost(region, p_enter_cost);
}
real_t NavigationPolygonInstance::get_enter_cost() const {
return enter_cost;
}
void NavigationPolygonInstance::set_travel_cost(real_t p_travel_cost) {
ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
travel_cost = MAX(p_travel_cost, 0.0);
Navigation2DServer::get_singleton()->region_set_enter_cost(region, travel_cost);
}
real_t NavigationPolygonInstance::get_travel_cost() const {
return travel_cost;
}
RID NavigationPolygonInstance::get_region_rid() const {
return region;
}
/////////////////////////////
#ifdef TOOLS_ENABLED
Rect2 NavigationPolygonInstance::_edit_get_rect() const {
return navpoly.is_valid() ? navpoly->_edit_get_rect() : Rect2();
}
bool NavigationPolygonInstance::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
return navpoly.is_valid() ? navpoly->_edit_is_selected_on_click(p_point, p_tolerance) : false;
}
#endif
void NavigationPolygonInstance::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
Node2D *c = this;
while (c) {
navigation = Object::cast_to<Navigation2D>(c);
if (navigation) {
if (enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
}
break;
}
c = Object::cast_to<Node2D>(c->get_parent());
}
if (enabled && navigation == nullptr) {
// did not find a valid navigation node parent, fallback to default navigation map on world resource
Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
}
if (enabled) {
Navigation2DServer::get_singleton_mut()->connect("map_changed", this, "_map_changed");
}
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
Navigation2DServer::get_singleton()->region_set_transform(region, get_global_transform());
} break;
case NOTIFICATION_EXIT_TREE: {
if (navigation) {
Navigation2DServer::get_singleton()->region_set_map(region, RID());
}
navigation = nullptr;
if (enabled) {
Navigation2DServer::get_singleton_mut()->disconnect("map_changed", this, "_map_changed");
}
} break;
case NOTIFICATION_DRAW: {
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()) && navpoly.is_valid()) {
PoolVector<Vector2> verts = navpoly->get_vertices();
if (verts.size() < 3) {
return;
}
Color color;
if (enabled) {
color = get_tree()->get_debug_navigation_color();
} else {
color = get_tree()->get_debug_navigation_disabled_color();
}
Color doors_color = color.lightened(0.2);
RandomPCG rand;
for (int i = 0; i < navpoly->get_polygon_count(); i++) {
// An array of vertices for this polygon.
Vector<int> polygon = navpoly->get_polygon(i);
Vector<Vector2> vertices;
vertices.resize(polygon.size());
for (int j = 0; j < polygon.size(); j++) {
ERR_FAIL_INDEX(polygon[j], verts.size());
vertices.write[j] = verts[polygon[j]];
}
// Generate the polygon color, slightly randomly modified from the settings one.
Color random_variation_color;
random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.05, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.1);
random_variation_color.a = color.a;
Vector<Color> colors;
colors.push_back(random_variation_color);
RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), vertices, colors);
}
// Draw the region
Transform2D xform = get_global_transform();
const Navigation2DServer *ns = Navigation2DServer::get_singleton();
real_t radius = 1.0;
if (navigation != nullptr) {
radius = Navigation2DServer::get_singleton()->map_get_edge_connection_margin(navigation->get_rid());
} else {
radius = Navigation2DServer::get_singleton()->map_get_edge_connection_margin(get_world_2d()->get_navigation_map());
}
radius = radius * 0.5;
for (int i = 0; i < ns->region_get_connections_count(region); i++) {
// Two main points
Vector2 a = ns->region_get_connection_pathway_start(region, i);
a = xform.affine_inverse().xform(a);
Vector2 b = ns->region_get_connection_pathway_end(region, i);
b = xform.affine_inverse().xform(b);
draw_line(a, b, doors_color);
// Draw a circle to illustrate the margins.
real_t angle = a.angle_to_point(b);
draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color);
draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color);
}
}
} break;
}
}
void NavigationPolygonInstance::set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly) {
if (p_navpoly == navpoly) {
return;
}
if (navpoly.is_valid()) {
navpoly->disconnect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
}
navpoly = p_navpoly;
Navigation2DServer::get_singleton()->region_set_navpoly(region, p_navpoly);
if (navpoly.is_valid()) {
navpoly->connect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
}
_navpoly_changed();
_change_notify("navpoly");
update_configuration_warning();
}
Ref<NavigationPolygon> NavigationPolygonInstance::get_navigation_polygon() const {
return navpoly;
}
void NavigationPolygonInstance::_navpoly_changed() {
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
update();
}
if (navpoly.is_valid()) {
Navigation2DServer::get_singleton()->region_set_navpoly(region, navpoly);
}
}
void NavigationPolygonInstance::_map_changed(RID p_map) {
if (navigation != nullptr && enabled && (navigation->get_rid() == p_map)) {
update();
} else if (is_inside_tree() && enabled && (get_world_2d()->get_navigation_map() == p_map)) {
update();
}
}
String NavigationPolygonInstance::get_configuration_warning() const {
if (!is_visible_in_tree() || !is_inside_tree()) {
return String();
}
String warning = Node2D::get_configuration_warning();
if (!navpoly.is_valid()) {
if (warning != String()) {
warning += "\n\n";
}
warning += TTR("A NavigationPolygon resource must be set or created for this node to work. Please set a property or draw a polygon.");
}
return warning;
}
void NavigationPolygonInstance::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navpoly"), &NavigationPolygonInstance::set_navigation_polygon);
ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationPolygonInstance::get_navigation_polygon);
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationPolygonInstance::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationPolygonInstance::is_enabled);
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationPolygonInstance::set_navigation_layers);
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationPolygonInstance::get_navigation_layers);
ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationPolygonInstance::get_region_rid);
ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationPolygonInstance::set_enter_cost);
ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationPolygonInstance::get_enter_cost);
ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationPolygonInstance::set_travel_cost);
ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationPolygonInstance::get_travel_cost);
ClassDB::bind_method(D_METHOD("_navpoly_changed"), &NavigationPolygonInstance::_navpoly_changed);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navpoly", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "enter_cost"), "set_enter_cost", "get_enter_cost");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "travel_cost"), "set_travel_cost", "get_travel_cost");
ClassDB::bind_method(D_METHOD("_map_changed"), &NavigationPolygonInstance::_map_changed);
}
NavigationPolygonInstance::NavigationPolygonInstance() {
enabled = true;
set_notify_transform(true);
region = Navigation2DServer::get_singleton()->region_create();
navigation = nullptr;
enter_cost = 0.0;
travel_cost = 1.0;
navigation_layers = 1;
Navigation2DServer::get_singleton()->region_set_enter_cost(region, get_enter_cost());
Navigation2DServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());
}
NavigationPolygonInstance::~NavigationPolygonInstance() {
Navigation2DServer::get_singleton()->free(region);
}

View File

@ -92,55 +92,4 @@ public:
~NavigationPolygon();
};
class Navigation2D;
class NavigationPolygonInstance : public Node2D {
GDCLASS(NavigationPolygonInstance, Node2D);
bool enabled;
RID region;
Navigation2D *navigation;
Ref<NavigationPolygon> navpoly;
real_t enter_cost;
real_t travel_cost;
uint32_t navigation_layers;
void _navpoly_changed();
void _map_changed(RID p_map);
protected:
void _notification(int p_what);
static void _bind_methods();
public:
#ifdef TOOLS_ENABLED
virtual Rect2 _edit_get_rect() const;
virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
#endif
void set_enabled(bool p_enabled);
bool is_enabled() const;
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;
RID get_region_rid() const;
void set_enter_cost(real_t p_enter_cost);
real_t get_enter_cost() const;
void set_travel_cost(real_t p_travel_cost);
real_t get_travel_cost() const;
void set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly);
Ref<NavigationPolygon> get_navigation_polygon() const;
String get_configuration_warning() const;
NavigationPolygonInstance();
~NavigationPolygonInstance();
};
#endif // NAVIGATIONPOLYGON_H

View File

@ -33,7 +33,9 @@
#include "core/containers/rid.h"
#include "core/object/object.h"
#include "scene/2d/navigation_polygon.h"
#include "scene/2d/navigation_region_2d.h"
#include "scene/resources/navigation_polygon.h"
// This server exposes the 3D `NavigationServer` features in the 2D world.
class Navigation2DServer : public Object {