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