area_2d.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/vset.h"
  33. #include "scene/2d/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 space_override;
  46. Vector2 gravity_vec;
  47. real_t gravity;
  48. bool gravity_is_point;
  49. real_t gravity_distance_scale;
  50. real_t linear_damp;
  51. real_t angular_damp;
  52. int priority;
  53. bool monitoring;
  54. bool monitorable;
  55. bool locked;
  56. void _body_inout(int p_status, const RID &p_body, int p_instance, int p_body_shape, int p_area_shape);
  57. void _body_enter_tree(ObjectID p_id);
  58. void _body_exit_tree(ObjectID p_id);
  59. struct ShapePair {
  60. int body_shape;
  61. int area_shape;
  62. bool operator<(const ShapePair &p_sp) const {
  63. if (body_shape == p_sp.body_shape) {
  64. return area_shape < p_sp.area_shape;
  65. } else {
  66. return body_shape < p_sp.body_shape;
  67. }
  68. }
  69. ShapePair() {}
  70. ShapePair(int p_bs, int p_as) {
  71. body_shape = p_bs;
  72. area_shape = p_as;
  73. }
  74. };
  75. struct BodyState {
  76. RID rid;
  77. int rc;
  78. bool in_tree;
  79. VSet<ShapePair> shapes;
  80. };
  81. Map<ObjectID, BodyState> body_map;
  82. void _area_inout(int p_status, const RID &p_area, int p_instance, int p_area_shape, int p_self_shape);
  83. void _area_enter_tree(ObjectID p_id);
  84. void _area_exit_tree(ObjectID p_id);
  85. struct AreaShapePair {
  86. int area_shape;
  87. int self_shape;
  88. bool operator<(const AreaShapePair &p_sp) const {
  89. if (area_shape == p_sp.area_shape) {
  90. return self_shape < p_sp.self_shape;
  91. } else {
  92. return area_shape < p_sp.area_shape;
  93. }
  94. }
  95. AreaShapePair() {}
  96. AreaShapePair(int p_bs, int p_as) {
  97. area_shape = p_bs;
  98. self_shape = p_as;
  99. }
  100. };
  101. struct AreaState {
  102. RID rid;
  103. int rc;
  104. bool in_tree;
  105. VSet<AreaShapePair> shapes;
  106. };
  107. Map<ObjectID, AreaState> area_map;
  108. void _clear_monitoring();
  109. bool audio_bus_override;
  110. StringName audio_bus;
  111. protected:
  112. void _notification(int p_what);
  113. static void _bind_methods();
  114. void _validate_property(PropertyInfo &property) const;
  115. public:
  116. void set_space_override_mode(SpaceOverride p_mode);
  117. SpaceOverride get_space_override_mode() const;
  118. void set_gravity_is_point(bool p_enabled);
  119. bool is_gravity_a_point() const;
  120. void set_gravity_distance_scale(real_t p_scale);
  121. real_t get_gravity_distance_scale() const;
  122. void set_gravity_vector(const Vector2 &p_vec);
  123. Vector2 get_gravity_vector() const;
  124. void set_gravity(real_t p_gravity);
  125. real_t get_gravity() const;
  126. void set_linear_damp(real_t p_linear_damp);
  127. real_t get_linear_damp() const;
  128. void set_angular_damp(real_t p_angular_damp);
  129. real_t get_angular_damp() const;
  130. void set_priority(real_t p_priority);
  131. real_t get_priority() const;
  132. void set_monitoring(bool p_enable);
  133. bool is_monitoring() const;
  134. void set_monitorable(bool p_enable);
  135. bool is_monitorable() const;
  136. Array get_overlapping_bodies() const; //function for script
  137. Array get_overlapping_areas() const; //function for script
  138. bool overlaps_area(Node *p_area) const;
  139. bool overlaps_body(Node *p_body) const;
  140. void set_audio_bus_override(bool p_override);
  141. bool is_overriding_audio_bus() const;
  142. void set_audio_bus_name(const StringName &p_audio_bus);
  143. StringName get_audio_bus_name() const;
  144. Area2D();
  145. ~Area2D();
  146. };
  147. VARIANT_ENUM_CAST(Area2D::SpaceOverride);
  148. #endif // AREA_2D_H