2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* area_2d.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
2021-01-01 19:13:46 +00:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 01:10:30 +00:00
/* */
/* 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. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2014-02-10 01:10:30 +00:00
# include "area_2d.h"
2020-02-21 22:26:13 +00:00
2014-02-10 01:10:30 +00:00
# include "scene/scene_string_names.h"
2017-06-19 00:07:32 +00:00
# include "servers/audio_server.h"
2020-03-27 18:21:27 +00:00
# include "servers/physics_server_2d.h"
2017-06-19 00:07:32 +00:00
2014-02-10 01:10:30 +00:00
void Area2D : : set_space_override_mode ( SpaceOverride p_mode ) {
2017-03-05 15:44:50 +00:00
space_override = p_mode ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_space_override_mode ( get_rid ( ) , PhysicsServer2D : : AreaSpaceOverrideMode ( p_mode ) ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
Area2D : : SpaceOverride Area2D : : get_space_override_mode ( ) const {
2014-02-10 01:10:30 +00:00
return space_override ;
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_gravity_is_point ( bool p_enabled ) {
gravity_is_point = p_enabled ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_GRAVITY_IS_POINT , p_enabled ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
bool Area2D : : is_gravity_a_point ( ) const {
2014-02-10 01:10:30 +00:00
return gravity_is_point ;
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_gravity_distance_scale ( real_t p_scale ) {
gravity_distance_scale = p_scale ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_GRAVITY_DISTANCE_SCALE , p_scale ) ;
2015-04-29 16:06:25 +00:00
}
2017-03-05 15:44:50 +00:00
real_t Area2D : : get_gravity_distance_scale ( ) const {
2015-04-29 16:06:25 +00:00
return gravity_distance_scale ;
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_gravity_vector ( const Vector2 & p_vec ) {
gravity_vec = p_vec ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_GRAVITY_VECTOR , p_vec ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
Vector2 Area2D : : get_gravity_vector ( ) const {
2014-02-10 01:10:30 +00:00
return gravity_vec ;
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_gravity ( real_t p_gravity ) {
gravity = p_gravity ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_GRAVITY , p_gravity ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
real_t Area2D : : get_gravity ( ) const {
2014-02-10 01:10:30 +00:00
return gravity ;
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_linear_damp ( real_t p_linear_damp ) {
linear_damp = p_linear_damp ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_LINEAR_DAMP , p_linear_damp ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
real_t Area2D : : get_linear_damp ( ) const {
2015-01-05 21:37:12 +00:00
return linear_damp ;
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_angular_damp ( real_t p_angular_damp ) {
angular_damp = p_angular_damp ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_ANGULAR_DAMP , p_angular_damp ) ;
2015-01-05 21:37:12 +00:00
}
2017-03-05 15:44:50 +00:00
real_t Area2D : : get_angular_damp ( ) const {
2015-01-05 21:37:12 +00:00
return angular_damp ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void Area2D : : set_priority ( real_t p_priority ) {
priority = p_priority ;
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_param ( get_rid ( ) , PhysicsServer2D : : AREA_PARAM_PRIORITY , p_priority ) ;
2014-02-10 01:10:30 +00:00
}
2020-05-14 12:29:06 +00:00
2017-03-05 15:44:50 +00:00
real_t Area2D : : get_priority ( ) const {
2014-02-10 01:10:30 +00:00
return priority ;
}
2014-11-06 00:20:42 +00:00
void Area2D : : _body_enter_tree ( ObjectID p_id ) {
2014-02-10 01:10:30 +00:00
Object * obj = ObjectDB : : get_instance ( p_id ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! node ) ;
2017-03-05 15:44:50 +00:00
Map < ObjectID , BodyState > : : Element * E = body_map . find ( p_id ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! E ) ;
2014-11-06 00:20:42 +00:00
ERR_FAIL_COND ( E - > get ( ) . in_tree ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
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 ) ;
2014-02-10 01:10:30 +00:00
}
}
2014-11-06 00:20:42 +00:00
void Area2D : : _body_exit_tree ( ObjectID p_id ) {
2014-02-10 01:10:30 +00:00
Object * obj = ObjectDB : : get_instance ( p_id ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! node ) ;
2017-03-05 15:44:50 +00:00
Map < ObjectID , BodyState > : : Element * E = body_map . find ( p_id ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! E ) ;
2014-11-06 00:20:42 +00:00
ERR_FAIL_COND ( ! E - > get ( ) . in_tree ) ;
2017-03-05 15:44:50 +00:00
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 ) ;
2014-02-10 01:10:30 +00:00
}
}
2020-02-12 17:24:06 +00:00
void Area2D : : _body_inout ( int p_status , const RID & p_body , ObjectID p_instance , int p_body_shape , int p_area_shape ) {
2020-03-27 18:21:27 +00:00
bool body_in = p_status = = PhysicsServer2D : : AREA_BODY_ADDED ;
2017-03-05 15:44:50 +00:00
ObjectID objid = p_instance ;
2014-02-10 01:10:30 +00:00
Object * obj = ObjectDB : : get_instance ( objid ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < ObjectID , BodyState > : : Element * E = body_map . find ( objid ) ;
2014-02-10 01:10:30 +00:00
2018-11-28 21:40:58 +00:00
if ( ! body_in & & ! E ) {
return ; //does not exist because it was likely removed from the tree
}
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
locked = true ;
2014-12-07 05:04:20 +00:00
2014-02-10 01:10:30 +00:00
if ( body_in ) {
if ( ! E ) {
2017-03-05 15:44:50 +00:00
E = body_map . insert ( objid , BodyState ( ) ) ;
E - > get ( ) . rc = 0 ;
E - > get ( ) . in_tree = node & & node - > is_inside_tree ( ) ;
2014-02-10 01:10:30 +00:00
if ( node ) {
2020-02-21 22:26:13 +00:00
node - > connect ( SceneStringNames : : get_singleton ( ) - > tree_entered , callable_mp ( this , & Area2D : : _body_enter_tree ) , make_binds ( objid ) ) ;
node - > connect ( SceneStringNames : : get_singleton ( ) - > tree_exiting , callable_mp ( this , & Area2D : : _body_exit_tree ) , make_binds ( objid ) ) ;
2014-11-06 00:20:42 +00:00
if ( E - > get ( ) . in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > body_entered , node ) ;
2014-02-10 01:10:30 +00:00
}
}
}
E - > get ( ) . rc + + ;
2020-05-14 14:41:43 +00:00
if ( node ) {
2017-03-05 15:44:50 +00:00
E - > get ( ) . shapes . insert ( ShapePair ( p_body_shape , p_area_shape ) ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2014-11-06 14:02:40 +00:00
if ( ! node | | E - > get ( ) . in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > body_shape_entered , objid , node , p_body_shape , p_area_shape ) ;
2014-02-10 01:10:30 +00:00
}
} else {
E - > get ( ) . rc - - ;
2020-05-14 14:41:43 +00:00
if ( node ) {
2017-03-05 15:44:50 +00:00
E - > get ( ) . shapes . erase ( ShapePair ( p_body_shape , p_area_shape ) ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-10-02 15:41:55 +00:00
bool in_tree = E - > get ( ) . in_tree ;
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . rc = = 0 ) {
2020-10-02 15:41:55 +00:00
body_map . erase ( E ) ;
2014-02-10 01:10:30 +00:00
if ( node ) {
2020-02-21 22:26:13 +00:00
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 ) ) ;
2020-10-02 15:41:55 +00:00
if ( in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > body_exited , obj ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
}
}
2020-10-02 15:41:55 +00:00
if ( ! node | | in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > body_shape_exited , objid , obj , p_body_shape , p_area_shape ) ;
2014-02-10 01:10:30 +00:00
}
}
2017-03-05 15:44:50 +00:00
locked = false ;
2014-02-10 01:10:30 +00:00
}
2015-03-17 03:45:25 +00:00
void Area2D : : _area_enter_tree ( ObjectID p_id ) {
Object * obj = ObjectDB : : get_instance ( p_id ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2015-03-17 03:45:25 +00:00
ERR_FAIL_COND ( ! node ) ;
2017-03-05 15:44:50 +00:00
Map < ObjectID , AreaState > : : Element * E = area_map . find ( p_id ) ;
2015-03-17 03:45:25 +00:00
ERR_FAIL_COND ( ! E ) ;
ERR_FAIL_COND ( E - > get ( ) . in_tree ) ;
2017-03-05 15:44:50 +00:00
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 ) ;
2015-03-17 03:45:25 +00:00
}
}
void Area2D : : _area_exit_tree ( ObjectID p_id ) {
Object * obj = ObjectDB : : get_instance ( p_id ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2015-03-17 03:45:25 +00:00
ERR_FAIL_COND ( ! node ) ;
2017-03-05 15:44:50 +00:00
Map < ObjectID , AreaState > : : Element * E = area_map . find ( p_id ) ;
2015-03-17 03:45:25 +00:00
ERR_FAIL_COND ( ! E ) ;
ERR_FAIL_COND ( ! E - > get ( ) . in_tree ) ;
2017-03-05 15:44:50 +00:00
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 ) ;
2015-03-17 03:45:25 +00:00
}
}
2020-02-12 17:24:06 +00:00
void Area2D : : _area_inout ( int p_status , const RID & p_area , ObjectID p_instance , int p_area_shape , int p_self_shape ) {
2020-03-27 18:21:27 +00:00
bool area_in = p_status = = PhysicsServer2D : : AREA_BODY_ADDED ;
2017-03-05 15:44:50 +00:00
ObjectID objid = p_instance ;
2015-03-17 03:45:25 +00:00
Object * obj = ObjectDB : : get_instance ( objid ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2015-03-17 03:45:25 +00:00
2017-03-05 15:44:50 +00:00
Map < ObjectID , AreaState > : : Element * E = area_map . find ( objid ) ;
2015-03-17 03:45:25 +00:00
2019-04-15 02:31:51 +00:00
if ( ! area_in & & ! E ) {
return ; //likely removed from the tree
}
2017-03-05 15:44:50 +00:00
locked = true ;
2015-03-17 03:45:25 +00:00
if ( area_in ) {
if ( ! E ) {
2017-03-05 15:44:50 +00:00
E = area_map . insert ( objid , AreaState ( ) ) ;
E - > get ( ) . rc = 0 ;
E - > get ( ) . in_tree = node & & node - > is_inside_tree ( ) ;
2015-03-17 03:45:25 +00:00
if ( node ) {
2020-02-21 22:26:13 +00:00
node - > connect ( SceneStringNames : : get_singleton ( ) - > tree_entered , callable_mp ( this , & Area2D : : _area_enter_tree ) , make_binds ( objid ) ) ;
node - > connect ( SceneStringNames : : get_singleton ( ) - > tree_exiting , callable_mp ( this , & Area2D : : _area_exit_tree ) , make_binds ( objid ) ) ;
2015-03-17 03:45:25 +00:00
if ( E - > get ( ) . in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > area_entered , node ) ;
2015-03-17 03:45:25 +00:00
}
}
}
E - > get ( ) . rc + + ;
2020-05-14 14:41:43 +00:00
if ( node ) {
2017-03-05 15:44:50 +00:00
E - > get ( ) . shapes . insert ( AreaShapePair ( p_area_shape , p_self_shape ) ) ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 03:45:25 +00:00
if ( ! node | | E - > get ( ) . in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > area_shape_entered , objid , node , p_area_shape , p_self_shape ) ;
2015-03-17 03:45:25 +00:00
}
} else {
E - > get ( ) . rc - - ;
2020-05-14 14:41:43 +00:00
if ( node ) {
2017-03-05 15:44:50 +00:00
E - > get ( ) . shapes . erase ( AreaShapePair ( p_area_shape , p_self_shape ) ) ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 03:45:25 +00:00
2020-10-02 15:41:55 +00:00
bool in_tree = E - > get ( ) . in_tree ;
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . rc = = 0 ) {
2020-10-02 15:41:55 +00:00
area_map . erase ( E ) ;
2015-03-17 03:45:25 +00:00
if ( node ) {
2020-02-21 22:26:13 +00:00
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 ) ) ;
2020-10-02 15:41:55 +00:00
if ( in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > area_exited , obj ) ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 03:45:25 +00:00
}
}
2020-10-02 15:41:55 +00:00
if ( ! node | | in_tree ) {
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > area_shape_exited , objid , obj , p_area_shape , p_self_shape ) ;
2015-03-17 03:45:25 +00:00
}
}
2017-03-05 15:44:50 +00:00
locked = false ;
2015-03-17 03:45:25 +00:00
}
2014-02-10 01:10:30 +00:00
void Area2D : : _clear_monitoring ( ) {
2019-08-08 20:11:48 +00:00
ERR_FAIL_COND_MSG ( locked , " This function can't be used during the in/out signal. " ) ;
2014-02-10 01:10:30 +00:00
2015-03-17 03:45:25 +00:00
{
2017-03-05 15:44:50 +00:00
Map < ObjectID , BodyState > bmcopy = body_map ;
2015-03-17 03:45:25 +00:00
body_map . clear ( ) ;
//disconnect all monitored stuff
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
for ( Map < ObjectID , BodyState > : : Element * E = bmcopy . front ( ) ; E ; E = E - > next ( ) ) {
2015-03-17 03:45:25 +00:00
Object * obj = ObjectDB : : get_instance ( E - > key ( ) ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2017-07-10 14:20:56 +00:00
2020-05-14 14:41:43 +00:00
if ( ! node ) { //node may have been deleted in previous frame or at other legitimate point
2017-07-10 14:20:56 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2017-01-25 19:52:44 +00:00
2020-02-21 22:26:13 +00:00
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 ) ) ;
2017-01-25 19:52:44 +00:00
2020-05-14 14:41:43 +00:00
if ( ! E - > get ( ) . in_tree ) {
2015-03-17 03:45:25 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
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 ) ;
2015-03-17 03:45:25 +00:00
}
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > body_exited , obj ) ;
2014-02-10 01:10:30 +00:00
}
2015-03-17 03:45:25 +00:00
}
{
2017-03-05 15:44:50 +00:00
Map < ObjectID , AreaState > bmcopy = area_map ;
2015-03-17 03:45:25 +00:00
area_map . clear ( ) ;
//disconnect all monitored stuff
2017-03-05 15:44:50 +00:00
for ( Map < ObjectID , AreaState > : : Element * E = bmcopy . front ( ) ; E ; E = E - > next ( ) ) {
2015-03-17 03:45:25 +00:00
Object * obj = ObjectDB : : get_instance ( E - > key ( ) ) ;
2017-08-24 20:58:51 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
2015-12-31 14:15:49 +00:00
2020-05-14 14:41:43 +00:00
if ( ! node ) { //node may have been deleted in previous frame or at other legitimate point
2015-12-31 14:15:49 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2015-12-31 14:15:49 +00:00
2020-02-21 22:26:13 +00:00
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 ) ) ;
2017-01-25 19:52:44 +00:00
2020-05-14 14:41:43 +00:00
if ( ! E - > get ( ) . in_tree ) {
2015-03-17 03:45:25 +00:00
continue ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 03:45:25 +00:00
2017-03-05 15:44:50 +00:00
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 ) ;
2015-03-17 03:45:25 +00:00
}
2017-03-05 15:44:50 +00:00
emit_signal ( SceneStringNames : : get_singleton ( ) - > area_exited , obj ) ;
2015-03-17 03:45:25 +00:00
}
2014-02-10 01:10:30 +00:00
}
}
void Area2D : : _notification ( int p_what ) {
2017-03-05 15:44:50 +00:00
switch ( p_what ) {
2014-11-06 00:20:42 +00:00
case NOTIFICATION_EXIT_TREE : {
2014-02-10 01:10:30 +00:00
_clear_monitoring ( ) ;
} break ;
}
}
2017-01-13 23:03:51 +00:00
void Area2D : : set_monitoring ( bool p_enable ) {
2020-05-14 14:41:43 +00:00
if ( p_enable = = monitoring ) {
2014-02-10 01:10:30 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2019-08-08 20:11:48 +00:00
ERR_FAIL_COND_MSG ( locked , " Function blocked during in/out signal. Use set_deferred( \" monitoring \" , true/false). " ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
monitoring = p_enable ;
2014-02-10 01:10:30 +00:00
if ( monitoring ) {
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_monitor_callback ( get_rid ( ) , this , SceneStringNames : : get_singleton ( ) - > _body_inout ) ;
PhysicsServer2D : : get_singleton ( ) - > area_set_area_monitor_callback ( get_rid ( ) , this , SceneStringNames : : get_singleton ( ) - > _area_inout ) ;
2015-06-29 03:29:49 +00:00
2014-02-10 01:10:30 +00:00
} else {
2020-04-01 23:20:12 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_monitor_callback ( get_rid ( ) , nullptr , StringName ( ) ) ;
PhysicsServer2D : : get_singleton ( ) - > area_set_area_monitor_callback ( get_rid ( ) , nullptr , StringName ( ) ) ;
2014-02-10 01:10:30 +00:00
_clear_monitoring ( ) ;
}
}
2017-01-13 23:03:51 +00:00
bool Area2D : : is_monitoring ( ) const {
2014-02-10 01:10:30 +00:00
return monitoring ;
}
2015-03-17 03:45:25 +00:00
void Area2D : : set_monitorable ( bool p_enable ) {
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_MSG ( locked | | ( is_inside_tree ( ) & & PhysicsServer2D : : get_singleton ( ) - > is_flushing_queries ( ) ) , " Function blocked during in/out signal. Use set_deferred( \" monitorable \" , true/false). " ) ;
2015-03-17 03:45:25 +00:00
2020-05-14 14:41:43 +00:00
if ( p_enable = = monitorable ) {
2015-03-17 03:45:25 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 03:45:25 +00:00
2017-03-05 15:44:50 +00:00
monitorable = p_enable ;
2015-03-17 03:45:25 +00:00
2020-03-27 18:21:27 +00:00
PhysicsServer2D : : get_singleton ( ) - > area_set_monitorable ( get_rid ( ) , monitorable ) ;
2015-03-17 03:45:25 +00:00
}
bool Area2D : : is_monitorable ( ) const {
return monitorable ;
}
2020-04-21 15:16:45 +00:00
TypedArray < Node2D > Area2D : : get_overlapping_bodies ( ) const {
2019-09-25 08:28:50 +00:00
ERR_FAIL_COND_V_MSG ( ! monitoring , Array ( ) , " Can't find overlapping bodies when monitoring is off. " ) ;
2020-04-21 15:16:45 +00:00
TypedArray < Node2D > ret ;
2014-11-13 03:53:12 +00:00
ret . resize ( body_map . size ( ) ) ;
2017-03-05 15:44:50 +00:00
int idx = 0 ;
for ( const Map < ObjectID , BodyState > : : Element * E = body_map . front ( ) ; E ; E = E - > next ( ) ) {
2014-11-13 03:53:12 +00:00
Object * obj = ObjectDB : : get_instance ( E - > key ( ) ) ;
if ( ! obj ) {
2017-03-05 15:44:50 +00:00
ret . resize ( ret . size ( ) - 1 ) ; //ops
2014-11-13 03:53:12 +00:00
} else {
2017-03-05 15:44:50 +00:00
ret [ idx + + ] = obj ;
2014-11-13 03:53:12 +00:00
}
}
return ret ;
}
2020-04-21 15:16:45 +00:00
TypedArray < Area2D > Area2D : : get_overlapping_areas ( ) const {
2019-09-25 08:28:50 +00:00
ERR_FAIL_COND_V_MSG ( ! monitoring , Array ( ) , " Can't find overlapping bodies when monitoring is off. " ) ;
2020-04-21 15:16:45 +00:00
TypedArray < Area2D > ret ;
2015-03-17 03:45:25 +00:00
ret . resize ( area_map . size ( ) ) ;
2017-03-05 15:44:50 +00:00
int idx = 0 ;
for ( const Map < ObjectID , AreaState > : : Element * E = area_map . front ( ) ; E ; E = E - > next ( ) ) {
2015-03-17 03:45:25 +00:00
Object * obj = ObjectDB : : get_instance ( E - > key ( ) ) ;
if ( ! obj ) {
2017-03-05 15:44:50 +00:00
ret . resize ( ret . size ( ) - 1 ) ; //ops
2015-03-17 03:45:25 +00:00
} else {
2017-03-05 15:44:50 +00:00
ret [ idx + + ] = obj ;
2015-03-17 03:45:25 +00:00
}
}
return ret ;
}
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
bool Area2D : : overlaps_area ( Node * p_area ) const {
ERR_FAIL_NULL_V ( p_area , false ) ;
2017-08-07 10:17:31 +00:00
const Map < ObjectID , AreaState > : : Element * E = area_map . find ( p_area - > get_instance_id ( ) ) ;
2020-05-14 14:41:43 +00:00
if ( ! E ) {
2015-03-17 12:25:35 +00:00
return false ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 12:25:35 +00:00
return E - > get ( ) . in_tree ;
}
2017-03-05 15:44:50 +00:00
bool Area2D : : overlaps_body ( Node * p_body ) const {
ERR_FAIL_NULL_V ( p_body , false ) ;
2017-08-07 10:17:31 +00:00
const Map < ObjectID , BodyState > : : Element * E = body_map . find ( p_body - > get_instance_id ( ) ) ;
2020-05-14 14:41:43 +00:00
if ( ! E ) {
2015-03-17 12:25:35 +00:00
return false ;
2020-05-14 14:41:43 +00:00
}
2015-03-17 12:25:35 +00:00
return E - > get ( ) . in_tree ;
}
2017-06-19 00:07:32 +00:00
void Area2D : : set_audio_bus_override ( bool p_override ) {
audio_bus_override = p_override ;
}
bool Area2D : : is_overriding_audio_bus ( ) const {
return audio_bus_override ;
}
2017-09-22 16:29:33 +00:00
void Area2D : : set_audio_bus_name ( const StringName & p_audio_bus ) {
2017-06-19 00:07:32 +00:00
audio_bus = p_audio_bus ;
}
2017-09-22 16:29:33 +00:00
StringName Area2D : : get_audio_bus_name ( ) const {
2017-06-19 00:07:32 +00:00
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 ;
}
}
return " Master " ;
}
void Area2D : : _validate_property ( PropertyInfo & property ) const {
if ( property . name = = " audio_bus_name " ) {
String options ;
for ( int i = 0 ; i < AudioServer : : get_singleton ( ) - > get_bus_count ( ) ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i > 0 ) {
2017-06-19 00:07:32 +00:00
options + = " , " ;
2020-05-14 14:41:43 +00:00
}
2017-06-19 00:07:32 +00:00
String name = AudioServer : : get_singleton ( ) - > get_bus_name ( i ) ;
options + = name ;
}
property . hint_string = options ;
}
}
2014-02-10 01:10:30 +00:00
void Area2D : : _bind_methods ( ) {
2017-09-12 03:33:44 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_space_override_mode " , " space_override_mode " ) , & Area2D : : set_space_override_mode ) ;
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_space_override_mode " ) , & Area2D : : get_space_override_mode ) ;
2015-04-29 16:06:25 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_gravity_is_point " , " enable " ) , & Area2D : : set_gravity_is_point ) ;
ClassDB : : bind_method ( D_METHOD ( " is_gravity_a_point " ) , & Area2D : : is_gravity_a_point ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_gravity_distance_scale " , " distance_scale " ) , & Area2D : : set_gravity_distance_scale ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gravity_distance_scale " ) , & Area2D : : get_gravity_distance_scale ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_gravity_vector " , " vector " ) , & Area2D : : set_gravity_vector ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gravity_vector " ) , & Area2D : : get_gravity_vector ) ;
2015-01-05 21:37:12 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_gravity " , " gravity " ) , & Area2D : : set_gravity ) ;
ClassDB : : bind_method ( D_METHOD ( " get_gravity " ) , & Area2D : : get_gravity ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_linear_damp " , " linear_damp " ) , & Area2D : : set_linear_damp ) ;
ClassDB : : bind_method ( D_METHOD ( " get_linear_damp " ) , & Area2D : : get_linear_damp ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_angular_damp " , " angular_damp " ) , & Area2D : : set_angular_damp ) ;
ClassDB : : bind_method ( D_METHOD ( " get_angular_damp " ) , & Area2D : : get_angular_damp ) ;
2015-05-03 19:47:21 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_priority " , " priority " ) , & Area2D : : set_priority ) ;
ClassDB : : bind_method ( D_METHOD ( " get_priority " ) , & Area2D : : get_priority ) ;
2015-05-03 19:47:21 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_monitoring " , " enable " ) , & Area2D : : set_monitoring ) ;
ClassDB : : bind_method ( D_METHOD ( " is_monitoring " ) , & Area2D : : is_monitoring ) ;
2014-11-13 03:53:12 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_monitorable " , " enable " ) , & Area2D : : set_monitorable ) ;
ClassDB : : bind_method ( D_METHOD ( " is_monitorable " ) , & Area2D : : is_monitorable ) ;
2015-03-17 12:25:35 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_overlapping_bodies " ) , & Area2D : : get_overlapping_bodies ) ;
ClassDB : : bind_method ( D_METHOD ( " get_overlapping_areas " ) , & Area2D : : get_overlapping_areas ) ;
2015-03-17 03:45:25 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " overlaps_body " , " body " ) , & Area2D : : overlaps_body ) ;
ClassDB : : bind_method ( D_METHOD ( " overlaps_area " , " area " ) , & Area2D : : overlaps_area ) ;
2015-03-17 03:45:25 +00:00
2017-09-22 16:29:33 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_audio_bus_name " , " name " ) , & Area2D : : set_audio_bus_name ) ;
ClassDB : : bind_method ( D_METHOD ( " get_audio_bus_name " ) , & Area2D : : get_audio_bus_name ) ;
2017-06-19 00:07:32 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_audio_bus_override " , " enable " ) , & Area2D : : set_audio_bus_override ) ;
ClassDB : : bind_method ( D_METHOD ( " is_overriding_audio_bus " ) , & Area2D : : is_overriding_audio_bus ) ;
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " _body_inout " ) , & Area2D : : _body_inout ) ;
ClassDB : : bind_method ( D_METHOD ( " _area_inout " ) , & Area2D : : _area_inout ) ;
2014-02-10 01:10:30 +00:00
2021-02-24 04:51:20 +00:00
ADD_SIGNAL ( MethodInfo ( " body_shape_entered " , PropertyInfo ( Variant : : INT , " body_id " ) , PropertyInfo ( Variant : : OBJECT , " body " , PROPERTY_HINT_RESOURCE_TYPE , " Node2D " ) , PropertyInfo ( Variant : : INT , " body_shape " ) , PropertyInfo ( Variant : : INT , " local_shape " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " body_shape_exited " , PropertyInfo ( Variant : : INT , " body_id " ) , PropertyInfo ( Variant : : OBJECT , " body " , PROPERTY_HINT_RESOURCE_TYPE , " Node2D " ) , PropertyInfo ( Variant : : INT , " body_shape " ) , PropertyInfo ( Variant : : INT , " local_shape " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " body_entered " , PropertyInfo ( Variant : : OBJECT , " body " , PROPERTY_HINT_RESOURCE_TYPE , " Node2D " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " body_exited " , PropertyInfo ( Variant : : OBJECT , " body " , PROPERTY_HINT_RESOURCE_TYPE , " Node2D " ) ) ) ;
2014-02-10 01:10:30 +00:00
2020-10-11 11:50:50 +00:00
ADD_SIGNAL ( MethodInfo ( " area_shape_entered " , PropertyInfo ( Variant : : INT , " area_id " ) , PropertyInfo ( Variant : : OBJECT , " area " , PROPERTY_HINT_RESOURCE_TYPE , " Area2D " ) , PropertyInfo ( Variant : : INT , " area_shape " ) , PropertyInfo ( Variant : : INT , " local_shape " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " area_shape_exited " , PropertyInfo ( Variant : : INT , " area_id " ) , PropertyInfo ( Variant : : OBJECT , " area " , PROPERTY_HINT_RESOURCE_TYPE , " Area2D " ) , PropertyInfo ( Variant : : INT , " area_shape " ) , PropertyInfo ( Variant : : INT , " local_shape " ) ) ) ;
2017-03-05 15:44:50 +00:00
ADD_SIGNAL ( MethodInfo ( " area_entered " , PropertyInfo ( Variant : : OBJECT , " area " , PROPERTY_HINT_RESOURCE_TYPE , " Area2D " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " area_exited " , PropertyInfo ( Variant : : OBJECT , " area " , PROPERTY_HINT_RESOURCE_TYPE , " Area2D " ) ) ) ;
2014-02-10 01:10:30 +00:00
2021-04-18 00:18:17 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " monitoring " ) , " set_monitoring " , " is_monitoring " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " monitorable " ) , " set_monitorable " , " is_monitorable " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " priority " , PROPERTY_HINT_RANGE , " 0,128,1 " ) , " set_priority " , " get_priority " ) ;
ADD_GROUP ( " Physics Overrides " , " " ) ;
2018-11-08 14:30:02 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " space_override " , PROPERTY_HINT_ENUM , " Disabled,Combine,Combine-Replace,Replace,Replace-Combine " ) , " set_space_override_mode " , " get_space_override_mode " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " gravity_point " ) , " set_gravity_is_point " , " is_gravity_a_point " ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " gravity_distance_scale " , PROPERTY_HINT_EXP_RANGE , " 0,1024,0.001,or_greater " ) , " set_gravity_distance_scale " , " get_gravity_distance_scale " ) ;
2017-03-05 15:44:50 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : VECTOR2 , " gravity_vec " ) , " set_gravity_vector " , " get_gravity_vector " ) ;
2021-05-06 02:43:11 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " gravity " , PROPERTY_HINT_RANGE , " -4096,4096,0.001,or_lesser,or_greater " ) , " set_gravity " , " get_gravity " ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " linear_damp " , PROPERTY_HINT_RANGE , " 0,100,0.001,or_greater " ) , " set_linear_damp " , " get_linear_damp " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : FLOAT , " angular_damp " , PROPERTY_HINT_RANGE , " 0,100,0.001,or_greater " ) , " set_angular_damp " , " get_angular_damp " ) ;
2021-04-18 00:18:17 +00:00
2017-06-19 00:07:32 +00:00
ADD_GROUP ( " Audio Bus " , " audio_bus_ " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " audio_bus_override " ) , " set_audio_bus_override " , " is_overriding_audio_bus " ) ;
2020-02-20 21:58:05 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : STRING_NAME , " audio_bus_name " , PROPERTY_HINT_ENUM , " " ) , " set_audio_bus_name " , " get_audio_bus_name " ) ;
2017-09-12 03:33:44 +00:00
BIND_ENUM_CONSTANT ( SPACE_OVERRIDE_DISABLED ) ;
BIND_ENUM_CONSTANT ( SPACE_OVERRIDE_COMBINE ) ;
BIND_ENUM_CONSTANT ( SPACE_OVERRIDE_COMBINE_REPLACE ) ;
BIND_ENUM_CONSTANT ( SPACE_OVERRIDE_REPLACE ) ;
BIND_ENUM_CONSTANT ( SPACE_OVERRIDE_REPLACE_COMBINE ) ;
2014-02-10 01:10:30 +00:00
}
2017-12-06 20:36:34 +00:00
Area2D : : Area2D ( ) :
2020-03-27 18:21:27 +00:00
CollisionObject2D ( PhysicsServer2D : : get_singleton ( ) - > area_create ( ) , true ) {
2020-02-16 11:09:53 +00:00
set_gravity ( 980 ) ;
2017-03-05 15:44:50 +00:00
set_gravity_vector ( Vector2 ( 0 , 1 ) ) ;
2017-01-13 23:03:51 +00:00
set_monitoring ( true ) ;
2015-03-17 03:45:25 +00:00
set_monitorable ( true ) ;
2014-02-10 01:10:30 +00:00
}
Area2D : : ~ Area2D ( ) {
}