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-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #ifndef COLLISION_OBJECT_2D_H
31 #define COLLISION_OBJECT_2D_H
32 
33 #include "scene/2d/node_2d.h"
34 #include "scene/resources/shape_2d.h"
35 
36 class CollisionObject2D : public Node2D {
37 
38 	OBJ_TYPE(CollisionObject2D, Node2D);
39 
40 	bool area;
41 	RID rid;
42 	bool pickable;
43 
44 	struct ShapeData {
45 		Matrix32 xform;
46 		Ref<Shape2D> shape;
47 		bool trigger;
48 
ShapeDataShapeData49 		ShapeData() {
50 			trigger = false;
51 		}
52 	};
53 
54 	Vector<ShapeData> shapes;
55 
56 	void _update_shapes();
57 
58 	friend class CollisionShape2D;
59 	friend class CollisionPolygon2D;
60 	void _update_shapes_from_children();
61 
62 protected:
63 	CollisionObject2D(RID p_rid, bool p_area);
64 
65 	void _notification(int p_what);
66 	bool _set(const StringName &p_name, const Variant &p_value);
67 	bool _get(const StringName &p_name, Variant &r_ret) const;
68 	void _get_property_list(List<PropertyInfo> *p_list) const;
69 	static void _bind_methods();
70 
71 	void _update_pickable();
72 	friend class Viewport;
73 	void _input_event(Node *p_viewport, const InputEvent &p_input_event, int p_shape);
74 	void _mouse_enter();
75 	void _mouse_exit();
76 
77 public:
78 	void add_shape(const Ref<Shape2D> &p_shape, const Matrix32 &p_transform = Matrix32());
79 	int get_shape_count() const;
80 	void set_shape(int p_shape_idx, const Ref<Shape2D> &p_shape);
81 	void set_shape_transform(int p_shape_idx, const Matrix32 &p_transform);
82 	Ref<Shape2D> get_shape(int p_shape_idx) const;
83 	Matrix32 get_shape_transform(int p_shape_idx) const;
84 	void set_shape_as_trigger(int p_shape_idx, bool p_trigger);
85 	bool is_shape_set_as_trigger(int p_shape_idx) const;
86 	void remove_shape(int p_shape_idx);
87 	void clear_shapes();
88 
89 	void set_pickable(bool p_enabled);
90 	bool is_pickable() const;
91 
get_rid()92 	_FORCE_INLINE_ RID get_rid() const { return rid; }
93 
94 	CollisionObject2D();
95 	~CollisionObject2D();
96 };
97 
98 #endif // COLLISION_OBJECT_2D_H
99