1 | /**************************************************************************/ |
2 | /* collision_object_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 COLLISION_OBJECT_2D_H |
32 | #define COLLISION_OBJECT_2D_H |
33 | |
34 | #include "scene/2d/node_2d.h" |
35 | #include "scene/main/viewport.h" |
36 | #include "scene/resources/shape_2d.h" |
37 | #include "servers/physics_server_2d.h" |
38 | |
39 | class CollisionObject2D : public Node2D { |
40 | GDCLASS(CollisionObject2D, Node2D); |
41 | |
42 | public: |
43 | enum DisableMode { |
44 | DISABLE_MODE_REMOVE, |
45 | DISABLE_MODE_MAKE_STATIC, |
46 | DISABLE_MODE_KEEP_ACTIVE, |
47 | }; |
48 | |
49 | private: |
50 | uint32_t collision_layer = 1; |
51 | uint32_t collision_mask = 1; |
52 | real_t collision_priority = 1.0; |
53 | |
54 | bool area = false; |
55 | RID rid; |
56 | uint32_t callback_lock = 0; |
57 | bool pickable = false; |
58 | |
59 | DisableMode disable_mode = DISABLE_MODE_REMOVE; |
60 | |
61 | PhysicsServer2D::BodyMode body_mode = PhysicsServer2D::BODY_MODE_STATIC; |
62 | |
63 | struct ShapeData { |
64 | ObjectID owner_id; |
65 | Transform2D xform; |
66 | struct Shape { |
67 | Ref<Shape2D> shape; |
68 | int index = 0; |
69 | }; |
70 | |
71 | Vector<Shape> shapes; |
72 | |
73 | bool disabled = false; |
74 | bool one_way_collision = false; |
75 | real_t one_way_collision_margin = 0.0; |
76 | }; |
77 | |
78 | int total_subshapes = 0; |
79 | |
80 | RBMap<uint32_t, ShapeData> shapes; |
81 | bool only_update_transform_changes = false; // This is used for sync to physics. |
82 | |
83 | void _apply_disabled(); |
84 | void _apply_enabled(); |
85 | |
86 | protected: |
87 | _FORCE_INLINE_ void lock_callback() { callback_lock++; } |
88 | _FORCE_INLINE_ void unlock_callback() { |
89 | ERR_FAIL_COND(callback_lock == 0); |
90 | callback_lock--; |
91 | } |
92 | |
93 | CollisionObject2D(RID p_rid, bool p_area); |
94 | |
95 | void _notification(int p_what); |
96 | static void _bind_methods(); |
97 | |
98 | void _update_pickable(); |
99 | friend class Viewport; |
100 | void _input_event_call(Viewport *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape); |
101 | void _mouse_enter(); |
102 | void _mouse_exit(); |
103 | |
104 | void _mouse_shape_enter(int p_shape); |
105 | void _mouse_shape_exit(int p_shape); |
106 | |
107 | void set_only_update_transform_changes(bool p_enable); |
108 | bool is_only_update_transform_changes_enabled() const; |
109 | |
110 | void set_body_mode(PhysicsServer2D::BodyMode p_mode); |
111 | |
112 | GDVIRTUAL3(_input_event, Viewport *, Ref<InputEvent>, int) |
113 | GDVIRTUAL0(_mouse_enter) |
114 | GDVIRTUAL0(_mouse_exit) |
115 | GDVIRTUAL1(_mouse_shape_enter, int) |
116 | GDVIRTUAL1(_mouse_shape_exit, int) |
117 | public: |
118 | void set_collision_layer(uint32_t p_layer); |
119 | uint32_t get_collision_layer() const; |
120 | |
121 | void set_collision_mask(uint32_t p_mask); |
122 | uint32_t get_collision_mask() const; |
123 | |
124 | void set_collision_layer_value(int p_layer_number, bool p_value); |
125 | bool get_collision_layer_value(int p_layer_number) const; |
126 | |
127 | void set_collision_mask_value(int p_layer_number, bool p_value); |
128 | bool get_collision_mask_value(int p_layer_number) const; |
129 | |
130 | void set_collision_priority(real_t p_priority); |
131 | real_t get_collision_priority() const; |
132 | |
133 | void set_disable_mode(DisableMode p_mode); |
134 | DisableMode get_disable_mode() const; |
135 | |
136 | uint32_t create_shape_owner(Object *p_owner); |
137 | void remove_shape_owner(uint32_t owner); |
138 | void get_shape_owners(List<uint32_t> *r_owners); |
139 | PackedInt32Array _get_shape_owners(); |
140 | |
141 | void shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform); |
142 | Transform2D shape_owner_get_transform(uint32_t p_owner) const; |
143 | Object *shape_owner_get_owner(uint32_t p_owner) const; |
144 | |
145 | void shape_owner_set_disabled(uint32_t p_owner, bool p_disabled); |
146 | bool is_shape_owner_disabled(uint32_t p_owner) const; |
147 | |
148 | void shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable); |
149 | bool is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const; |
150 | |
151 | void shape_owner_set_one_way_collision_margin(uint32_t p_owner, real_t p_margin); |
152 | real_t get_shape_owner_one_way_collision_margin(uint32_t p_owner) const; |
153 | |
154 | void shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape); |
155 | int shape_owner_get_shape_count(uint32_t p_owner) const; |
156 | Ref<Shape2D> shape_owner_get_shape(uint32_t p_owner, int p_shape) const; |
157 | int shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const; |
158 | |
159 | void shape_owner_remove_shape(uint32_t p_owner, int p_shape); |
160 | void shape_owner_clear_shapes(uint32_t p_owner); |
161 | |
162 | uint32_t shape_find_owner(int p_shape_index) const; |
163 | |
164 | void set_pickable(bool p_enabled); |
165 | bool is_pickable() const; |
166 | |
167 | PackedStringArray get_configuration_warnings() const override; |
168 | |
169 | _FORCE_INLINE_ RID get_rid() const { return rid; } |
170 | |
171 | CollisionObject2D(); |
172 | ~CollisionObject2D(); |
173 | }; |
174 | |
175 | VARIANT_ENUM_CAST(CollisionObject2D::DisableMode); |
176 | |
177 | #endif // COLLISION_OBJECT_2D_H |
178 | |