summaryrefslogtreecommitdiff
path: root/scene/2d/navigation_agent_2d.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/2d/navigation_agent_2d.cpp')
-rw-r--r--scene/2d/navigation_agent_2d.cpp52
1 files changed, 39 insertions, 13 deletions
diff --git a/scene/2d/navigation_agent_2d.cpp b/scene/2d/navigation_agent_2d.cpp
index d7f75c63a4..1a62c9bb6c 100644
--- a/scene/2d/navigation_agent_2d.cpp
+++ b/scene/2d/navigation_agent_2d.cpp
@@ -75,6 +75,7 @@ void NavigationAgent2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_target_location", "location"), &NavigationAgent2D::set_target_location);
ClassDB::bind_method(D_METHOD("get_target_location"), &NavigationAgent2D::get_target_location);
+
ClassDB::bind_method(D_METHOD("get_next_location"), &NavigationAgent2D::get_next_location);
ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target);
ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity);
@@ -88,6 +89,7 @@ void NavigationAgent2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done);
ADD_GROUP("Pathfinding", "");
+ ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_location", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_location", "get_target_location");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,suffix:px"), "set_path_desired_distance", "get_path_desired_distance");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,suffix:px"), "set_target_desired_distance", "get_target_desired_distance");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "10,100,1,suffix:px"), "set_path_max_distance", "get_path_max_distance");
@@ -104,7 +106,7 @@ void NavigationAgent2D::_bind_methods() {
ADD_SIGNAL(MethodInfo("path_changed"));
ADD_SIGNAL(MethodInfo("target_reached"));
ADD_SIGNAL(MethodInfo("navigation_finished"));
- ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
+ ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR2, "safe_velocity")));
}
void NavigationAgent2D::_notification(int p_what) {
@@ -178,6 +180,13 @@ NavigationAgent2D::NavigationAgent2D() {
set_time_horizon(20.0);
set_radius(10.0);
set_max_speed(200.0);
+
+ // Preallocate query and result objects to improve performance.
+ navigation_query = Ref<NavigationPathQueryParameters2D>();
+ navigation_query.instantiate();
+
+ navigation_result = Ref<NavigationPathQueryResult2D>();
+ navigation_result.instantiate();
}
NavigationAgent2D::~NavigationAgent2D() {
@@ -314,6 +323,8 @@ Vector2 NavigationAgent2D::get_target_location() const {
Vector2 NavigationAgent2D::get_next_location() {
update_navigation();
+
+ const Vector<Vector2> &navigation_path = navigation_result->get_path();
if (navigation_path.size() == 0) {
ERR_FAIL_COND_V_MSG(agent_parent == nullptr, Vector2(), "The agent has no parent.");
return agent_parent->get_global_position();
@@ -322,6 +333,10 @@ Vector2 NavigationAgent2D::get_next_location() {
}
}
+const Vector<Vector2> &NavigationAgent2D::get_nav_path() const {
+ return navigation_result->get_path();
+}
+
real_t NavigationAgent2D::distance_to_target() const {
ERR_FAIL_COND_V_MSG(agent_parent == nullptr, 0.0, "The agent has no parent.");
return agent_parent->get_global_position().distance_to(target_location);
@@ -342,6 +357,8 @@ bool NavigationAgent2D::is_navigation_finished() {
Vector2 NavigationAgent2D::get_final_location() {
update_navigation();
+
+ const Vector<Vector2> &navigation_path = navigation_result->get_path();
if (navigation_path.size() == 0) {
return Vector2();
}
@@ -368,8 +385,8 @@ void NavigationAgent2D::_avoidance_done(Vector3 p_new_velocity) {
emit_signal(SNAME("velocity_computed"), velocity);
}
-TypedArray<String> NavigationAgent2D::get_configuration_warnings() const {
- TypedArray<String> warnings = Node::get_configuration_warnings();
+PackedStringArray NavigationAgent2D::get_configuration_warnings() const {
+ PackedStringArray warnings = Node::get_configuration_warnings();
if (!Object::cast_to<Node2D>(get_parent())) {
warnings.push_back(RTR("The NavigationAgent2D can be used only under a Node2D inheriting parent node."));
@@ -391,22 +408,24 @@ void NavigationAgent2D::update_navigation() {
update_frame_id = Engine::get_singleton()->get_physics_frames();
- Vector2 o = agent_parent->get_global_position();
+ Vector2 origin = agent_parent->get_global_position();
bool reload_path = false;
if (NavigationServer2D::get_singleton()->agent_is_map_changed(agent)) {
reload_path = true;
- } else if (navigation_path.size() == 0) {
+ } else if (navigation_result->get_path().size() == 0) {
reload_path = true;
} else {
// Check if too far from the navigation path
if (nav_path_index > 0) {
+ const Vector<Vector2> &navigation_path = navigation_result->get_path();
+
Vector2 segment[2];
segment[0] = navigation_path[nav_path_index - 1];
segment[1] = navigation_path[nav_path_index];
- Vector2 p = Geometry2D::get_closest_point_to_segment(o, segment);
- if (o.distance_to(p) >= path_max_distance) {
+ Vector2 p = Geometry2D::get_closest_point_to_segment(origin, segment);
+ if (origin.distance_to(p) >= path_max_distance) {
// To faraway, reload path
reload_path = true;
}
@@ -414,24 +433,31 @@ void NavigationAgent2D::update_navigation() {
}
if (reload_path) {
+ navigation_query->set_start_position(origin);
+ navigation_query->set_target_position(target_location);
+ navigation_query->set_navigation_layers(navigation_layers);
+
if (map_override.is_valid()) {
- navigation_path = NavigationServer2D::get_singleton()->map_get_path(map_override, o, target_location, true, navigation_layers);
+ navigation_query->set_map(map_override);
} else {
- navigation_path = NavigationServer2D::get_singleton()->map_get_path(agent_parent->get_world_2d()->get_navigation_map(), o, target_location, true, navigation_layers);
+ navigation_query->set_map(agent_parent->get_world_2d()->get_navigation_map());
}
+
+ NavigationServer2D::get_singleton()->query_path(navigation_query, navigation_result);
navigation_finished = false;
nav_path_index = 0;
emit_signal(SNAME("path_changed"));
}
- if (navigation_path.size() == 0) {
+ if (navigation_result->get_path().size() == 0) {
return;
}
// Check if we can advance the navigation path
if (navigation_finished == false) {
// Advances to the next far away location.
- while (o.distance_to(navigation_path[nav_path_index]) < path_desired_distance) {
+ const Vector<Vector2> &navigation_path = navigation_result->get_path();
+ while (origin.distance_to(navigation_path[nav_path_index]) < path_desired_distance) {
nav_path_index += 1;
if (nav_path_index == navigation_path.size()) {
_check_distance_to_target();
@@ -445,7 +471,7 @@ void NavigationAgent2D::update_navigation() {
}
void NavigationAgent2D::_request_repath() {
- navigation_path.clear();
+ navigation_result->reset();
target_reached = false;
navigation_finished = false;
update_frame_id = 0;
@@ -454,8 +480,8 @@ void NavigationAgent2D::_request_repath() {
void NavigationAgent2D::_check_distance_to_target() {
if (!target_reached) {
if (distance_to_target() < target_desired_distance) {
- emit_signal(SNAME("target_reached"));
target_reached = true;
+ emit_signal(SNAME("target_reached"));
}
}
}