area_2d_sw.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**************************************************************************/
  2. /* area_2d_sw.h */
  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. #ifndef AREA_2D_SW_H
  31. #define AREA_2D_SW_H
  32. #include "collision_object_2d_sw.h"
  33. #include "core/self_list.h"
  34. #include "servers/physics_2d_server.h"
  35. //#include "servers/physics/query_sw.h"
  36. class Space2DSW;
  37. class Body2DSW;
  38. class Constraint2DSW;
  39. class Area2DSW : public CollisionObject2DSW {
  40. Physics2DServer::AreaSpaceOverrideMode space_override_mode;
  41. real_t gravity;
  42. Vector2 gravity_vector;
  43. bool gravity_is_point;
  44. real_t gravity_distance_scale;
  45. real_t point_attenuation;
  46. real_t linear_damp;
  47. real_t angular_damp;
  48. int priority;
  49. bool monitorable;
  50. ObjectID monitor_callback_id;
  51. StringName monitor_callback_method;
  52. ObjectID area_monitor_callback_id;
  53. StringName area_monitor_callback_method;
  54. SelfList<Area2DSW> monitor_query_list;
  55. SelfList<Area2DSW> moved_list;
  56. struct BodyKey {
  57. RID rid;
  58. ObjectID instance_id;
  59. uint32_t body_shape;
  60. uint32_t area_shape;
  61. _FORCE_INLINE_ bool operator<(const BodyKey &p_key) const {
  62. if (rid == p_key.rid) {
  63. if (body_shape == p_key.body_shape) {
  64. return area_shape < p_key.area_shape;
  65. } else {
  66. return body_shape < p_key.body_shape;
  67. }
  68. } else {
  69. return rid < p_key.rid;
  70. }
  71. }
  72. _FORCE_INLINE_ BodyKey() {}
  73. BodyKey(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  74. BodyKey(Area2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  75. };
  76. struct BodyState {
  77. int state;
  78. _FORCE_INLINE_ void inc() { state++; }
  79. _FORCE_INLINE_ void dec() { state--; }
  80. _FORCE_INLINE_ BodyState() { state = 0; }
  81. };
  82. Map<BodyKey, BodyState> monitored_bodies;
  83. Map<BodyKey, BodyState> monitored_areas;
  84. //virtual void shape_changed_notify(Shape2DSW *p_shape);
  85. //virtual void shape_deleted_notify(Shape2DSW *p_shape);
  86. Set<Constraint2DSW *> constraints;
  87. virtual void _shapes_changed();
  88. void _queue_monitor_update();
  89. public:
  90. //_FORCE_INLINE_ const Matrix32& get_inverse_transform() const { return inverse_transform; }
  91. //_FORCE_INLINE_ SpaceSW* get_owner() { return owner; }
  92. void set_monitor_callback(ObjectID p_id, const StringName &p_method);
  93. _FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback_id; }
  94. void set_area_monitor_callback(ObjectID p_id, const StringName &p_method);
  95. _FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback_id; }
  96. _FORCE_INLINE_ void add_body_to_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  97. _FORCE_INLINE_ void remove_body_from_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  98. _FORCE_INLINE_ void add_area_to_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  99. _FORCE_INLINE_ void remove_area_from_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  100. void set_param(Physics2DServer::AreaParameter p_param, const Variant &p_value);
  101. Variant get_param(Physics2DServer::AreaParameter p_param) const;
  102. void set_space_override_mode(Physics2DServer::AreaSpaceOverrideMode p_mode);
  103. Physics2DServer::AreaSpaceOverrideMode get_space_override_mode() const { return space_override_mode; }
  104. _FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }
  105. _FORCE_INLINE_ real_t get_gravity() const { return gravity; }
  106. _FORCE_INLINE_ void set_gravity_vector(const Vector2 &p_gravity) { gravity_vector = p_gravity; }
  107. _FORCE_INLINE_ Vector2 get_gravity_vector() const { return gravity_vector; }
  108. _FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }
  109. _FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }
  110. _FORCE_INLINE_ void set_gravity_distance_scale(real_t scale) { gravity_distance_scale = scale; }
  111. _FORCE_INLINE_ real_t get_gravity_distance_scale() const { return gravity_distance_scale; }
  112. _FORCE_INLINE_ void set_point_attenuation(real_t p_point_attenuation) { point_attenuation = p_point_attenuation; }
  113. _FORCE_INLINE_ real_t get_point_attenuation() const { return point_attenuation; }
  114. _FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }
  115. _FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
  116. _FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }
  117. _FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
  118. _FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }
  119. _FORCE_INLINE_ int get_priority() const { return priority; }
  120. _FORCE_INLINE_ void add_constraint(Constraint2DSW *p_constraint) { constraints.insert(p_constraint); }
  121. _FORCE_INLINE_ void remove_constraint(Constraint2DSW *p_constraint) { constraints.erase(p_constraint); }
  122. _FORCE_INLINE_ const Set<Constraint2DSW *> &get_constraints() const { return constraints; }
  123. _FORCE_INLINE_ void clear_constraints() { constraints.clear(); }
  124. void set_monitorable(bool p_monitorable);
  125. _FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
  126. void set_transform(const Transform2D &p_transform);
  127. void set_space(Space2DSW *p_space);
  128. void call_queries();
  129. Area2DSW();
  130. ~Area2DSW();
  131. };
  132. void Area2DSW::add_body_to_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  133. BodyKey bk(p_body, p_body_shape, p_area_shape);
  134. monitored_bodies[bk].inc();
  135. if (!monitor_query_list.in_list()) {
  136. _queue_monitor_update();
  137. }
  138. }
  139. void Area2DSW::remove_body_from_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  140. BodyKey bk(p_body, p_body_shape, p_area_shape);
  141. monitored_bodies[bk].dec();
  142. if (!monitor_query_list.in_list()) {
  143. _queue_monitor_update();
  144. }
  145. }
  146. void Area2DSW::add_area_to_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  147. BodyKey bk(p_area, p_area_shape, p_self_shape);
  148. monitored_areas[bk].inc();
  149. if (!monitor_query_list.in_list()) {
  150. _queue_monitor_update();
  151. }
  152. }
  153. void Area2DSW::remove_area_from_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  154. BodyKey bk(p_area, p_area_shape, p_self_shape);
  155. monitored_areas[bk].dec();
  156. if (!monitor_query_list.in_list()) {
  157. _queue_monitor_update();
  158. }
  159. }
  160. #endif // AREA_2D_SW_H