Make Navigation Agents and Obstacles respect parent process mode

Temporarily removes agent from navigation map when parent node cannot process due to SceneTree pause and process_mode property. Normal process_mode does not work as other agents would still avoid the paused agents because they were still active on the navigation map and the rvo world. Also fixes potential crash when region_get_map or agent_get_map is called while no map is set.

(cherry picked from commit 6b51ab66d8)
This commit is contained in:
smix8 2022-05-17 20:08:39 +02:00 committed by Rémi Verschelde
parent 44915696d2
commit 88acb5b798
9 changed files with 86 additions and 3 deletions

View file

@ -273,14 +273,20 @@ Array GodotNavigationServer::map_get_agents(RID p_map) const {
RID GodotNavigationServer::region_get_map(RID p_region) const {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(region == nullptr, RID());
if (region->get_map()) {
return region->get_map()->get_self();
}
return RID();
}
RID GodotNavigationServer::agent_get_map(RID p_agent) const {
RvoAgent *agent = agent_owner.getornull(p_agent);
ERR_FAIL_COND_V(agent == nullptr, RID());
if (agent->get_map()) {
return agent->get_map()->get_self();
}
return RID();
}
RID GodotNavigationServer::region_create() const {
auto mut_this = const_cast<GodotNavigationServer *>(this);

View file

@ -114,6 +114,24 @@ void NavigationAgent2D::_notification(int p_what) {
set_physics_process_internal(true);
} break;
case NOTIFICATION_PAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_EXIT_TREE: {
agent_parent = nullptr;
set_navigation(nullptr);

View file

@ -44,6 +44,7 @@ class NavigationAgent2D : public Node {
Navigation2D *navigation;
RID agent;
RID map_before_pause;
real_t target_desired_distance;
real_t radius;

View file

@ -94,6 +94,24 @@ void NavigationObstacle2D::_notification(int p_what) {
case NOTIFICATION_UNPARENTED: {
parent_node2d = nullptr;
} break;
case NOTIFICATION_PAUSED: {
if (parent_node2d && !parent_node2d->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_node2d && parent_node2d->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (parent_node2d && !parent_node2d->can_process()) {
map_before_pause = Navigation2DServer::get_singleton()->agent_get_map(get_rid());
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_node2d && parent_node2d->can_process() && !(map_before_pause == RID())) {
Navigation2DServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (parent_node2d && parent_node2d->is_inside_tree()) {
Navigation2DServer::get_singleton()->agent_set_position(agent, parent_node2d->get_global_transform().get_origin());

View file

@ -41,8 +41,9 @@ class NavigationObstacle2D : public Node {
Navigation2D *navigation;
Node2D *parent_node2d = nullptr;
RID agent;
RID map_before_pause;
bool estimate_radius = true;
real_t radius = 1.0;

View file

@ -130,6 +130,24 @@ void NavigationAgent::_notification(int p_what) {
// the navigation map may not be ready at that time. This fixes issues with taking the agent out of the scene tree.
request_ready();
} break;
case NOTIFICATION_PAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (agent_parent && !agent_parent->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (agent_parent) {
NavigationServer::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);

View file

@ -44,6 +44,7 @@ class NavigationAgent : public Node {
Navigation *navigation;
RID agent;
RID map_before_pause;
real_t target_desired_distance;
real_t radius;

View file

@ -81,6 +81,24 @@ void NavigationObstacle::_notification(int p_what) {
set_physics_process_internal(true);
} break;
case NOTIFICATION_PAUSED: {
if (parent_spatial && !parent_spatial->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_spatial && parent_spatial->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_UNPAUSED: {
if (parent_spatial && !parent_spatial->can_process()) {
map_before_pause = NavigationServer::get_singleton()->agent_get_map(get_rid());
NavigationServer::get_singleton()->agent_set_map(get_rid(), RID());
} else if (parent_spatial && parent_spatial->can_process() && !(map_before_pause == RID())) {
NavigationServer::get_singleton()->agent_set_map(get_rid(), map_before_pause);
map_before_pause = RID();
}
} break;
case NOTIFICATION_EXIT_TREE: {
set_navigation(nullptr);
set_physics_process_internal(false);

View file

@ -43,6 +43,8 @@ class NavigationObstacle : public Node {
Navigation *navigation;
RID agent;
RID map_before_pause;
bool estimate_radius = true;
real_t radius = 1.0;