area_2d.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /* area_2d.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_H
  31. #define AREA_2D_H
  32. #include "core/templates/vset.h"
  33. #include "scene/2d/physics/collision_object_2d.h"
  34. class Area2D : public CollisionObject2D {
  35. GDCLASS(Area2D, CollisionObject2D);
  36. public:
  37. enum SpaceOverride {
  38. SPACE_OVERRIDE_DISABLED,
  39. SPACE_OVERRIDE_COMBINE,
  40. SPACE_OVERRIDE_COMBINE_REPLACE,
  41. SPACE_OVERRIDE_REPLACE,
  42. SPACE_OVERRIDE_REPLACE_COMBINE
  43. };
  44. private:
  45. SpaceOverride gravity_space_override = SPACE_OVERRIDE_DISABLED;
  46. Vector2 gravity_vec;
  47. real_t gravity = 0.0;
  48. bool gravity_is_point = false;
  49. real_t gravity_point_unit_distance = 0.0;
  50. SpaceOverride linear_damp_space_override = SPACE_OVERRIDE_DISABLED;
  51. SpaceOverride angular_damp_space_override = SPACE_OVERRIDE_DISABLED;
  52. real_t linear_damp = 0.1;
  53. real_t angular_damp = 1.0;
  54. int priority = 0;
  55. bool monitoring = false;
  56. bool monitorable = false;
  57. bool locked = false;
  58. void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_area_shape);
  59. void _body_enter_tree(ObjectID p_id);
  60. void _body_exit_tree(ObjectID p_id);
  61. struct ShapePair {
  62. int body_shape = 0;
  63. int area_shape = 0;
  64. bool operator<(const ShapePair &p_sp) const {
  65. if (body_shape == p_sp.body_shape) {
  66. return area_shape < p_sp.area_shape;
  67. } else {
  68. return body_shape < p_sp.body_shape;
  69. }
  70. }
  71. ShapePair() {}
  72. ShapePair(int p_bs, int p_as) {
  73. body_shape = p_bs;
  74. area_shape = p_as;
  75. }
  76. };
  77. struct BodyState {
  78. RID rid;
  79. int rc = 0;
  80. bool in_tree = false;
  81. VSet<ShapePair> shapes;
  82. };
  83. HashMap<ObjectID, BodyState> body_map;
  84. void _area_inout(int p_status, const RID &p_area, ObjectID p_instance, int p_area_shape, int p_self_shape);
  85. void _area_enter_tree(ObjectID p_id);
  86. void _area_exit_tree(ObjectID p_id);
  87. struct AreaShapePair {
  88. int area_shape = 0;
  89. int self_shape = 0;
  90. bool operator<(const AreaShapePair &p_sp) const {
  91. if (area_shape == p_sp.area_shape) {
  92. return self_shape < p_sp.self_shape;
  93. } else {
  94. return area_shape < p_sp.area_shape;
  95. }
  96. }
  97. AreaShapePair() {}
  98. AreaShapePair(int p_bs, int p_as) {
  99. area_shape = p_bs;
  100. self_shape = p_as;
  101. }
  102. };
  103. struct AreaState {
  104. RID rid;
  105. int rc = 0;
  106. bool in_tree = false;
  107. VSet<AreaShapePair> shapes;
  108. };
  109. HashMap<ObjectID, AreaState> area_map;
  110. void _clear_monitoring();
  111. bool audio_bus_override = false;
  112. StringName audio_bus;
  113. protected:
  114. static void _bind_methods();
  115. void _validate_property(PropertyInfo &p_property) const;
  116. virtual void _space_changed(const RID &p_new_space) override;
  117. public:
  118. void set_gravity_space_override_mode(SpaceOverride p_mode);
  119. SpaceOverride get_gravity_space_override_mode() const;
  120. void set_gravity_is_point(bool p_enabled);
  121. bool is_gravity_a_point() const;
  122. void set_gravity_point_unit_distance(real_t p_scale);
  123. real_t get_gravity_point_unit_distance() const;
  124. void set_gravity_point_center(const Vector2 &p_center);
  125. const Vector2 &get_gravity_point_center() const;
  126. void set_gravity_direction(const Vector2 &p_direction);
  127. const Vector2 &get_gravity_direction() const;
  128. void set_gravity(real_t p_gravity);
  129. real_t get_gravity() const;
  130. void set_linear_damp_space_override_mode(SpaceOverride p_mode);
  131. SpaceOverride get_linear_damp_space_override_mode() const;
  132. void set_angular_damp_space_override_mode(SpaceOverride p_mode);
  133. SpaceOverride get_angular_damp_space_override_mode() const;
  134. void set_linear_damp(real_t p_linear_damp);
  135. real_t get_linear_damp() const;
  136. void set_angular_damp(real_t p_angular_damp);
  137. real_t get_angular_damp() const;
  138. void set_priority(int p_priority);
  139. int get_priority() const;
  140. void set_monitoring(bool p_enable);
  141. bool is_monitoring() const;
  142. void set_monitorable(bool p_enable);
  143. bool is_monitorable() const;
  144. TypedArray<Node2D> get_overlapping_bodies() const; //function for script
  145. TypedArray<Area2D> get_overlapping_areas() const; //function for script
  146. bool has_overlapping_bodies() const;
  147. bool has_overlapping_areas() const;
  148. bool overlaps_area(Node *p_area) const;
  149. bool overlaps_body(Node *p_body) const;
  150. void set_audio_bus_override(bool p_override);
  151. bool is_overriding_audio_bus() const;
  152. void set_audio_bus_name(const StringName &p_audio_bus);
  153. StringName get_audio_bus_name() const;
  154. Area2D();
  155. ~Area2D();
  156. };
  157. VARIANT_ENUM_CAST(Area2D::SpaceOverride);
  158. #endif // AREA_2D_H