1 /*************************************************************************/
2 /*  area_2d.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_H
31 #define AREA_2D_H
32 
33 #include "scene/2d/collision_object_2d.h"
34 #include "vset.h"
35 
36 class Area2D : public CollisionObject2D {
37 
38 	OBJ_TYPE(Area2D, CollisionObject2D);
39 
40 public:
41 	enum SpaceOverride {
42 		SPACE_OVERRIDE_DISABLED,
43 		SPACE_OVERRIDE_COMBINE,
44 		SPACE_OVERRIDE_COMBINE_REPLACE,
45 		SPACE_OVERRIDE_REPLACE,
46 		SPACE_OVERRIDE_REPLACE_COMBINE
47 	};
48 
49 private:
50 	SpaceOverride space_override;
51 	Vector2 gravity_vec;
52 	real_t gravity;
53 	bool gravity_is_point;
54 	real_t gravity_distance_scale;
55 	real_t linear_damp;
56 	real_t angular_damp;
57 	uint32_t collision_mask;
58 	uint32_t layer_mask;
59 	int priority;
60 	bool monitoring;
61 	bool monitoring_stored;
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 protected:
131 	void _notification(int p_what);
132 	static void _bind_methods();
133 
134 public:
135 	void set_space_override_mode(SpaceOverride p_mode);
136 	SpaceOverride get_space_override_mode() const;
137 
138 	void set_gravity_is_point(bool p_enabled);
139 	bool is_gravity_a_point() const;
140 
141 	void set_gravity_distance_scale(real_t p_scale);
142 	real_t get_gravity_distance_scale() const;
143 
144 	void set_gravity_vector(const Vector2 &p_vec);
145 	Vector2 get_gravity_vector() const;
146 
147 	void set_gravity(real_t p_gravity);
148 	real_t get_gravity() const;
149 
150 	void set_linear_damp(real_t p_linear_damp);
151 	real_t get_linear_damp() const;
152 
153 	void set_angular_damp(real_t p_angular_damp);
154 	real_t get_angular_damp() const;
155 
156 	void set_priority(real_t p_priority);
157 	real_t get_priority() const;
158 
159 	void set_enable_monitoring(bool p_enable);
160 	bool is_monitoring_enabled() const;
161 
162 	void set_monitorable(bool p_enable);
163 	bool is_monitorable() const;
164 
165 	void set_collision_mask(uint32_t p_mask);
166 	uint32_t get_collision_mask() const;
167 
168 	void set_layer_mask(uint32_t p_mask);
169 	uint32_t get_layer_mask() const;
170 
171 	void set_collision_mask_bit(int p_bit, bool p_value);
172 	bool get_collision_mask_bit(int p_bit) const;
173 
174 	void set_layer_mask_bit(int p_bit, bool p_value);
175 	bool get_layer_mask_bit(int p_bit) const;
176 
177 	Array get_overlapping_bodies() const; //function for script
178 	Array get_overlapping_areas() const; //function for script
179 
180 	bool overlaps_area(Node *p_area) const;
181 	bool overlaps_body(Node *p_body) const;
182 
183 	Area2D();
184 	~Area2D();
185 };
186 
187 VARIANT_ENUM_CAST(Area2D::SpaceOverride);
188 
189 #endif // AREA_2D_H
190