1 /*************************************************************************/ 2 /* area_2d.h */ 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 31 #ifndef AREA_2D_H 32 #define AREA_2D_H 33 34 #include "core/vset.h" 35 #include "scene/2d/collision_object_2d.h" 36 37 class Area2D : public CollisionObject2D { 38 39 GDCLASS(Area2D, CollisionObject2D); 40 41 public: 42 enum SpaceOverride { 43 SPACE_OVERRIDE_DISABLED, 44 SPACE_OVERRIDE_COMBINE, 45 SPACE_OVERRIDE_COMBINE_REPLACE, 46 SPACE_OVERRIDE_REPLACE, 47 SPACE_OVERRIDE_REPLACE_COMBINE 48 }; 49 50 private: 51 SpaceOverride space_override; 52 Vector2 gravity_vec; 53 real_t gravity; 54 bool gravity_is_point; 55 real_t gravity_distance_scale; 56 real_t linear_damp; 57 real_t angular_damp; 58 uint32_t collision_mask; 59 uint32_t collision_layer; 60 int priority; 61 bool monitoring; 62 bool monitorable; 63 bool locked; 64 65 void _body_inout(int p_status, const RID &p_body, int p_instance, int p_body_shape, int p_area_shape); 66 67 void _body_enter_tree(ObjectID p_id); 68 void _body_exit_tree(ObjectID p_id); 69 70 struct ShapePair { 71 72 int body_shape; 73 int area_shape; 74 bool operator<(const ShapePair &p_sp) const { 75 if (body_shape == p_sp.body_shape) 76 return area_shape < p_sp.area_shape; 77 else 78 return body_shape < p_sp.body_shape; 79 } 80 ShapePairShapePair81 ShapePair() {} ShapePairShapePair82 ShapePair(int p_bs, int p_as) { 83 body_shape = p_bs; 84 area_shape = p_as; 85 } 86 }; 87 88 struct BodyState { 89 90 int rc; 91 bool in_tree; 92 VSet<ShapePair> shapes; 93 }; 94 95 Map<ObjectID, BodyState> body_map; 96 97 void _area_inout(int p_status, const RID &p_area, int p_instance, int p_area_shape, int p_self_shape); 98 99 void _area_enter_tree(ObjectID p_id); 100 void _area_exit_tree(ObjectID p_id); 101 102 struct AreaShapePair { 103 104 int area_shape; 105 int self_shape; 106 bool operator<(const AreaShapePair &p_sp) const { 107 if (area_shape == p_sp.area_shape) 108 return self_shape < p_sp.self_shape; 109 else 110 return area_shape < p_sp.area_shape; 111 } 112 AreaShapePairAreaShapePair113 AreaShapePair() {} AreaShapePairAreaShapePair114 AreaShapePair(int p_bs, int p_as) { 115 area_shape = p_bs; 116 self_shape = p_as; 117 } 118 }; 119 120 struct AreaState { 121 122 int rc; 123 bool in_tree; 124 VSet<AreaShapePair> shapes; 125 }; 126 127 Map<ObjectID, AreaState> area_map; 128 void _clear_monitoring(); 129 130 bool audio_bus_override; 131 StringName audio_bus; 132 133 protected: 134 void _notification(int p_what); 135 static void _bind_methods(); 136 void _validate_property(PropertyInfo &property) const; 137 138 public: 139 void set_space_override_mode(SpaceOverride p_mode); 140 SpaceOverride get_space_override_mode() const; 141 142 void set_gravity_is_point(bool p_enabled); 143 bool is_gravity_a_point() const; 144 145 void set_gravity_distance_scale(real_t p_scale); 146 real_t get_gravity_distance_scale() const; 147 148 void set_gravity_vector(const Vector2 &p_vec); 149 Vector2 get_gravity_vector() const; 150 151 void set_gravity(real_t p_gravity); 152 real_t get_gravity() const; 153 154 void set_linear_damp(real_t p_linear_damp); 155 real_t get_linear_damp() const; 156 157 void set_angular_damp(real_t p_angular_damp); 158 real_t get_angular_damp() const; 159 160 void set_priority(real_t p_priority); 161 real_t get_priority() const; 162 163 void set_monitoring(bool p_enable); 164 bool is_monitoring() const; 165 166 void set_monitorable(bool p_enable); 167 bool is_monitorable() const; 168 169 void set_collision_mask(uint32_t p_mask); 170 uint32_t get_collision_mask() const; 171 172 void set_collision_layer(uint32_t p_layer); 173 uint32_t get_collision_layer() const; 174 175 void set_collision_mask_bit(int p_bit, bool p_value); 176 bool get_collision_mask_bit(int p_bit) const; 177 178 void set_collision_layer_bit(int p_bit, bool p_value); 179 bool get_collision_layer_bit(int p_bit) const; 180 181 Array get_overlapping_bodies() const; //function for script 182 Array get_overlapping_areas() const; //function for script 183 184 bool overlaps_area(Node *p_area) const; 185 bool overlaps_body(Node *p_body) const; 186 187 void set_audio_bus_override(bool p_override); 188 bool is_overriding_audio_bus() const; 189 190 void set_audio_bus_name(const StringName &p_audio_bus); 191 StringName get_audio_bus_name() const; 192 193 Area2D(); 194 ~Area2D(); 195 }; 196 197 VARIANT_ENUM_CAST(Area2D::SpaceOverride); 198 199 #endif // AREA_2D_H 200