Skip to content

Commit

Permalink
Same for Area3D
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindbee committed Sep 17, 2023
1 parent e742bc4 commit c2f65fb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
13 changes: 6 additions & 7 deletions scene/2d/area_2d.cpp
Expand Up @@ -391,7 +391,7 @@ void Area2D::_notification(int p_what) {
} break;

case NOTIFICATION_DISABLED: {
// Clean up the remaining bodies so that it can work properly when enabled.
// Clean up for the remaining bodies so that it can work properly when enabled.
for (KeyValue<ObjectID, Area2D::BodyState> &E : body_map) {
Object *obj = ObjectDB::get_instance(E.key);
Node *node = Object::cast_to<Node>(obj);
Expand All @@ -400,18 +400,18 @@ void Area2D::_notification(int p_what) {
node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area2D::_body_enter_tree));
node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area2D::_body_exit_tree));
if (E.value.in_tree) {
emit_signal(SceneStringNames::get_singleton()->body_exited, obj);
emit_signal(SceneStringNames::get_singleton()->body_exited, node);
}
}
if (!node || E.value.in_tree) {
for (int i = 0; i < E.value.shapes.size(); i++) {
emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E.value.rid, node, E.value.shapes[i].body_shape, E.value.shapes[i].area_shape);
emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E.value.rid, obj, E.value.shapes[i].body_shape, E.value.shapes[i].area_shape);
}
}
}
body_map.clear();

// Clean up the remaining areas so that it can work properly when enabled.
// Clean up for the remaining areas so that it can work properly when enabled.
for (KeyValue<ObjectID, Area2D::AreaState> &E : area_map) {
Object *obj = ObjectDB::get_instance(E.key);
Node *node = Object::cast_to<Node>(obj);
Expand All @@ -420,17 +420,16 @@ void Area2D::_notification(int p_what) {
node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area2D::_area_enter_tree));
node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area2D::_area_exit_tree));
if (E.value.in_tree) {
emit_signal(SceneStringNames::get_singleton()->area_exited, obj);
emit_signal(SceneStringNames::get_singleton()->area_exited, node);
}
}
if (!node || E.value.in_tree) {
for (int i = 0; i < E.value.shapes.size(); i++) {
emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E.value.rid, node, E.value.shapes[i].area_shape, E.value.shapes[i].self_shape);
emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E.value.rid, obj, E.value.shapes[i].area_shape, E.value.shapes[i].self_shape);
}
}
}
area_map.clear();

} break;
}
}
Expand Down
42 changes: 42 additions & 0 deletions scene/3d/area_3d.cpp
Expand Up @@ -356,6 +356,48 @@ void Area3D::_notification(int p_what) {
case NOTIFICATION_ENTER_TREE: {
_initialize_wind();
} break;

case NOTIFICATION_DISABLED: {
// Clean up for the remaining bodies so that it can work properly when enabled.
for (KeyValue<ObjectID, Area3D::BodyState> &E : body_map) {
Object *obj = ObjectDB::get_instance(E.key);
Node *node = Object::cast_to<Node>(obj);

if (node) {
node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_body_enter_tree));
node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_body_exit_tree));
if (E.value.in_tree) {
emit_signal(SceneStringNames::get_singleton()->body_exited, node);
}
}
if (!node || E.value.in_tree) {
for (int i = 0; i < E.value.shapes.size(); i++) {
emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E.value.rid, obj, E.value.shapes[i].body_shape, E.value.shapes[i].area_shape);
}
}
}
body_map.clear();

// Clean up for the remaining areas so that it can work properly when enabled.
for (KeyValue<ObjectID, Area3D::AreaState> &E : area_map) {
Object *obj = ObjectDB::get_instance(E.key);
Node *node = Object::cast_to<Node>(obj);

if (node) {
node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_area_enter_tree));
node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_area_exit_tree));
if (E.value.in_tree) {
emit_signal(SceneStringNames::get_singleton()->area_exited, node);
}
}
if (!node || E.value.in_tree) {
for (int i = 0; i < E.value.shapes.size(); i++) {
emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E.value.rid, obj, E.value.shapes[i].area_shape, E.value.shapes[i].self_shape);
}
}
}
area_map.clear();
} break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions servers/physics_3d/godot_area_3d.h
Expand Up @@ -204,7 +204,7 @@ void GodotArea3D::add_body_to_query(GodotBody3D *p_body, uint32_t p_body_shape,
void GodotArea3D::remove_body_from_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
BodyKey bk(p_body, p_body_shape, p_area_shape);
monitored_bodies[bk].dec();
if (!monitor_query_list.in_list()) {
if (get_space() && !monitor_query_list.in_list()) {
_queue_monitor_update();
}
}
Expand All @@ -220,7 +220,7 @@ void GodotArea3D::add_area_to_query(GodotArea3D *p_area, uint32_t p_area_shape,
void GodotArea3D::remove_area_from_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
BodyKey bk(p_area, p_area_shape, p_self_shape);
monitored_areas[bk].dec();
if (!monitor_query_list.in_list()) {
if (get_space() && !monitor_query_list.in_list()) {
_queue_monitor_update();
}
}
Expand Down
11 changes: 6 additions & 5 deletions servers/physics_3d/godot_collision_object_3d.cpp
Expand Up @@ -210,20 +210,21 @@ void GodotCollisionObject3D::_update_shapes_with_motion(const Vector3 &p_motion)
}

void GodotCollisionObject3D::_set_space(GodotSpace3D *p_space) {
if (space) {
space->remove_object(this);
GodotSpace3D *old_space = space;
space = p_space;

if (old_space) {
old_space->remove_object(this);

for (int i = 0; i < shapes.size(); i++) {
Shape &s = shapes.write[i];
if (s.bpid) {
space->get_broadphase()->remove(s.bpid);
old_space->get_broadphase()->remove(s.bpid);
s.bpid = 0;
}
}
}

space = p_space;

if (space) {
space->add_object(this);
_update_shapes();
Expand Down

0 comments on commit c2f65fb

Please sign in to comment.