Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/area_2d.h
9906 views
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
#pragma once
32
33
#include "core/templates/vset.h"
34
#include "scene/2d/physics/collision_object_2d.h"
35
36
class Area2D : public CollisionObject2D {
37
GDCLASS(Area2D, CollisionObject2D);
38
39
public:
40
enum SpaceOverride {
41
SPACE_OVERRIDE_DISABLED,
42
SPACE_OVERRIDE_COMBINE,
43
SPACE_OVERRIDE_COMBINE_REPLACE,
44
SPACE_OVERRIDE_REPLACE,
45
SPACE_OVERRIDE_REPLACE_COMBINE
46
};
47
48
private:
49
SpaceOverride gravity_space_override = SPACE_OVERRIDE_DISABLED;
50
Vector2 gravity_vec;
51
real_t gravity = 0.0;
52
bool gravity_is_point = false;
53
real_t gravity_point_unit_distance = 0.0;
54
55
SpaceOverride linear_damp_space_override = SPACE_OVERRIDE_DISABLED;
56
SpaceOverride angular_damp_space_override = SPACE_OVERRIDE_DISABLED;
57
real_t linear_damp = 0.1;
58
real_t angular_damp = 1.0;
59
60
int priority = 0;
61
62
bool monitoring = false;
63
bool monitorable = false;
64
bool locked = false;
65
66
void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_area_shape);
67
68
void _body_enter_tree(ObjectID p_id);
69
void _body_exit_tree(ObjectID p_id);
70
71
struct ShapePair {
72
int body_shape = 0;
73
int area_shape = 0;
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
}
81
82
ShapePair() {}
83
ShapePair(int p_bs, int p_as) {
84
body_shape = p_bs;
85
area_shape = p_as;
86
}
87
};
88
89
struct BodyState {
90
RID rid;
91
int rc = 0;
92
bool in_tree = false;
93
VSet<ShapePair> shapes;
94
};
95
96
HashMap<ObjectID, BodyState> body_map;
97
98
void _area_inout(int p_status, const RID &p_area, ObjectID p_instance, int p_area_shape, int p_self_shape);
99
100
void _area_enter_tree(ObjectID p_id);
101
void _area_exit_tree(ObjectID p_id);
102
103
struct AreaShapePair {
104
int area_shape = 0;
105
int self_shape = 0;
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
}
113
114
AreaShapePair() {}
115
AreaShapePair(int p_bs, int p_as) {
116
area_shape = p_bs;
117
self_shape = p_as;
118
}
119
};
120
121
struct AreaState {
122
RID rid;
123
int rc = 0;
124
bool in_tree = false;
125
VSet<AreaShapePair> shapes;
126
};
127
128
HashMap<ObjectID, AreaState> area_map;
129
void _clear_monitoring();
130
131
bool audio_bus_override = false;
132
StringName audio_bus;
133
134
protected:
135
static void _bind_methods();
136
void _validate_property(PropertyInfo &p_property) const;
137
138
virtual void _space_changed(const RID &p_new_space) override;
139
140
public:
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
199
VARIANT_ENUM_CAST(Area2D::SpaceOverride);
200
201