area_2d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*************************************************************************/
  2. /* area_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "area_2d.h"
  31. #include "scene/scene_string_names.h"
  32. #include "servers/audio_server.h"
  33. #include "servers/physics_2d_server.h"
  34. void Area2D::set_space_override_mode(SpaceOverride p_mode) {
  35. space_override = p_mode;
  36. Physics2DServer::get_singleton()->area_set_space_override_mode(get_rid(), Physics2DServer::AreaSpaceOverrideMode(p_mode));
  37. }
  38. Area2D::SpaceOverride Area2D::get_space_override_mode() const {
  39. return space_override;
  40. }
  41. void Area2D::set_gravity_is_point(bool p_enabled) {
  42. gravity_is_point = p_enabled;
  43. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_GRAVITY_IS_POINT, p_enabled);
  44. }
  45. bool Area2D::is_gravity_a_point() const {
  46. return gravity_is_point;
  47. }
  48. void Area2D::set_gravity_distance_scale(real_t p_scale) {
  49. gravity_distance_scale = p_scale;
  50. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE, p_scale);
  51. }
  52. real_t Area2D::get_gravity_distance_scale() const {
  53. return gravity_distance_scale;
  54. }
  55. void Area2D::set_gravity_vector(const Vector2 &p_vec) {
  56. gravity_vec = p_vec;
  57. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_GRAVITY_VECTOR, p_vec);
  58. }
  59. Vector2 Area2D::get_gravity_vector() const {
  60. return gravity_vec;
  61. }
  62. void Area2D::set_gravity(real_t p_gravity) {
  63. gravity = p_gravity;
  64. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_GRAVITY, p_gravity);
  65. }
  66. real_t Area2D::get_gravity() const {
  67. return gravity;
  68. }
  69. void Area2D::set_linear_damp(real_t p_linear_damp) {
  70. linear_damp = p_linear_damp;
  71. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_LINEAR_DAMP, p_linear_damp);
  72. }
  73. real_t Area2D::get_linear_damp() const {
  74. return linear_damp;
  75. }
  76. void Area2D::set_angular_damp(real_t p_angular_damp) {
  77. angular_damp = p_angular_damp;
  78. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_ANGULAR_DAMP, p_angular_damp);
  79. }
  80. real_t Area2D::get_angular_damp() const {
  81. return angular_damp;
  82. }
  83. void Area2D::set_priority(real_t p_priority) {
  84. priority = p_priority;
  85. Physics2DServer::get_singleton()->area_set_param(get_rid(), Physics2DServer::AREA_PARAM_PRIORITY, p_priority);
  86. }
  87. real_t Area2D::get_priority() const {
  88. return priority;
  89. }
  90. void Area2D::_body_enter_tree(ObjectID p_id) {
  91. Object *obj = ObjectDB::get_instance(p_id);
  92. Node *node = Object::cast_to<Node>(obj);
  93. ERR_FAIL_COND(!node);
  94. Map<ObjectID, BodyState>::Element *E = body_map.find(p_id);
  95. ERR_FAIL_COND(!E);
  96. ERR_FAIL_COND(E->get().in_tree);
  97. E->get().in_tree = true;
  98. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  99. for (int i = 0; i < E->get().shapes.size(); i++) {
  100. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].area_shape);
  101. }
  102. }
  103. void Area2D::_body_exit_tree(ObjectID p_id) {
  104. Object *obj = ObjectDB::get_instance(p_id);
  105. Node *node = Object::cast_to<Node>(obj);
  106. ERR_FAIL_COND(!node);
  107. Map<ObjectID, BodyState>::Element *E = body_map.find(p_id);
  108. ERR_FAIL_COND(!E);
  109. ERR_FAIL_COND(!E->get().in_tree);
  110. E->get().in_tree = false;
  111. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  112. for (int i = 0; i < E->get().shapes.size(); i++) {
  113. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].area_shape);
  114. }
  115. }
  116. void Area2D::_body_inout(int p_status, const RID &p_body, int p_instance, int p_body_shape, int p_area_shape) {
  117. bool body_in = p_status == Physics2DServer::AREA_BODY_ADDED;
  118. ObjectID objid = p_instance;
  119. Object *obj = ObjectDB::get_instance(objid);
  120. Node *node = Object::cast_to<Node>(obj);
  121. Map<ObjectID, BodyState>::Element *E = body_map.find(objid);
  122. if (!body_in && !E) {
  123. return; //does not exist because it was likely removed from the tree
  124. }
  125. locked = true;
  126. if (body_in) {
  127. if (!E) {
  128. E = body_map.insert(objid, BodyState());
  129. E->get().rc = 0;
  130. E->get().in_tree = node && node->is_inside_tree();
  131. if (node) {
  132. node->connect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree, make_binds(objid));
  133. node->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree, make_binds(objid));
  134. if (E->get().in_tree) {
  135. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  136. }
  137. }
  138. }
  139. E->get().rc++;
  140. if (node)
  141. E->get().shapes.insert(ShapePair(p_body_shape, p_area_shape));
  142. if (!node || E->get().in_tree) {
  143. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, objid, node, p_body_shape, p_area_shape);
  144. }
  145. } else {
  146. E->get().rc--;
  147. if (node)
  148. E->get().shapes.erase(ShapePair(p_body_shape, p_area_shape));
  149. bool eraseit = false;
  150. if (E->get().rc == 0) {
  151. if (node) {
  152. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  153. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  154. if (E->get().in_tree)
  155. emit_signal(SceneStringNames::get_singleton()->body_exited, obj);
  156. }
  157. eraseit = true;
  158. }
  159. if (!node || E->get().in_tree) {
  160. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, objid, obj, p_body_shape, p_area_shape);
  161. }
  162. if (eraseit)
  163. body_map.erase(E);
  164. }
  165. locked = false;
  166. }
  167. void Area2D::_area_enter_tree(ObjectID p_id) {
  168. Object *obj = ObjectDB::get_instance(p_id);
  169. Node *node = Object::cast_to<Node>(obj);
  170. ERR_FAIL_COND(!node);
  171. Map<ObjectID, AreaState>::Element *E = area_map.find(p_id);
  172. ERR_FAIL_COND(!E);
  173. ERR_FAIL_COND(E->get().in_tree);
  174. E->get().in_tree = true;
  175. emit_signal(SceneStringNames::get_singleton()->area_entered, node);
  176. for (int i = 0; i < E->get().shapes.size(); i++) {
  177. emit_signal(SceneStringNames::get_singleton()->area_shape_entered, p_id, node, E->get().shapes[i].area_shape, E->get().shapes[i].self_shape);
  178. }
  179. }
  180. void Area2D::_area_exit_tree(ObjectID p_id) {
  181. Object *obj = ObjectDB::get_instance(p_id);
  182. Node *node = Object::cast_to<Node>(obj);
  183. ERR_FAIL_COND(!node);
  184. Map<ObjectID, AreaState>::Element *E = area_map.find(p_id);
  185. ERR_FAIL_COND(!E);
  186. ERR_FAIL_COND(!E->get().in_tree);
  187. E->get().in_tree = false;
  188. emit_signal(SceneStringNames::get_singleton()->area_exited, node);
  189. for (int i = 0; i < E->get().shapes.size(); i++) {
  190. emit_signal(SceneStringNames::get_singleton()->area_shape_exited, p_id, node, E->get().shapes[i].area_shape, E->get().shapes[i].self_shape);
  191. }
  192. }
  193. void Area2D::_area_inout(int p_status, const RID &p_area, int p_instance, int p_area_shape, int p_self_shape) {
  194. bool area_in = p_status == Physics2DServer::AREA_BODY_ADDED;
  195. ObjectID objid = p_instance;
  196. Object *obj = ObjectDB::get_instance(objid);
  197. Node *node = Object::cast_to<Node>(obj);
  198. Map<ObjectID, AreaState>::Element *E = area_map.find(objid);
  199. ERR_FAIL_COND(!area_in && !E);
  200. locked = true;
  201. if (area_in) {
  202. if (!E) {
  203. E = area_map.insert(objid, AreaState());
  204. E->get().rc = 0;
  205. E->get().in_tree = node && node->is_inside_tree();
  206. if (node) {
  207. node->connect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_area_enter_tree, make_binds(objid));
  208. node->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_area_exit_tree, make_binds(objid));
  209. if (E->get().in_tree) {
  210. emit_signal(SceneStringNames::get_singleton()->area_entered, node);
  211. }
  212. }
  213. }
  214. E->get().rc++;
  215. if (node)
  216. E->get().shapes.insert(AreaShapePair(p_area_shape, p_self_shape));
  217. if (!node || E->get().in_tree) {
  218. emit_signal(SceneStringNames::get_singleton()->area_shape_entered, objid, node, p_area_shape, p_self_shape);
  219. }
  220. } else {
  221. E->get().rc--;
  222. if (node)
  223. E->get().shapes.erase(AreaShapePair(p_area_shape, p_self_shape));
  224. bool eraseit = false;
  225. if (E->get().rc == 0) {
  226. if (node) {
  227. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_area_enter_tree);
  228. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_area_exit_tree);
  229. if (E->get().in_tree)
  230. emit_signal(SceneStringNames::get_singleton()->area_exited, obj);
  231. }
  232. eraseit = true;
  233. }
  234. if (!node || E->get().in_tree) {
  235. emit_signal(SceneStringNames::get_singleton()->area_shape_exited, objid, obj, p_area_shape, p_self_shape);
  236. }
  237. if (eraseit)
  238. area_map.erase(E);
  239. }
  240. locked = false;
  241. }
  242. void Area2D::_clear_monitoring() {
  243. if (locked) {
  244. ERR_EXPLAIN("This function can't be used during the in/out signal.");
  245. }
  246. ERR_FAIL_COND(locked);
  247. {
  248. Map<ObjectID, BodyState> bmcopy = body_map;
  249. body_map.clear();
  250. //disconnect all monitored stuff
  251. for (Map<ObjectID, BodyState>::Element *E = bmcopy.front(); E; E = E->next()) {
  252. Object *obj = ObjectDB::get_instance(E->key());
  253. Node *node = Object::cast_to<Node>(obj);
  254. if (!node) //node may have been deleted in previous frame or at other legiminate point
  255. continue;
  256. //ERR_CONTINUE(!node);
  257. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
  258. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
  259. if (!E->get().in_tree)
  260. continue;
  261. for (int i = 0; i < E->get().shapes.size(); i++) {
  262. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E->key(), node, E->get().shapes[i].body_shape, E->get().shapes[i].area_shape);
  263. }
  264. emit_signal(SceneStringNames::get_singleton()->body_exited, obj);
  265. }
  266. }
  267. {
  268. Map<ObjectID, AreaState> bmcopy = area_map;
  269. area_map.clear();
  270. //disconnect all monitored stuff
  271. for (Map<ObjectID, AreaState>::Element *E = bmcopy.front(); E; E = E->next()) {
  272. Object *obj = ObjectDB::get_instance(E->key());
  273. Node *node = Object::cast_to<Node>(obj);
  274. if (!node) //node may have been deleted in previous frame or at other legiminate point
  275. continue;
  276. //ERR_CONTINUE(!node);
  277. node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_area_enter_tree);
  278. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_area_exit_tree);
  279. if (!E->get().in_tree)
  280. continue;
  281. for (int i = 0; i < E->get().shapes.size(); i++) {
  282. emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E->key(), node, E->get().shapes[i].area_shape, E->get().shapes[i].self_shape);
  283. }
  284. emit_signal(SceneStringNames::get_singleton()->area_exited, obj);
  285. }
  286. }
  287. }
  288. void Area2D::_notification(int p_what) {
  289. switch (p_what) {
  290. case NOTIFICATION_EXIT_TREE: {
  291. _clear_monitoring();
  292. } break;
  293. }
  294. }
  295. void Area2D::set_monitoring(bool p_enable) {
  296. if (p_enable == monitoring)
  297. return;
  298. if (locked) {
  299. ERR_EXPLAIN("Function blocked during in/out signal. Use set_deferred(\"monitoring\",true/false)");
  300. }
  301. ERR_FAIL_COND(locked);
  302. monitoring = p_enable;
  303. if (monitoring) {
  304. Physics2DServer::get_singleton()->area_set_monitor_callback(get_rid(), this, SceneStringNames::get_singleton()->_body_inout);
  305. Physics2DServer::get_singleton()->area_set_area_monitor_callback(get_rid(), this, SceneStringNames::get_singleton()->_area_inout);
  306. } else {
  307. Physics2DServer::get_singleton()->area_set_monitor_callback(get_rid(), NULL, StringName());
  308. Physics2DServer::get_singleton()->area_set_area_monitor_callback(get_rid(), NULL, StringName());
  309. _clear_monitoring();
  310. }
  311. }
  312. bool Area2D::is_monitoring() const {
  313. return monitoring;
  314. }
  315. void Area2D::set_monitorable(bool p_enable) {
  316. if (locked || Physics2DServer::get_singleton()->is_flushing_queries()) {
  317. ERR_EXPLAIN("Function blocked during in/out signal. Use set_deferred(\"monitorable\",true/false)");
  318. }
  319. ERR_FAIL_COND(locked || Physics2DServer::get_singleton()->is_flushing_queries());
  320. if (p_enable == monitorable)
  321. return;
  322. monitorable = p_enable;
  323. Physics2DServer::get_singleton()->area_set_monitorable(get_rid(), monitorable);
  324. }
  325. bool Area2D::is_monitorable() const {
  326. return monitorable;
  327. }
  328. Array Area2D::get_overlapping_bodies() const {
  329. ERR_FAIL_COND_V(!monitoring, Array());
  330. Array ret;
  331. ret.resize(body_map.size());
  332. int idx = 0;
  333. for (const Map<ObjectID, BodyState>::Element *E = body_map.front(); E; E = E->next()) {
  334. Object *obj = ObjectDB::get_instance(E->key());
  335. if (!obj) {
  336. ret.resize(ret.size() - 1); //ops
  337. } else {
  338. ret[idx++] = obj;
  339. }
  340. }
  341. return ret;
  342. }
  343. Array Area2D::get_overlapping_areas() const {
  344. ERR_FAIL_COND_V(!monitoring, Array());
  345. Array ret;
  346. ret.resize(area_map.size());
  347. int idx = 0;
  348. for (const Map<ObjectID, AreaState>::Element *E = area_map.front(); E; E = E->next()) {
  349. Object *obj = ObjectDB::get_instance(E->key());
  350. if (!obj) {
  351. ret.resize(ret.size() - 1); //ops
  352. } else {
  353. ret[idx++] = obj;
  354. }
  355. }
  356. return ret;
  357. }
  358. bool Area2D::overlaps_area(Node *p_area) const {
  359. ERR_FAIL_NULL_V(p_area, false);
  360. const Map<ObjectID, AreaState>::Element *E = area_map.find(p_area->get_instance_id());
  361. if (!E)
  362. return false;
  363. return E->get().in_tree;
  364. }
  365. bool Area2D::overlaps_body(Node *p_body) const {
  366. ERR_FAIL_NULL_V(p_body, false);
  367. const Map<ObjectID, BodyState>::Element *E = body_map.find(p_body->get_instance_id());
  368. if (!E)
  369. return false;
  370. return E->get().in_tree;
  371. }
  372. void Area2D::set_collision_mask(uint32_t p_mask) {
  373. collision_mask = p_mask;
  374. Physics2DServer::get_singleton()->area_set_collision_mask(get_rid(), p_mask);
  375. }
  376. uint32_t Area2D::get_collision_mask() const {
  377. return collision_mask;
  378. }
  379. void Area2D::set_collision_layer(uint32_t p_layer) {
  380. collision_layer = p_layer;
  381. Physics2DServer::get_singleton()->area_set_collision_layer(get_rid(), p_layer);
  382. }
  383. uint32_t Area2D::get_collision_layer() const {
  384. return collision_layer;
  385. }
  386. void Area2D::set_collision_mask_bit(int p_bit, bool p_value) {
  387. uint32_t mask = get_collision_mask();
  388. if (p_value)
  389. mask |= 1 << p_bit;
  390. else
  391. mask &= ~(1 << p_bit);
  392. set_collision_mask(mask);
  393. }
  394. bool Area2D::get_collision_mask_bit(int p_bit) const {
  395. return get_collision_mask() & (1 << p_bit);
  396. }
  397. void Area2D::set_collision_layer_bit(int p_bit, bool p_value) {
  398. uint32_t layer = get_collision_layer();
  399. if (p_value)
  400. layer |= 1 << p_bit;
  401. else
  402. layer &= ~(1 << p_bit);
  403. set_collision_layer(layer);
  404. }
  405. bool Area2D::get_collision_layer_bit(int p_bit) const {
  406. return get_collision_layer() & (1 << p_bit);
  407. }
  408. void Area2D::set_audio_bus_override(bool p_override) {
  409. audio_bus_override = p_override;
  410. }
  411. bool Area2D::is_overriding_audio_bus() const {
  412. return audio_bus_override;
  413. }
  414. void Area2D::set_audio_bus_name(const StringName &p_audio_bus) {
  415. audio_bus = p_audio_bus;
  416. }
  417. StringName Area2D::get_audio_bus_name() const {
  418. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  419. if (AudioServer::get_singleton()->get_bus_name(i) == audio_bus) {
  420. return audio_bus;
  421. }
  422. }
  423. return "Master";
  424. }
  425. void Area2D::_validate_property(PropertyInfo &property) const {
  426. if (property.name == "audio_bus_name") {
  427. String options;
  428. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  429. if (i > 0)
  430. options += ",";
  431. String name = AudioServer::get_singleton()->get_bus_name(i);
  432. options += name;
  433. }
  434. property.hint_string = options;
  435. }
  436. }
  437. void Area2D::_bind_methods() {
  438. ClassDB::bind_method(D_METHOD("_body_enter_tree", "id"), &Area2D::_body_enter_tree);
  439. ClassDB::bind_method(D_METHOD("_body_exit_tree", "id"), &Area2D::_body_exit_tree);
  440. ClassDB::bind_method(D_METHOD("_area_enter_tree", "id"), &Area2D::_area_enter_tree);
  441. ClassDB::bind_method(D_METHOD("_area_exit_tree", "id"), &Area2D::_area_exit_tree);
  442. ClassDB::bind_method(D_METHOD("set_space_override_mode", "space_override_mode"), &Area2D::set_space_override_mode);
  443. ClassDB::bind_method(D_METHOD("get_space_override_mode"), &Area2D::get_space_override_mode);
  444. ClassDB::bind_method(D_METHOD("set_gravity_is_point", "enable"), &Area2D::set_gravity_is_point);
  445. ClassDB::bind_method(D_METHOD("is_gravity_a_point"), &Area2D::is_gravity_a_point);
  446. ClassDB::bind_method(D_METHOD("set_gravity_distance_scale", "distance_scale"), &Area2D::set_gravity_distance_scale);
  447. ClassDB::bind_method(D_METHOD("get_gravity_distance_scale"), &Area2D::get_gravity_distance_scale);
  448. ClassDB::bind_method(D_METHOD("set_gravity_vector", "vector"), &Area2D::set_gravity_vector);
  449. ClassDB::bind_method(D_METHOD("get_gravity_vector"), &Area2D::get_gravity_vector);
  450. ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &Area2D::set_gravity);
  451. ClassDB::bind_method(D_METHOD("get_gravity"), &Area2D::get_gravity);
  452. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &Area2D::set_linear_damp);
  453. ClassDB::bind_method(D_METHOD("get_linear_damp"), &Area2D::get_linear_damp);
  454. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &Area2D::set_angular_damp);
  455. ClassDB::bind_method(D_METHOD("get_angular_damp"), &Area2D::get_angular_damp);
  456. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &Area2D::set_priority);
  457. ClassDB::bind_method(D_METHOD("get_priority"), &Area2D::get_priority);
  458. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &Area2D::set_collision_mask);
  459. ClassDB::bind_method(D_METHOD("get_collision_mask"), &Area2D::get_collision_mask);
  460. ClassDB::bind_method(D_METHOD("set_collision_layer", "collision_layer"), &Area2D::set_collision_layer);
  461. ClassDB::bind_method(D_METHOD("get_collision_layer"), &Area2D::get_collision_layer);
  462. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &Area2D::set_collision_mask_bit);
  463. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &Area2D::get_collision_mask_bit);
  464. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &Area2D::set_collision_layer_bit);
  465. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &Area2D::get_collision_layer_bit);
  466. ClassDB::bind_method(D_METHOD("set_monitoring", "enable"), &Area2D::set_monitoring);
  467. ClassDB::bind_method(D_METHOD("is_monitoring"), &Area2D::is_monitoring);
  468. ClassDB::bind_method(D_METHOD("set_monitorable", "enable"), &Area2D::set_monitorable);
  469. ClassDB::bind_method(D_METHOD("is_monitorable"), &Area2D::is_monitorable);
  470. ClassDB::bind_method(D_METHOD("get_overlapping_bodies"), &Area2D::get_overlapping_bodies);
  471. ClassDB::bind_method(D_METHOD("get_overlapping_areas"), &Area2D::get_overlapping_areas);
  472. ClassDB::bind_method(D_METHOD("overlaps_body", "body"), &Area2D::overlaps_body);
  473. ClassDB::bind_method(D_METHOD("overlaps_area", "area"), &Area2D::overlaps_area);
  474. ClassDB::bind_method(D_METHOD("set_audio_bus_name", "name"), &Area2D::set_audio_bus_name);
  475. ClassDB::bind_method(D_METHOD("get_audio_bus_name"), &Area2D::get_audio_bus_name);
  476. ClassDB::bind_method(D_METHOD("set_audio_bus_override", "enable"), &Area2D::set_audio_bus_override);
  477. ClassDB::bind_method(D_METHOD("is_overriding_audio_bus"), &Area2D::is_overriding_audio_bus);
  478. ClassDB::bind_method(D_METHOD("_body_inout"), &Area2D::_body_inout);
  479. ClassDB::bind_method(D_METHOD("_area_inout"), &Area2D::_area_inout);
  480. ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::INT, "body_id"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsBody2D"), PropertyInfo(Variant::INT, "body_shape"), PropertyInfo(Variant::INT, "area_shape")));
  481. ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::INT, "body_id"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsBody2D"), PropertyInfo(Variant::INT, "body_shape"), PropertyInfo(Variant::INT, "area_shape")));
  482. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsBody2D")));
  483. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsBody2D")));
  484. 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, "self_shape")));
  485. 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, "self_shape")));
  486. ADD_SIGNAL(MethodInfo("area_entered", PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area2D")));
  487. ADD_SIGNAL(MethodInfo("area_exited", PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area2D")));
  488. 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");
  489. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gravity_point"), "set_gravity_is_point", "is_gravity_a_point");
  490. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_distance_scale", PROPERTY_HINT_EXP_RANGE, "0,1024,0.001,or_greater"), "set_gravity_distance_scale", "get_gravity_distance_scale");
  491. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity_vec"), "set_gravity_vector", "get_gravity_vector");
  492. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity", PROPERTY_HINT_RANGE, "-1024,1024,0.001"), "set_gravity", "get_gravity");
  493. ADD_PROPERTY(PropertyInfo(Variant::REAL, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.01,or_greater"), "set_linear_damp", "get_linear_damp");
  494. ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.01,or_greater"), "set_angular_damp", "get_angular_damp");
  495. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,128,1"), "set_priority", "get_priority");
  496. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitoring"), "set_monitoring", "is_monitoring");
  497. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitorable"), "set_monitorable", "is_monitorable");
  498. ADD_GROUP("Collision", "collision_");
  499. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
  500. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
  501. ADD_GROUP("Audio Bus", "audio_bus_");
  502. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_bus_override"), "set_audio_bus_override", "is_overriding_audio_bus");
  503. ADD_PROPERTY(PropertyInfo(Variant::STRING, "audio_bus_name", PROPERTY_HINT_ENUM, ""), "set_audio_bus_name", "get_audio_bus_name");
  504. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_DISABLED);
  505. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_COMBINE);
  506. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_COMBINE_REPLACE);
  507. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_REPLACE);
  508. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_REPLACE_COMBINE);
  509. }
  510. Area2D::Area2D() :
  511. CollisionObject2D(Physics2DServer::get_singleton()->area_create(), true) {
  512. space_override = SPACE_OVERRIDE_DISABLED;
  513. set_gravity(98);
  514. set_gravity_vector(Vector2(0, 1));
  515. gravity_is_point = false;
  516. gravity_distance_scale = 0;
  517. linear_damp = 0.1;
  518. angular_damp = 1;
  519. locked = false;
  520. priority = 0;
  521. monitoring = false;
  522. monitorable = false;
  523. collision_mask = 1;
  524. collision_layer = 1;
  525. audio_bus_override = false;
  526. set_monitoring(true);
  527. set_monitorable(true);
  528. }
  529. Area2D::~Area2D() {
  530. }