area_2d_sw.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*************************************************************************/
  2. /* area_2d_sw.h */
  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. #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. } else
  68. return rid < p_key.rid;
  69. }
  70. _FORCE_INLINE_ BodyKey() {}
  71. BodyKey(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  72. BodyKey(Area2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  73. };
  74. struct BodyState {
  75. int state;
  76. _FORCE_INLINE_ void inc() { state++; }
  77. _FORCE_INLINE_ void dec() { state--; }
  78. _FORCE_INLINE_ BodyState() { state = 0; }
  79. };
  80. Map<BodyKey, BodyState> monitored_bodies;
  81. Map<BodyKey, BodyState> monitored_areas;
  82. //virtual void shape_changed_notify(Shape2DSW *p_shape);
  83. //virtual void shape_deleted_notify(Shape2DSW *p_shape);
  84. Set<Constraint2DSW *> constraints;
  85. virtual void _shapes_changed();
  86. void _queue_monitor_update();
  87. public:
  88. //_FORCE_INLINE_ const Matrix32& get_inverse_transform() const { return inverse_transform; }
  89. //_FORCE_INLINE_ SpaceSW* get_owner() { return owner; }
  90. void set_monitor_callback(ObjectID p_id, const StringName &p_method);
  91. _FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback_id; }
  92. void set_area_monitor_callback(ObjectID p_id, const StringName &p_method);
  93. _FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback_id; }
  94. _FORCE_INLINE_ void add_body_to_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  95. _FORCE_INLINE_ void remove_body_from_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  96. _FORCE_INLINE_ void add_area_to_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  97. _FORCE_INLINE_ void remove_area_from_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  98. void set_param(Physics2DServer::AreaParameter p_param, const Variant &p_value);
  99. Variant get_param(Physics2DServer::AreaParameter p_param) const;
  100. void set_space_override_mode(Physics2DServer::AreaSpaceOverrideMode p_mode);
  101. Physics2DServer::AreaSpaceOverrideMode get_space_override_mode() const { return space_override_mode; }
  102. _FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }
  103. _FORCE_INLINE_ real_t get_gravity() const { return gravity; }
  104. _FORCE_INLINE_ void set_gravity_vector(const Vector2 &p_gravity) { gravity_vector = p_gravity; }
  105. _FORCE_INLINE_ Vector2 get_gravity_vector() const { return gravity_vector; }
  106. _FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }
  107. _FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }
  108. _FORCE_INLINE_ void set_gravity_distance_scale(real_t scale) { gravity_distance_scale = scale; }
  109. _FORCE_INLINE_ real_t get_gravity_distance_scale() const { return gravity_distance_scale; }
  110. _FORCE_INLINE_ void set_point_attenuation(real_t p_point_attenuation) { point_attenuation = p_point_attenuation; }
  111. _FORCE_INLINE_ real_t get_point_attenuation() const { return point_attenuation; }
  112. _FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }
  113. _FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
  114. _FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }
  115. _FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
  116. _FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }
  117. _FORCE_INLINE_ int get_priority() const { return priority; }
  118. _FORCE_INLINE_ void add_constraint(Constraint2DSW *p_constraint) { constraints.insert(p_constraint); }
  119. _FORCE_INLINE_ void remove_constraint(Constraint2DSW *p_constraint) { constraints.erase(p_constraint); }
  120. _FORCE_INLINE_ const Set<Constraint2DSW *> &get_constraints() const { return constraints; }
  121. _FORCE_INLINE_ void clear_constraints() { constraints.clear(); }
  122. void set_monitorable(bool p_monitorable);
  123. _FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
  124. void set_transform(const Transform2D &p_transform);
  125. void set_space(Space2DSW *p_space);
  126. void call_queries();
  127. Area2DSW();
  128. ~Area2DSW();
  129. };
  130. void Area2DSW::add_body_to_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  131. BodyKey bk(p_body, p_body_shape, p_area_shape);
  132. monitored_bodies[bk].inc();
  133. if (!monitor_query_list.in_list())
  134. _queue_monitor_update();
  135. }
  136. void Area2DSW::remove_body_from_query(Body2DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  137. BodyKey bk(p_body, p_body_shape, p_area_shape);
  138. monitored_bodies[bk].dec();
  139. if (!monitor_query_list.in_list())
  140. _queue_monitor_update();
  141. }
  142. void Area2DSW::add_area_to_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  143. BodyKey bk(p_area, p_area_shape, p_self_shape);
  144. monitored_areas[bk].inc();
  145. if (!monitor_query_list.in_list())
  146. _queue_monitor_update();
  147. }
  148. void Area2DSW::remove_area_from_query(Area2DSW *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  149. BodyKey bk(p_area, p_area_shape, p_self_shape);
  150. monitored_areas[bk].dec();
  151. if (!monitor_query_list.in_list())
  152. _queue_monitor_update();
  153. }
  154. #endif // AREA_2D_SW_H