area_2d.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**************************************************************************/
  2. /* area_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "servers/audio_server.h"
  32. void Area2D::set_gravity_space_override_mode(SpaceOverride p_mode) {
  33. gravity_space_override = p_mode;
  34. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE, p_mode);
  35. }
  36. Area2D::SpaceOverride Area2D::get_gravity_space_override_mode() const {
  37. return gravity_space_override;
  38. }
  39. void Area2D::set_gravity_is_point(bool p_enabled) {
  40. gravity_is_point = p_enabled;
  41. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT, p_enabled);
  42. }
  43. bool Area2D::is_gravity_a_point() const {
  44. return gravity_is_point;
  45. }
  46. void Area2D::set_gravity_point_unit_distance(real_t p_scale) {
  47. gravity_point_unit_distance = p_scale;
  48. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE, p_scale);
  49. }
  50. real_t Area2D::get_gravity_point_unit_distance() const {
  51. return gravity_point_unit_distance;
  52. }
  53. void Area2D::set_gravity_point_center(const Vector2 &p_center) {
  54. gravity_vec = p_center;
  55. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, p_center);
  56. }
  57. const Vector2 &Area2D::get_gravity_point_center() const {
  58. return gravity_vec;
  59. }
  60. void Area2D::set_gravity_direction(const Vector2 &p_direction) {
  61. gravity_vec = p_direction;
  62. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, p_direction);
  63. }
  64. const Vector2 &Area2D::get_gravity_direction() const {
  65. return gravity_vec;
  66. }
  67. void Area2D::set_gravity(real_t p_gravity) {
  68. gravity = p_gravity;
  69. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_GRAVITY, p_gravity);
  70. }
  71. real_t Area2D::get_gravity() const {
  72. return gravity;
  73. }
  74. void Area2D::set_linear_damp_space_override_mode(SpaceOverride p_mode) {
  75. linear_damp_space_override = p_mode;
  76. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE, p_mode);
  77. }
  78. Area2D::SpaceOverride Area2D::get_linear_damp_space_override_mode() const {
  79. return linear_damp_space_override;
  80. }
  81. void Area2D::set_angular_damp_space_override_mode(SpaceOverride p_mode) {
  82. angular_damp_space_override = p_mode;
  83. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE, p_mode);
  84. }
  85. Area2D::SpaceOverride Area2D::get_angular_damp_space_override_mode() const {
  86. return angular_damp_space_override;
  87. }
  88. void Area2D::set_linear_damp(real_t p_linear_damp) {
  89. linear_damp = p_linear_damp;
  90. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_LINEAR_DAMP, p_linear_damp);
  91. }
  92. real_t Area2D::get_linear_damp() const {
  93. return linear_damp;
  94. }
  95. void Area2D::set_angular_damp(real_t p_angular_damp) {
  96. angular_damp = p_angular_damp;
  97. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP, p_angular_damp);
  98. }
  99. real_t Area2D::get_angular_damp() const {
  100. return angular_damp;
  101. }
  102. void Area2D::set_priority(int p_priority) {
  103. priority = p_priority;
  104. PhysicsServer2D::get_singleton()->area_set_param(get_rid(), PhysicsServer2D::AREA_PARAM_PRIORITY, p_priority);
  105. }
  106. int Area2D::get_priority() const {
  107. return priority;
  108. }
  109. void Area2D::_body_enter_tree(ObjectID p_id) {
  110. Object *obj = ObjectDB::get_instance(p_id);
  111. Node *node = Object::cast_to<Node>(obj);
  112. ERR_FAIL_NULL(node);
  113. HashMap<ObjectID, BodyState>::Iterator E = body_map.find(p_id);
  114. ERR_FAIL_COND(!E);
  115. ERR_FAIL_COND(E->value.in_tree);
  116. E->value.in_tree = true;
  117. emit_signal(SceneStringName(body_entered), node);
  118. for (int i = 0; i < E->value.shapes.size(); i++) {
  119. emit_signal(SceneStringName(body_shape_entered), E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].area_shape);
  120. }
  121. }
  122. void Area2D::_body_exit_tree(ObjectID p_id) {
  123. Object *obj = ObjectDB::get_instance(p_id);
  124. Node *node = Object::cast_to<Node>(obj);
  125. ERR_FAIL_NULL(node);
  126. HashMap<ObjectID, BodyState>::Iterator E = body_map.find(p_id);
  127. ERR_FAIL_COND(!E);
  128. ERR_FAIL_COND(!E->value.in_tree);
  129. E->value.in_tree = false;
  130. emit_signal(SceneStringName(body_exited), node);
  131. for (int i = 0; i < E->value.shapes.size(); i++) {
  132. emit_signal(SceneStringName(body_shape_exited), E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].area_shape);
  133. }
  134. }
  135. void Area2D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_area_shape) {
  136. bool body_in = p_status == PhysicsServer2D::AREA_BODY_ADDED;
  137. ObjectID objid = p_instance;
  138. // Exit early if instance is invalid.
  139. if (objid.is_null()) {
  140. // Emit the appropriate signals.
  141. lock_callback();
  142. locked = true;
  143. if (body_in) {
  144. emit_signal(SceneStringName(body_shape_entered), p_body, (Node *)nullptr, p_body_shape, p_area_shape);
  145. } else {
  146. emit_signal(SceneStringName(body_shape_exited), p_body, (Node *)nullptr, p_body_shape, p_area_shape);
  147. }
  148. locked = false;
  149. unlock_callback();
  150. return;
  151. }
  152. Object *obj = ObjectDB::get_instance(objid);
  153. Node *node = Object::cast_to<Node>(obj);
  154. HashMap<ObjectID, BodyState>::Iterator E = body_map.find(objid);
  155. if (!body_in && !E) {
  156. return; //does not exist because it was likely removed from the tree
  157. }
  158. lock_callback();
  159. locked = true;
  160. if (body_in) {
  161. if (!E) {
  162. E = body_map.insert(objid, BodyState());
  163. E->value.rid = p_body;
  164. E->value.rc = 0;
  165. E->value.in_tree = node && node->is_inside_tree();
  166. if (node) {
  167. node->connect(SceneStringName(tree_entered), callable_mp(this, &Area2D::_body_enter_tree).bind(objid));
  168. node->connect(SceneStringName(tree_exiting), callable_mp(this, &Area2D::_body_exit_tree).bind(objid));
  169. if (E->value.in_tree) {
  170. emit_signal(SceneStringName(body_entered), node);
  171. }
  172. }
  173. }
  174. E->value.rc++;
  175. if (node) {
  176. E->value.shapes.insert(ShapePair(p_body_shape, p_area_shape));
  177. }
  178. if (!node || E->value.in_tree) {
  179. emit_signal(SceneStringName(body_shape_entered), p_body, node, p_body_shape, p_area_shape);
  180. }
  181. } else {
  182. E->value.rc--;
  183. if (node) {
  184. E->value.shapes.erase(ShapePair(p_body_shape, p_area_shape));
  185. }
  186. bool in_tree = E->value.in_tree;
  187. if (E->value.rc == 0) {
  188. body_map.remove(E);
  189. if (node) {
  190. node->disconnect(SceneStringName(tree_entered), callable_mp(this, &Area2D::_body_enter_tree));
  191. node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Area2D::_body_exit_tree));
  192. if (in_tree) {
  193. emit_signal(SceneStringName(body_exited), obj);
  194. }
  195. }
  196. }
  197. if (!node || in_tree) {
  198. emit_signal(SceneStringName(body_shape_exited), p_body, obj, p_body_shape, p_area_shape);
  199. }
  200. }
  201. locked = false;
  202. unlock_callback();
  203. }
  204. void Area2D::_area_enter_tree(ObjectID p_id) {
  205. Object *obj = ObjectDB::get_instance(p_id);
  206. Node *node = Object::cast_to<Node>(obj);
  207. ERR_FAIL_NULL(node);
  208. HashMap<ObjectID, AreaState>::Iterator E = area_map.find(p_id);
  209. ERR_FAIL_COND(!E);
  210. ERR_FAIL_COND(E->value.in_tree);
  211. E->value.in_tree = true;
  212. emit_signal(SceneStringName(area_entered), node);
  213. for (int i = 0; i < E->value.shapes.size(); i++) {
  214. emit_signal(SceneStringName(area_shape_entered), E->value.rid, node, E->value.shapes[i].area_shape, E->value.shapes[i].self_shape);
  215. }
  216. }
  217. void Area2D::_area_exit_tree(ObjectID p_id) {
  218. Object *obj = ObjectDB::get_instance(p_id);
  219. Node *node = Object::cast_to<Node>(obj);
  220. ERR_FAIL_NULL(node);
  221. HashMap<ObjectID, AreaState>::Iterator E = area_map.find(p_id);
  222. ERR_FAIL_COND(!E);
  223. ERR_FAIL_COND(!E->value.in_tree);
  224. E->value.in_tree = false;
  225. emit_signal(SceneStringName(area_exited), node);
  226. for (int i = 0; i < E->value.shapes.size(); i++) {
  227. emit_signal(SceneStringName(area_shape_exited), E->value.rid, node, E->value.shapes[i].area_shape, E->value.shapes[i].self_shape);
  228. }
  229. }
  230. void Area2D::_area_inout(int p_status, const RID &p_area, ObjectID p_instance, int p_area_shape, int p_self_shape) {
  231. bool area_in = p_status == PhysicsServer2D::AREA_BODY_ADDED;
  232. ObjectID objid = p_instance;
  233. // Exit early if instance is invalid.
  234. if (objid.is_null()) {
  235. // Emit the appropriate signals.
  236. lock_callback();
  237. locked = true;
  238. if (area_in) {
  239. emit_signal(SceneStringName(area_shape_entered), p_area, (Node *)nullptr, p_area_shape, p_self_shape);
  240. } else {
  241. emit_signal(SceneStringName(area_shape_exited), p_area, (Node *)nullptr, p_area_shape, p_self_shape);
  242. }
  243. locked = false;
  244. unlock_callback();
  245. return;
  246. }
  247. Object *obj = ObjectDB::get_instance(objid);
  248. Node *node = Object::cast_to<Node>(obj);
  249. HashMap<ObjectID, AreaState>::Iterator E = area_map.find(objid);
  250. if (!area_in && !E) {
  251. return; //likely removed from the tree
  252. }
  253. lock_callback();
  254. locked = true;
  255. if (area_in) {
  256. if (!E) {
  257. E = area_map.insert(objid, AreaState());
  258. E->value.rid = p_area;
  259. E->value.rc = 0;
  260. E->value.in_tree = node && node->is_inside_tree();
  261. if (node) {
  262. node->connect(SceneStringName(tree_entered), callable_mp(this, &Area2D::_area_enter_tree).bind(objid));
  263. node->connect(SceneStringName(tree_exiting), callable_mp(this, &Area2D::_area_exit_tree).bind(objid));
  264. if (E->value.in_tree) {
  265. emit_signal(SceneStringName(area_entered), node);
  266. }
  267. }
  268. }
  269. E->value.rc++;
  270. if (node) {
  271. E->value.shapes.insert(AreaShapePair(p_area_shape, p_self_shape));
  272. }
  273. if (!node || E->value.in_tree) {
  274. emit_signal(SceneStringName(area_shape_entered), p_area, node, p_area_shape, p_self_shape);
  275. }
  276. } else {
  277. E->value.rc--;
  278. if (node) {
  279. E->value.shapes.erase(AreaShapePair(p_area_shape, p_self_shape));
  280. }
  281. bool in_tree = E->value.in_tree;
  282. if (E->value.rc == 0) {
  283. area_map.remove(E);
  284. if (node) {
  285. node->disconnect(SceneStringName(tree_entered), callable_mp(this, &Area2D::_area_enter_tree));
  286. node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Area2D::_area_exit_tree));
  287. if (in_tree) {
  288. emit_signal(SceneStringName(area_exited), obj);
  289. }
  290. }
  291. }
  292. if (!node || in_tree) {
  293. emit_signal(SceneStringName(area_shape_exited), p_area, obj, p_area_shape, p_self_shape);
  294. }
  295. }
  296. locked = false;
  297. unlock_callback();
  298. }
  299. void Area2D::_clear_monitoring() {
  300. ERR_FAIL_COND_MSG(locked, "This function can't be used during the in/out signal.");
  301. {
  302. HashMap<ObjectID, BodyState> bmcopy = body_map;
  303. body_map.clear();
  304. //disconnect all monitored stuff
  305. for (const KeyValue<ObjectID, BodyState> &E : bmcopy) {
  306. Object *obj = ObjectDB::get_instance(E.key);
  307. Node *node = Object::cast_to<Node>(obj);
  308. if (!node) { //node may have been deleted in previous frame or at other legitimate point
  309. continue;
  310. }
  311. node->disconnect(SceneStringName(tree_entered), callable_mp(this, &Area2D::_body_enter_tree));
  312. node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Area2D::_body_exit_tree));
  313. if (!E.value.in_tree) {
  314. continue;
  315. }
  316. for (int i = 0; i < E.value.shapes.size(); i++) {
  317. emit_signal(SceneStringName(body_shape_exited), E.value.rid, node, E.value.shapes[i].body_shape, E.value.shapes[i].area_shape);
  318. }
  319. emit_signal(SceneStringName(body_exited), obj);
  320. }
  321. }
  322. {
  323. HashMap<ObjectID, AreaState> bmcopy = area_map;
  324. area_map.clear();
  325. //disconnect all monitored stuff
  326. for (const KeyValue<ObjectID, AreaState> &E : bmcopy) {
  327. Object *obj = ObjectDB::get_instance(E.key);
  328. Node *node = Object::cast_to<Node>(obj);
  329. if (!node) { //node may have been deleted in previous frame or at other legitimate point
  330. continue;
  331. }
  332. node->disconnect(SceneStringName(tree_entered), callable_mp(this, &Area2D::_area_enter_tree));
  333. node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Area2D::_area_exit_tree));
  334. if (!E.value.in_tree) {
  335. continue;
  336. }
  337. for (int i = 0; i < E.value.shapes.size(); i++) {
  338. emit_signal(SceneStringName(area_shape_exited), E.value.rid, node, E.value.shapes[i].area_shape, E.value.shapes[i].self_shape);
  339. }
  340. emit_signal(SceneStringName(area_exited), obj);
  341. }
  342. }
  343. }
  344. void Area2D::_space_changed(const RID &p_new_space) {
  345. if (p_new_space.is_null()) {
  346. _clear_monitoring();
  347. }
  348. }
  349. void Area2D::set_monitoring(bool p_enable) {
  350. if (p_enable == monitoring) {
  351. return;
  352. }
  353. ERR_FAIL_COND_MSG(locked, "Function blocked during in/out signal. Use set_deferred(\"monitoring\", true/false).");
  354. monitoring = p_enable;
  355. if (monitoring) {
  356. PhysicsServer2D::get_singleton()->area_set_monitor_callback(get_rid(), callable_mp(this, &Area2D::_body_inout));
  357. PhysicsServer2D::get_singleton()->area_set_area_monitor_callback(get_rid(), callable_mp(this, &Area2D::_area_inout));
  358. } else {
  359. PhysicsServer2D::get_singleton()->area_set_monitor_callback(get_rid(), Callable());
  360. PhysicsServer2D::get_singleton()->area_set_area_monitor_callback(get_rid(), Callable());
  361. _clear_monitoring();
  362. }
  363. }
  364. bool Area2D::is_monitoring() const {
  365. return monitoring;
  366. }
  367. void Area2D::set_monitorable(bool p_enable) {
  368. 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).");
  369. if (p_enable == monitorable) {
  370. return;
  371. }
  372. monitorable = p_enable;
  373. PhysicsServer2D::get_singleton()->area_set_monitorable(get_rid(), monitorable);
  374. }
  375. bool Area2D::is_monitorable() const {
  376. return monitorable;
  377. }
  378. TypedArray<Node2D> Area2D::get_overlapping_bodies() const {
  379. TypedArray<Node2D> ret;
  380. ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping bodies when monitoring is off.");
  381. ret.resize(body_map.size());
  382. int idx = 0;
  383. for (const KeyValue<ObjectID, BodyState> &E : body_map) {
  384. Object *obj = ObjectDB::get_instance(E.key);
  385. if (obj) {
  386. ret[idx] = obj;
  387. idx++;
  388. }
  389. }
  390. ret.resize(idx);
  391. return ret;
  392. }
  393. TypedArray<Area2D> Area2D::get_overlapping_areas() const {
  394. TypedArray<Area2D> ret;
  395. ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping areas when monitoring is off.");
  396. ret.resize(area_map.size());
  397. int idx = 0;
  398. for (const KeyValue<ObjectID, AreaState> &E : area_map) {
  399. Object *obj = ObjectDB::get_instance(E.key);
  400. if (obj) {
  401. ret[idx] = obj;
  402. idx++;
  403. }
  404. }
  405. ret.resize(idx);
  406. return ret;
  407. }
  408. bool Area2D::has_overlapping_bodies() const {
  409. ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping bodies when monitoring is off.");
  410. return !body_map.is_empty();
  411. }
  412. bool Area2D::has_overlapping_areas() const {
  413. ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping areas when monitoring is off.");
  414. return !area_map.is_empty();
  415. }
  416. bool Area2D::overlaps_area(Node *p_area) const {
  417. ERR_FAIL_NULL_V(p_area, false);
  418. HashMap<ObjectID, AreaState>::ConstIterator E = area_map.find(p_area->get_instance_id());
  419. if (!E) {
  420. return false;
  421. }
  422. return E->value.in_tree;
  423. }
  424. bool Area2D::overlaps_body(Node *p_body) const {
  425. ERR_FAIL_NULL_V(p_body, false);
  426. HashMap<ObjectID, BodyState>::ConstIterator E = body_map.find(p_body->get_instance_id());
  427. if (!E) {
  428. return false;
  429. }
  430. return E->value.in_tree;
  431. }
  432. void Area2D::set_audio_bus_override(bool p_override) {
  433. audio_bus_override = p_override;
  434. }
  435. bool Area2D::is_overriding_audio_bus() const {
  436. return audio_bus_override;
  437. }
  438. void Area2D::set_audio_bus_name(const StringName &p_audio_bus) {
  439. audio_bus = p_audio_bus;
  440. }
  441. StringName Area2D::get_audio_bus_name() const {
  442. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  443. if (AudioServer::get_singleton()->get_bus_name(i) == audio_bus) {
  444. return audio_bus;
  445. }
  446. }
  447. return SceneStringName(Master);
  448. }
  449. void Area2D::_validate_property(PropertyInfo &p_property) const {
  450. if (p_property.name == "audio_bus_name") {
  451. String options;
  452. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  453. if (i > 0) {
  454. options += ",";
  455. }
  456. String name = AudioServer::get_singleton()->get_bus_name(i);
  457. options += name;
  458. }
  459. p_property.hint_string = options;
  460. } else if (p_property.name.begins_with("gravity") && p_property.name != "gravity_space_override") {
  461. if (gravity_space_override == SPACE_OVERRIDE_DISABLED) {
  462. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  463. } else {
  464. if (gravity_is_point) {
  465. if (p_property.name == "gravity_direction") {
  466. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  467. }
  468. } else {
  469. if (p_property.name.begins_with("gravity_point_")) {
  470. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  471. }
  472. }
  473. }
  474. } else if (p_property.name.begins_with("linear_damp") && p_property.name != "linear_damp_space_override") {
  475. if (linear_damp_space_override == SPACE_OVERRIDE_DISABLED) {
  476. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  477. }
  478. } else if (p_property.name.begins_with("angular_damp") && p_property.name != "angular_damp_space_override") {
  479. if (angular_damp_space_override == SPACE_OVERRIDE_DISABLED) {
  480. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  481. }
  482. }
  483. }
  484. void Area2D::_bind_methods() {
  485. ClassDB::bind_method(D_METHOD("set_gravity_space_override_mode", "space_override_mode"), &Area2D::set_gravity_space_override_mode);
  486. ClassDB::bind_method(D_METHOD("get_gravity_space_override_mode"), &Area2D::get_gravity_space_override_mode);
  487. ClassDB::bind_method(D_METHOD("set_gravity_is_point", "enable"), &Area2D::set_gravity_is_point);
  488. ClassDB::bind_method(D_METHOD("is_gravity_a_point"), &Area2D::is_gravity_a_point);
  489. ClassDB::bind_method(D_METHOD("set_gravity_point_unit_distance", "distance_scale"), &Area2D::set_gravity_point_unit_distance);
  490. ClassDB::bind_method(D_METHOD("get_gravity_point_unit_distance"), &Area2D::get_gravity_point_unit_distance);
  491. ClassDB::bind_method(D_METHOD("set_gravity_point_center", "center"), &Area2D::set_gravity_point_center);
  492. ClassDB::bind_method(D_METHOD("get_gravity_point_center"), &Area2D::get_gravity_point_center);
  493. ClassDB::bind_method(D_METHOD("set_gravity_direction", "direction"), &Area2D::set_gravity_direction);
  494. ClassDB::bind_method(D_METHOD("get_gravity_direction"), &Area2D::get_gravity_direction);
  495. ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &Area2D::set_gravity);
  496. ClassDB::bind_method(D_METHOD("get_gravity"), &Area2D::get_gravity);
  497. ClassDB::bind_method(D_METHOD("set_linear_damp_space_override_mode", "space_override_mode"), &Area2D::set_linear_damp_space_override_mode);
  498. ClassDB::bind_method(D_METHOD("get_linear_damp_space_override_mode"), &Area2D::get_linear_damp_space_override_mode);
  499. ClassDB::bind_method(D_METHOD("set_angular_damp_space_override_mode", "space_override_mode"), &Area2D::set_angular_damp_space_override_mode);
  500. ClassDB::bind_method(D_METHOD("get_angular_damp_space_override_mode"), &Area2D::get_angular_damp_space_override_mode);
  501. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &Area2D::set_linear_damp);
  502. ClassDB::bind_method(D_METHOD("get_linear_damp"), &Area2D::get_linear_damp);
  503. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &Area2D::set_angular_damp);
  504. ClassDB::bind_method(D_METHOD("get_angular_damp"), &Area2D::get_angular_damp);
  505. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &Area2D::set_priority);
  506. ClassDB::bind_method(D_METHOD("get_priority"), &Area2D::get_priority);
  507. ClassDB::bind_method(D_METHOD("set_monitoring", "enable"), &Area2D::set_monitoring);
  508. ClassDB::bind_method(D_METHOD("is_monitoring"), &Area2D::is_monitoring);
  509. ClassDB::bind_method(D_METHOD("set_monitorable", "enable"), &Area2D::set_monitorable);
  510. ClassDB::bind_method(D_METHOD("is_monitorable"), &Area2D::is_monitorable);
  511. ClassDB::bind_method(D_METHOD("get_overlapping_bodies"), &Area2D::get_overlapping_bodies);
  512. ClassDB::bind_method(D_METHOD("get_overlapping_areas"), &Area2D::get_overlapping_areas);
  513. ClassDB::bind_method(D_METHOD("has_overlapping_bodies"), &Area2D::has_overlapping_bodies);
  514. ClassDB::bind_method(D_METHOD("has_overlapping_areas"), &Area2D::has_overlapping_areas);
  515. ClassDB::bind_method(D_METHOD("overlaps_body", "body"), &Area2D::overlaps_body);
  516. ClassDB::bind_method(D_METHOD("overlaps_area", "area"), &Area2D::overlaps_area);
  517. ClassDB::bind_method(D_METHOD("set_audio_bus_name", "name"), &Area2D::set_audio_bus_name);
  518. ClassDB::bind_method(D_METHOD("get_audio_bus_name"), &Area2D::get_audio_bus_name);
  519. ClassDB::bind_method(D_METHOD("set_audio_bus_override", "enable"), &Area2D::set_audio_bus_override);
  520. ClassDB::bind_method(D_METHOD("is_overriding_audio_bus"), &Area2D::is_overriding_audio_bus);
  521. ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node2D"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  522. ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node2D"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  523. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node2D")));
  524. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node2D")));
  525. ADD_SIGNAL(MethodInfo("area_shape_entered", PropertyInfo(Variant::RID, "area_rid"), PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area2D"), PropertyInfo(Variant::INT, "area_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  526. ADD_SIGNAL(MethodInfo("area_shape_exited", PropertyInfo(Variant::RID, "area_rid"), PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area2D"), PropertyInfo(Variant::INT, "area_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  527. ADD_SIGNAL(MethodInfo("area_entered", PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area2D")));
  528. ADD_SIGNAL(MethodInfo("area_exited", PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area2D")));
  529. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitoring"), "set_monitoring", "is_monitoring");
  530. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitorable"), "set_monitorable", "is_monitorable");
  531. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,100000,1,or_greater,or_less"), "set_priority", "get_priority");
  532. ADD_GROUP("Gravity", "gravity_");
  533. ADD_PROPERTY(PropertyInfo(Variant::INT, "gravity_space_override", PROPERTY_HINT_ENUM, "Disabled,Combine,Combine-Replace,Replace,Replace-Combine", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_gravity_space_override_mode", "get_gravity_space_override_mode");
  534. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gravity_point", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_gravity_is_point", "is_gravity_a_point");
  535. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_point_unit_distance", PROPERTY_HINT_RANGE, "0,1024,0.001,or_greater,exp,suffix:px"), "set_gravity_point_unit_distance", "get_gravity_point_unit_distance");
  536. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity_point_center", PROPERTY_HINT_NONE, "suffix:px"), "set_gravity_point_center", "get_gravity_point_center");
  537. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity_direction"), "set_gravity_direction", "get_gravity_direction");
  538. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity", PROPERTY_HINT_RANGE, U"-4096,4096,0.001,or_less,or_greater,suffix:px/s\u00B2"), "set_gravity", "get_gravity");
  539. ADD_GROUP("Linear Damp", "linear_damp_");
  540. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_space_override", PROPERTY_HINT_ENUM, "Disabled,Combine,Combine-Replace,Replace,Replace-Combine", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_linear_damp_space_override_mode", "get_linear_damp_space_override_mode");
  541. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  542. ADD_GROUP("Angular Damp", "angular_damp_");
  543. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_space_override", PROPERTY_HINT_ENUM, "Disabled,Combine,Combine-Replace,Replace,Replace-Combine", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_angular_damp_space_override_mode", "get_angular_damp_space_override_mode");
  544. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  545. ADD_GROUP("Audio Bus", "audio_bus_");
  546. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_bus_override"), "set_audio_bus_override", "is_overriding_audio_bus");
  547. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "audio_bus_name", PROPERTY_HINT_ENUM, ""), "set_audio_bus_name", "get_audio_bus_name");
  548. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_DISABLED);
  549. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_COMBINE);
  550. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_COMBINE_REPLACE);
  551. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_REPLACE);
  552. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_REPLACE_COMBINE);
  553. }
  554. Area2D::Area2D() :
  555. CollisionObject2D(PhysicsServer2D::get_singleton()->area_create(), true) {
  556. set_gravity(980);
  557. set_gravity_direction(Vector2(0, 1));
  558. set_monitoring(true);
  559. set_monitorable(true);
  560. set_hide_clip_children(true);
  561. }
  562. Area2D::~Area2D() {
  563. }