| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "godot_collision_object_2d.h" |
| 34 |
|
| 35 |
#include "core/templates/self_list.h" |
| 36 |
#include "servers/physics_2d/physics_server_2d.h" |
| 37 |
|
| 38 |
class GodotSpace2D; |
| 39 |
class GodotBody2D; |
| 40 |
class GodotConstraint2D; |
| 41 |
|
| 42 |
class GodotArea2D : public GodotCollisionObject2D { |
| 43 |
PhysicsServer2D::AreaSpaceOverrideMode gravity_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED; |
| 44 |
PhysicsServer2D::AreaSpaceOverrideMode linear_damping_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED; |
| 45 |
PhysicsServer2D::AreaSpaceOverrideMode angular_damping_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED; |
| 46 |
|
| 47 |
real_t gravity = 9.80665; |
| 48 |
Vector2 gravity_vector = Vector2(0, -1); |
| 49 |
bool gravity_is_point = false; |
| 50 |
real_t gravity_point_unit_distance = 0.0; |
| 51 |
real_t linear_damp = 0.1; |
| 52 |
real_t angular_damp = 1.0; |
| 53 |
int priority = 0; |
| 54 |
bool monitorable = false; |
| 55 |
|
| 56 |
Callable monitor_callback; |
| 57 |
|
| 58 |
Callable area_monitor_callback; |
| 59 |
|
| 60 |
SelfList<GodotArea2D> monitor_query_list; |
| 61 |
SelfList<GodotArea2D> moved_list; |
| 62 |
|
| 63 |
struct BodyKey { |
| 64 |
RID rid; |
| 65 |
ObjectID instance_id; |
| 66 |
uint32_t body_shape = 0; |
| 67 |
uint32_t area_shape = 0; |
| 68 |
|
| 69 |
static uint32_t hash(const BodyKey &p_key) { |
| 70 |
uint32_t h = hash_one_uint64(p_key.rid.get_id()); |
| 71 |
h = hash_murmur3_one_64(p_key.instance_id, h); |
| 72 |
h = hash_murmur3_one_32(p_key.area_shape, h); |
| 73 |
return hash_fmix32(hash_murmur3_one_32(p_key.body_shape, h)); |
| 74 |
} |
| 75 |
|
| 76 |
_FORCE_INLINE_ bool operator==(const BodyKey &p_key) const { |
| 77 |
return rid == p_key.rid && instance_id == p_key.instance_id && body_shape == p_key.body_shape && area_shape == p_key.area_shape; |
| 78 |
} |
| 79 |
|
| 80 |
_FORCE_INLINE_ BodyKey() {} |
| 81 |
BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape); |
| 82 |
BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape); |
| 83 |
}; |
| 84 |
|
| 85 |
struct BodyState { |
| 86 |
int state = 0; |
| 87 |
_FORCE_INLINE_ void inc() { state++; } |
| 88 |
_FORCE_INLINE_ void dec() { state--; } |
| 89 |
}; |
| 90 |
|
| 91 |
HashMap<BodyKey, BodyState, BodyKey> monitored_bodies; |
| 92 |
HashMap<BodyKey, BodyState, BodyKey> monitored_areas; |
| 93 |
|
| 94 |
HashSet<GodotConstraint2D *> constraints; |
| 95 |
|
| 96 |
virtual void _shapes_changed() override; |
| 97 |
void _queue_monitor_update(); |
| 98 |
|
| 99 |
void _set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode); |
| 100 |
|
| 101 |
public: |
| 102 |
void set_monitor_callback(const Callable &p_callback); |
| 103 |
_FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback.is_valid(); } |
| 104 |
|
| 105 |
void set_area_monitor_callback(const Callable &p_callback); |
| 106 |
_FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback.is_valid(); } |
| 107 |
|
| 108 |
_FORCE_INLINE_ void add_body_to_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape); |
| 109 |
_FORCE_INLINE_ void remove_body_from_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape); |
| 110 |
|
| 111 |
_FORCE_INLINE_ void add_area_to_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape); |
| 112 |
_FORCE_INLINE_ void remove_area_from_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape); |
| 113 |
|
| 114 |
void set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value); |
| 115 |
Variant get_param(PhysicsServer2D::AreaParameter p_param) const; |
| 116 |
|
| 117 |
_FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; } |
| 118 |
_FORCE_INLINE_ real_t get_gravity() const { return gravity; } |
| 119 |
|
| 120 |
_FORCE_INLINE_ void set_gravity_vector(const Vector2 &p_gravity) { gravity_vector = p_gravity; } |
| 121 |
_FORCE_INLINE_ Vector2 get_gravity_vector() const { return gravity_vector; } |
| 122 |
|
| 123 |
_FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; } |
| 124 |
_FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; } |
| 125 |
|
| 126 |
_FORCE_INLINE_ void set_gravity_point_unit_distance(real_t scale) { gravity_point_unit_distance = scale; } |
| 127 |
_FORCE_INLINE_ real_t get_gravity_point_unit_distance() const { return gravity_point_unit_distance; } |
| 128 |
|
| 129 |
_FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; } |
| 130 |
_FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; } |
| 131 |
|
| 132 |
_FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; } |
| 133 |
_FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; } |
| 134 |
|
| 135 |
_FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; } |
| 136 |
_FORCE_INLINE_ int get_priority() const { return priority; } |
| 137 |
|
| 138 |
_FORCE_INLINE_ void add_constraint(GodotConstraint2D *p_constraint) { constraints.insert(p_constraint); } |
| 139 |
_FORCE_INLINE_ void remove_constraint(GodotConstraint2D *p_constraint) { constraints.erase(p_constraint); } |
| 140 |
_FORCE_INLINE_ const HashSet<GodotConstraint2D *> &get_constraints() const { return constraints; } |
| 141 |
_FORCE_INLINE_ void clear_constraints() { constraints.clear(); } |
| 142 |
|
| 143 |
void set_monitorable(bool p_monitorable); |
| 144 |
_FORCE_INLINE_ bool is_monitorable() const { return monitorable; } |
| 145 |
|
| 146 |
void set_transform(const Transform2D &p_transform); |
| 147 |
|
| 148 |
void set_space(GodotSpace2D *p_space) override; |
| 149 |
|
| 150 |
void call_queries(); |
| 151 |
|
| 152 |
void compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const; |
| 153 |
|
| 154 |
GodotArea2D(); |
| 155 |
~GodotArea2D(); |
| 156 |
}; |
| 157 |
|
| 158 |
void GodotArea2D::add_body_to_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) { |
| 159 |
BodyKey bk(p_body, p_body_shape, p_area_shape); |
| 160 |
monitored_bodies[bk].inc(); |
| 161 |
if (!monitor_query_list.in_list()) { |
| 162 |
_queue_monitor_update(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void GodotArea2D::remove_body_from_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) { |
| 167 |
BodyKey bk(p_body, p_body_shape, p_area_shape); |
| 168 |
monitored_bodies[bk].dec(); |
| 169 |
if (get_space() && !monitor_query_list.in_list()) { |
| 170 |
_queue_monitor_update(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void GodotArea2D::add_area_to_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) { |
| 175 |
BodyKey bk(p_area, p_area_shape, p_self_shape); |
| 176 |
monitored_areas[bk].inc(); |
| 177 |
if (!monitor_query_list.in_list()) { |
| 178 |
_queue_monitor_update(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
void GodotArea2D::remove_area_from_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) { |
| 183 |
BodyKey bk(p_area, p_area_shape, p_self_shape); |
| 184 |
monitored_areas[bk].dec(); |
| 185 |
if (get_space() && !monitor_query_list.in_list()) { |
| 186 |
_queue_monitor_update(); |
| 187 |
} |
| 188 |
} |