area_2d_sw.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*************************************************************************/
  2. /* area_2d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_sw.h"
  31. #include "body_2d_sw.h"
  32. #include "space_2d_sw.h"
  33. Area2DSW::BodyKey::BodyKey(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  34. rid = p_body->get_self();
  35. instance_id = p_body->get_instance_id();
  36. body_shape = p_body_shape;
  37. area_shape = p_area_shape;
  38. }
  39. Area2DSW::BodyKey::BodyKey(Area2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  40. rid = p_body->get_self();
  41. instance_id = p_body->get_instance_id();
  42. body_shape = p_body_shape;
  43. area_shape = p_area_shape;
  44. }
  45. void Area2DSW::_shapes_changed() {
  46. if (!moved_list.in_list() && get_space())
  47. get_space()->area_add_to_moved_list(&moved_list);
  48. }
  49. void Area2DSW::set_transform(const Transform2D &p_transform) {
  50. if (!moved_list.in_list() && get_space())
  51. get_space()->area_add_to_moved_list(&moved_list);
  52. _set_transform(p_transform);
  53. _set_inv_transform(p_transform.affine_inverse());
  54. }
  55. void Area2DSW::set_space(Space2DSW *p_space) {
  56. if (get_space()) {
  57. if (monitor_query_list.in_list())
  58. get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
  59. if (moved_list.in_list())
  60. get_space()->area_remove_from_moved_list(&moved_list);
  61. }
  62. monitored_bodies.clear();
  63. monitored_areas.clear();
  64. _set_space(p_space);
  65. }
  66. void Area2DSW::set_monitor_callback(ObjectID p_id, const StringName &p_method) {
  67. if (p_id == monitor_callback_id) {
  68. monitor_callback_method = p_method;
  69. return;
  70. }
  71. _unregister_shapes();
  72. monitor_callback_id = p_id;
  73. monitor_callback_method = p_method;
  74. monitored_bodies.clear();
  75. monitored_areas.clear();
  76. _shape_changed();
  77. if (!moved_list.in_list() && get_space())
  78. get_space()->area_add_to_moved_list(&moved_list);
  79. }
  80. void Area2DSW::set_area_monitor_callback(ObjectID p_id, const StringName &p_method) {
  81. if (p_id == area_monitor_callback_id) {
  82. area_monitor_callback_method = p_method;
  83. return;
  84. }
  85. _unregister_shapes();
  86. area_monitor_callback_id = p_id;
  87. area_monitor_callback_method = p_method;
  88. monitored_bodies.clear();
  89. monitored_areas.clear();
  90. _shape_changed();
  91. if (!moved_list.in_list() && get_space())
  92. get_space()->area_add_to_moved_list(&moved_list);
  93. }
  94. void Area2DSW::set_space_override_mode(Physics2DServer::AreaSpaceOverrideMode p_mode) {
  95. bool do_override = p_mode != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED;
  96. if (do_override == (space_override_mode != Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED))
  97. return;
  98. _unregister_shapes();
  99. space_override_mode = p_mode;
  100. _shape_changed();
  101. }
  102. void Area2DSW::set_param(Physics2DServer::AreaParameter p_param, const Variant &p_value) {
  103. switch (p_param) {
  104. case Physics2DServer::AREA_PARAM_GRAVITY: gravity = p_value; break;
  105. case Physics2DServer::AREA_PARAM_GRAVITY_VECTOR: gravity_vector = p_value; break;
  106. case Physics2DServer::AREA_PARAM_GRAVITY_IS_POINT: gravity_is_point = p_value; break;
  107. case Physics2DServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE: gravity_distance_scale = p_value; break;
  108. case Physics2DServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION: point_attenuation = p_value; break;
  109. case Physics2DServer::AREA_PARAM_LINEAR_DAMP: linear_damp = p_value; break;
  110. case Physics2DServer::AREA_PARAM_ANGULAR_DAMP: angular_damp = p_value; break;
  111. case Physics2DServer::AREA_PARAM_PRIORITY: priority = p_value; break;
  112. }
  113. }
  114. Variant Area2DSW::get_param(Physics2DServer::AreaParameter p_param) const {
  115. switch (p_param) {
  116. case Physics2DServer::AREA_PARAM_GRAVITY: return gravity;
  117. case Physics2DServer::AREA_PARAM_GRAVITY_VECTOR: return gravity_vector;
  118. case Physics2DServer::AREA_PARAM_GRAVITY_IS_POINT: return gravity_is_point;
  119. case Physics2DServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE: return gravity_distance_scale;
  120. case Physics2DServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION: return point_attenuation;
  121. case Physics2DServer::AREA_PARAM_LINEAR_DAMP: return linear_damp;
  122. case Physics2DServer::AREA_PARAM_ANGULAR_DAMP: return angular_damp;
  123. case Physics2DServer::AREA_PARAM_PRIORITY: return priority;
  124. }
  125. return Variant();
  126. }
  127. void Area2DSW::_queue_monitor_update() {
  128. ERR_FAIL_COND(!get_space());
  129. if (!monitor_query_list.in_list())
  130. get_space()->area_add_to_monitor_query_list(&monitor_query_list);
  131. }
  132. void Area2DSW::set_monitorable(bool p_monitorable) {
  133. if (monitorable == p_monitorable)
  134. return;
  135. monitorable = p_monitorable;
  136. _set_static(!monitorable);
  137. }
  138. void Area2DSW::call_queries() {
  139. if (monitor_callback_id && !monitored_bodies.empty()) {
  140. Variant res[5];
  141. Variant *resptr[5];
  142. for (int i = 0; i < 5; i++)
  143. resptr[i] = &res[i];
  144. Object *obj = ObjectDB::get_instance(monitor_callback_id);
  145. if (!obj) {
  146. monitored_bodies.clear();
  147. monitor_callback_id = 0;
  148. return;
  149. }
  150. for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E;) {
  151. if (E->get().state == 0) { // Nothing happened
  152. E = E->next();
  153. continue;
  154. }
  155. res[0] = E->get().state > 0 ? Physics2DServer::AREA_BODY_ADDED : Physics2DServer::AREA_BODY_REMOVED;
  156. res[1] = E->key().rid;
  157. res[2] = E->key().instance_id;
  158. res[3] = E->key().body_shape;
  159. res[4] = E->key().area_shape;
  160. Map<BodyKey, BodyState>::Element *next = E->next();
  161. monitored_bodies.erase(E);
  162. E = next;
  163. Variant::CallError ce;
  164. obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
  165. }
  166. }
  167. if (area_monitor_callback_id && !monitored_areas.empty()) {
  168. Variant res[5];
  169. Variant *resptr[5];
  170. for (int i = 0; i < 5; i++)
  171. resptr[i] = &res[i];
  172. Object *obj = ObjectDB::get_instance(area_monitor_callback_id);
  173. if (!obj) {
  174. monitored_areas.clear();
  175. area_monitor_callback_id = 0;
  176. return;
  177. }
  178. for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E;) {
  179. if (E->get().state == 0) { // Nothing happened
  180. E = E->next();
  181. continue;
  182. }
  183. res[0] = E->get().state > 0 ? Physics2DServer::AREA_BODY_ADDED : Physics2DServer::AREA_BODY_REMOVED;
  184. res[1] = E->key().rid;
  185. res[2] = E->key().instance_id;
  186. res[3] = E->key().body_shape;
  187. res[4] = E->key().area_shape;
  188. Map<BodyKey, BodyState>::Element *next = E->next();
  189. monitored_areas.erase(E);
  190. E = next;
  191. Variant::CallError ce;
  192. obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
  193. }
  194. }
  195. }
  196. Area2DSW::Area2DSW() :
  197. CollisionObject2DSW(TYPE_AREA),
  198. monitor_query_list(this),
  199. moved_list(this) {
  200. _set_static(true); //areas are not active by default
  201. space_override_mode = Physics2DServer::AREA_SPACE_OVERRIDE_DISABLED;
  202. gravity = 9.80665;
  203. gravity_vector = Vector2(0, -1);
  204. gravity_is_point = false;
  205. gravity_distance_scale = 0;
  206. point_attenuation = 1;
  207. angular_damp = 1.0;
  208. linear_damp = 0.1;
  209. priority = 0;
  210. monitor_callback_id = 0;
  211. area_monitor_callback_id = 0;
  212. monitorable = false;
  213. }
  214. Area2DSW::~Area2DSW() {
  215. }