diff options
Diffstat (limited to 'scene/3d/area_3d.cpp')
-rw-r--r-- | scene/3d/area_3d.cpp | 151 |
1 files changed, 57 insertions, 94 deletions
diff --git a/scene/3d/area_3d.cpp b/scene/3d/area_3d.cpp index 5b02a91793..a024757927 100644 --- a/scene/3d/area_3d.cpp +++ b/scene/3d/area_3d.cpp @@ -35,27 +35,24 @@ #include "servers/physics_server_3d.h" void Area3D::set_space_override_mode(SpaceOverride p_mode) { - space_override = p_mode; PhysicsServer3D::get_singleton()->area_set_space_override_mode(get_rid(), PhysicsServer3D::AreaSpaceOverrideMode(p_mode)); } -Area3D::SpaceOverride Area3D::get_space_override_mode() const { +Area3D::SpaceOverride Area3D::get_space_override_mode() const { return space_override; } void Area3D::set_gravity_is_point(bool p_enabled) { - gravity_is_point = p_enabled; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT, p_enabled); } -bool Area3D::is_gravity_a_point() const { +bool Area3D::is_gravity_a_point() const { return gravity_is_point; } void Area3D::set_gravity_distance_scale(real_t p_scale) { - gravity_distance_scale = p_scale; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE, p_scale); } @@ -65,57 +62,51 @@ real_t Area3D::get_gravity_distance_scale() const { } void Area3D::set_gravity_vector(const Vector3 &p_vec) { - gravity_vec = p_vec; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, p_vec); } -Vector3 Area3D::get_gravity_vector() const { +Vector3 Area3D::get_gravity_vector() const { return gravity_vec; } void Area3D::set_gravity(real_t p_gravity) { - gravity = p_gravity; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY, p_gravity); } -real_t Area3D::get_gravity() const { +real_t Area3D::get_gravity() const { return gravity; } -void Area3D::set_linear_damp(real_t p_linear_damp) { +void Area3D::set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_LINEAR_DAMP, p_linear_damp); } -real_t Area3D::get_linear_damp() const { +real_t Area3D::get_linear_damp() const { return linear_damp; } void Area3D::set_angular_damp(real_t p_angular_damp) { - angular_damp = p_angular_damp; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP, p_angular_damp); } real_t Area3D::get_angular_damp() const { - return angular_damp; } void Area3D::set_priority(real_t p_priority) { - priority = p_priority; PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_PRIORITY, p_priority); } -real_t Area3D::get_priority() const { +real_t Area3D::get_priority() const { return priority; } void Area3D::_body_enter_tree(ObjectID p_id) { - Object *obj = ObjectDB::get_instance(p_id); Node *node = Object::cast_to<Node>(obj); ERR_FAIL_COND(!node); @@ -127,13 +118,11 @@ void Area3D::_body_enter_tree(ObjectID p_id) { E->get().in_tree = true; emit_signal(SceneStringNames::get_singleton()->body_entered, node); for (int i = 0; i < E->get().shapes.size(); i++) { - emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].area_shape); } } void Area3D::_body_exit_tree(ObjectID p_id) { - Object *obj = ObjectDB::get_instance(p_id); Node *node = Object::cast_to<Node>(obj); ERR_FAIL_COND(!node); @@ -143,13 +132,11 @@ void Area3D::_body_exit_tree(ObjectID p_id) { E->get().in_tree = false; emit_signal(SceneStringNames::get_singleton()->body_exited, node); for (int i = 0; i < E->get().shapes.size(); i++) { - emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].area_shape); } } void Area3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_area_shape) { - bool body_in = p_status == PhysicsServer3D::AREA_BODY_ADDED; ObjectID objid = p_instance; @@ -166,7 +153,6 @@ void Area3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, i if (body_in) { if (!E) { - E = body_map.insert(objid, BodyState()); E->get().rc = 0; E->get().in_tree = node && node->is_inside_tree(); @@ -179,29 +165,30 @@ void Area3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, i } } E->get().rc++; - if (node) + if (node) { E->get().shapes.insert(ShapePair(p_body_shape, p_area_shape)); + } if (E->get().in_tree) { emit_signal(SceneStringNames::get_singleton()->body_shape_entered, objid, node, p_body_shape, p_area_shape); } } else { - E->get().rc--; - if (node) + if (node) { E->get().shapes.erase(ShapePair(p_body_shape, p_area_shape)); + } bool eraseit = false; if (E->get().rc == 0) { - 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->get().in_tree) + if (E->get().in_tree) { emit_signal(SceneStringNames::get_singleton()->body_exited, obj); + } } eraseit = true; @@ -210,15 +197,15 @@ void Area3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, i emit_signal(SceneStringNames::get_singleton()->body_shape_exited, objid, obj, p_body_shape, p_area_shape); } - if (eraseit) + if (eraseit) { body_map.erase(E); + } } locked = false; } void Area3D::_clear_monitoring() { - ERR_FAIL_COND_MSG(locked, "This function can't be used during the in/out signal."); { @@ -227,19 +214,19 @@ void Area3D::_clear_monitoring() { //disconnect all monitored stuff for (Map<ObjectID, BodyState>::Element *E = bmcopy.front(); E; E = E->next()) { - Object *obj = ObjectDB::get_instance(E->key()); Node *node = Object::cast_to<Node>(obj); - if (!node) //node may have been deleted in previous frame or at other legiminate point + if (!node) { //node may have been deleted in previous frame or at other legiminate point continue; + } //ERR_CONTINUE(!node); - if (!E->get().in_tree) + if (!E->get().in_tree) { continue; + } for (int i = 0; i < E->get().shapes.size(); i++) { - emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E->key(), node, E->get().shapes[i].body_shape, E->get().shapes[i].area_shape); } @@ -251,25 +238,24 @@ void Area3D::_clear_monitoring() { } { - Map<ObjectID, AreaState> bmcopy = area_map; area_map.clear(); //disconnect all monitored stuff for (Map<ObjectID, AreaState>::Element *E = bmcopy.front(); E; E = E->next()) { - Object *obj = ObjectDB::get_instance(E->key()); Node *node = Object::cast_to<Node>(obj); - if (!node) //node may have been deleted in previous frame or at other legiminate point + if (!node) { //node may have been deleted in previous frame or at other legiminate point continue; + } //ERR_CONTINUE(!node); - if (!E->get().in_tree) + if (!E->get().in_tree) { continue; + } for (int i = 0; i < E->get().shapes.size(); i++) { - emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E->key(), node, E->get().shapes[i].area_shape, E->get().shapes[i].self_shape); } @@ -280,35 +266,33 @@ void Area3D::_clear_monitoring() { } } } -void Area3D::_notification(int p_what) { +void Area3D::_notification(int p_what) { if (p_what == NOTIFICATION_EXIT_TREE) { _clear_monitoring(); } } void Area3D::set_monitoring(bool p_enable) { - ERR_FAIL_COND_MSG(locked, "Function blocked during in/out signal. Use set_deferred(\"monitoring\", true/false)."); - if (p_enable == monitoring) + if (p_enable == monitoring) { return; + } monitoring = p_enable; if (monitoring) { - PhysicsServer3D::get_singleton()->area_set_monitor_callback(get_rid(), this, SceneStringNames::get_singleton()->_body_inout); PhysicsServer3D::get_singleton()->area_set_area_monitor_callback(get_rid(), this, SceneStringNames::get_singleton()->_area_inout); } else { - PhysicsServer3D::get_singleton()->area_set_monitor_callback(get_rid(), NULL, StringName()); - PhysicsServer3D::get_singleton()->area_set_area_monitor_callback(get_rid(), NULL, StringName()); + PhysicsServer3D::get_singleton()->area_set_monitor_callback(get_rid(), nullptr, StringName()); + PhysicsServer3D::get_singleton()->area_set_area_monitor_callback(get_rid(), nullptr, StringName()); _clear_monitoring(); } } void Area3D::_area_enter_tree(ObjectID p_id) { - Object *obj = ObjectDB::get_instance(p_id); Node *node = Object::cast_to<Node>(obj); ERR_FAIL_COND(!node); @@ -320,13 +304,11 @@ void Area3D::_area_enter_tree(ObjectID p_id) { E->get().in_tree = true; emit_signal(SceneStringNames::get_singleton()->area_entered, node); for (int i = 0; i < E->get().shapes.size(); i++) { - emit_signal(SceneStringNames::get_singleton()->area_shape_entered, p_id, node, E->get().shapes[i].area_shape, E->get().shapes[i].self_shape); } } void Area3D::_area_exit_tree(ObjectID p_id) { - Object *obj = ObjectDB::get_instance(p_id); Node *node = Object::cast_to<Node>(obj); ERR_FAIL_COND(!node); @@ -336,13 +318,11 @@ void Area3D::_area_exit_tree(ObjectID p_id) { E->get().in_tree = false; emit_signal(SceneStringNames::get_singleton()->area_exited, node); for (int i = 0; i < E->get().shapes.size(); i++) { - emit_signal(SceneStringNames::get_singleton()->area_shape_exited, p_id, node, E->get().shapes[i].area_shape, E->get().shapes[i].self_shape); } } void Area3D::_area_inout(int p_status, const RID &p_area, ObjectID p_instance, int p_area_shape, int p_self_shape) { - bool area_in = p_status == PhysicsServer3D::AREA_BODY_ADDED; ObjectID objid = p_instance; @@ -359,7 +339,6 @@ void Area3D::_area_inout(int p_status, const RID &p_area, ObjectID p_instance, i if (area_in) { if (!E) { - E = area_map.insert(objid, AreaState()); E->get().rc = 0; E->get().in_tree = node && node->is_inside_tree(); @@ -372,24 +351,24 @@ void Area3D::_area_inout(int p_status, const RID &p_area, ObjectID p_instance, i } } E->get().rc++; - if (node) + if (node) { E->get().shapes.insert(AreaShapePair(p_area_shape, p_self_shape)); + } if (!node || E->get().in_tree) { emit_signal(SceneStringNames::get_singleton()->area_shape_entered, objid, node, p_area_shape, p_self_shape); } } else { - E->get().rc--; - if (node) + if (node) { E->get().shapes.erase(AreaShapePair(p_area_shape, p_self_shape)); + } bool eraseit = false; if (E->get().rc == 0) { - 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)); @@ -404,20 +383,19 @@ void Area3D::_area_inout(int p_status, const RID &p_area, ObjectID p_instance, i emit_signal(SceneStringNames::get_singleton()->area_shape_exited, objid, obj, p_area_shape, p_self_shape); } - if (eraseit) + if (eraseit) { area_map.erase(E); + } } locked = false; } bool Area3D::is_monitoring() const { - return monitoring; } -Array Area3D::get_overlapping_bodies() const { - +TypedArray<Node3D> Area3D::get_overlapping_bodies() const { ERR_FAIL_COND_V(!monitoring, Array()); Array ret; ret.resize(body_map.size()); @@ -435,11 +413,11 @@ Array Area3D::get_overlapping_bodies() const { } void Area3D::set_monitorable(bool p_enable) { - ERR_FAIL_COND_MSG(locked || (is_inside_tree() && PhysicsServer3D::get_singleton()->is_flushing_queries()), "Function blocked during in/out signal. Use set_deferred(\"monitorable\", true/false)."); - if (p_enable == monitorable) + if (p_enable == monitorable) { return; + } monitorable = p_enable; @@ -447,12 +425,10 @@ void Area3D::set_monitorable(bool p_enable) { } bool Area3D::is_monitorable() const { - return monitorable; } -Array Area3D::get_overlapping_areas() const { - +TypedArray<Area3D> Area3D::get_overlapping_areas() const { ERR_FAIL_COND_V(!monitoring, Array()); Array ret; ret.resize(area_map.size()); @@ -470,89 +446,82 @@ Array Area3D::get_overlapping_areas() const { } bool Area3D::overlaps_area(Node *p_area) const { - ERR_FAIL_NULL_V(p_area, false); const Map<ObjectID, AreaState>::Element *E = area_map.find(p_area->get_instance_id()); - if (!E) + if (!E) { return false; + } return E->get().in_tree; } bool Area3D::overlaps_body(Node *p_body) const { - ERR_FAIL_NULL_V(p_body, false); const Map<ObjectID, BodyState>::Element *E = body_map.find(p_body->get_instance_id()); - if (!E) + if (!E) { return false; + } return E->get().in_tree; } -void Area3D::set_collision_mask(uint32_t p_mask) { +void Area3D::set_collision_mask(uint32_t p_mask) { collision_mask = p_mask; PhysicsServer3D::get_singleton()->area_set_collision_mask(get_rid(), p_mask); } uint32_t Area3D::get_collision_mask() const { - return collision_mask; } -void Area3D::set_collision_layer(uint32_t p_layer) { +void Area3D::set_collision_layer(uint32_t p_layer) { collision_layer = p_layer; PhysicsServer3D::get_singleton()->area_set_collision_layer(get_rid(), p_layer); } uint32_t Area3D::get_collision_layer() const { - return collision_layer; } void Area3D::set_collision_mask_bit(int p_bit, bool p_value) { - uint32_t mask = get_collision_mask(); - if (p_value) + if (p_value) { mask |= 1 << p_bit; - else + } else { mask &= ~(1 << p_bit); + } set_collision_mask(mask); } bool Area3D::get_collision_mask_bit(int p_bit) const { - return get_collision_mask() & (1 << p_bit); } void Area3D::set_collision_layer_bit(int p_bit, bool p_value) { - uint32_t layer = get_collision_layer(); - if (p_value) + if (p_value) { layer |= 1 << p_bit; - else + } else { layer &= ~(1 << p_bit); + } set_collision_layer(layer); } bool Area3D::get_collision_layer_bit(int p_bit) const { - return get_collision_layer() & (1 << p_bit); } void Area3D::set_audio_bus_override(bool p_override) { - audio_bus_override = p_override; } bool Area3D::is_overriding_audio_bus() const { - return audio_bus_override; } void Area3D::set_audio_bus(const StringName &p_audio_bus) { - audio_bus = p_audio_bus; } -StringName Area3D::get_audio_bus() const { +StringName Area3D::get_audio_bus() const { for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { if (AudioServer::get_singleton()->get_bus_name(i) == audio_bus) { return audio_bus; @@ -562,20 +531,18 @@ StringName Area3D::get_audio_bus() const { } void Area3D::set_use_reverb_bus(bool p_enable) { - use_reverb_bus = p_enable; } -bool Area3D::is_using_reverb_bus() const { +bool Area3D::is_using_reverb_bus() const { return use_reverb_bus; } void Area3D::set_reverb_bus(const StringName &p_audio_bus) { - reverb_bus = p_audio_bus; } -StringName Area3D::get_reverb_bus() const { +StringName Area3D::get_reverb_bus() const { for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { if (AudioServer::get_singleton()->get_bus_name(i) == reverb_bus) { return reverb_bus; @@ -585,31 +552,28 @@ StringName Area3D::get_reverb_bus() const { } void Area3D::set_reverb_amount(float p_amount) { - reverb_amount = p_amount; } -float Area3D::get_reverb_amount() const { +float Area3D::get_reverb_amount() const { return reverb_amount; } void Area3D::set_reverb_uniformity(float p_uniformity) { - reverb_uniformity = p_uniformity; } -float Area3D::get_reverb_uniformity() const { +float Area3D::get_reverb_uniformity() const { return reverb_uniformity; } void Area3D::_validate_property(PropertyInfo &property) const { - if (property.name == "audio_bus_name" || property.name == "reverb_bus_name") { - String options; for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) { - if (i > 0) + if (i > 0) { options += ","; + } String name = AudioServer::get_singleton()->get_bus_name(i); options += name; } @@ -729,7 +693,6 @@ void Area3D::_bind_methods() { Area3D::Area3D() : CollisionObject3D(PhysicsServer3D::get_singleton()->area_create(), true) { - space_override = SPACE_OVERRIDE_DISABLED; set_gravity(9.8); locked = false; |