1 /*************************************************************************/
2 /*  collision_object_2d.h                                                */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef COLLISION_OBJECT_2D_H
32 #define COLLISION_OBJECT_2D_H
33 
34 #include "scene/2d/node_2d.h"
35 #include "scene/resources/shape_2d.h"
36 
37 class CollisionObject2D : public Node2D {
38 
39 	GDCLASS(CollisionObject2D, Node2D);
40 
41 	bool area;
42 	RID rid;
43 	bool pickable;
44 
45 	struct ShapeData {
46 
47 		Object *owner;
48 		Transform2D xform;
49 		struct Shape {
50 			Ref<Shape2D> shape;
51 			int index;
52 		};
53 
54 		Vector<Shape> shapes;
55 		bool disabled;
56 		bool one_way_collision;
57 		float one_way_collision_margin;
58 
ShapeDataShapeData59 		ShapeData() {
60 			disabled = false;
61 			one_way_collision = false;
62 			one_way_collision_margin = 0;
63 			owner = NULL;
64 		}
65 	};
66 
67 	int total_subshapes;
68 
69 	Map<uint32_t, ShapeData> shapes;
70 	bool only_update_transform_changes; //this is used for sync physics in KinematicBody
71 
72 protected:
73 	CollisionObject2D(RID p_rid, bool p_area);
74 
75 	void _notification(int p_what);
76 	static void _bind_methods();
77 
78 	void _update_pickable();
79 	friend class Viewport;
80 	void _input_event(Node *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape);
81 	void _mouse_enter();
82 	void _mouse_exit();
83 
84 	void set_only_update_transform_changes(bool p_enable);
85 
86 public:
87 	uint32_t create_shape_owner(Object *p_owner);
88 	void remove_shape_owner(uint32_t owner);
89 	void get_shape_owners(List<uint32_t> *r_owners);
90 	Array _get_shape_owners();
91 
92 	void shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform);
93 	Transform2D shape_owner_get_transform(uint32_t p_owner) const;
94 	Object *shape_owner_get_owner(uint32_t p_owner) const;
95 
96 	void shape_owner_set_disabled(uint32_t p_owner, bool p_disabled);
97 	bool is_shape_owner_disabled(uint32_t p_owner) const;
98 
99 	void shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable);
100 	bool is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const;
101 
102 	void shape_owner_set_one_way_collision_margin(uint32_t p_owner, float p_margin);
103 	float get_shape_owner_one_way_collision_margin(uint32_t p_owner) const;
104 
105 	void shape_owner_add_shape(uint32_t p_owner, const Ref<Shape2D> &p_shape);
106 	int shape_owner_get_shape_count(uint32_t p_owner) const;
107 	Ref<Shape2D> shape_owner_get_shape(uint32_t p_owner, int p_shape) const;
108 	int shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const;
109 
110 	void shape_owner_remove_shape(uint32_t p_owner, int p_shape);
111 	void shape_owner_clear_shapes(uint32_t p_owner);
112 
113 	uint32_t shape_find_owner(int p_shape_index) const;
114 
115 	void set_pickable(bool p_enabled);
116 	bool is_pickable() const;
117 
118 	String get_configuration_warning() const;
119 
get_rid()120 	_FORCE_INLINE_ RID get_rid() const { return rid; }
121 
122 	CollisionObject2D();
123 	~CollisionObject2D();
124 };
125 
126 #endif // COLLISION_OBJECT_2D_H
127