1 | /**************************************************************************/ |
2 | /* skeleton_modification_2d_jiggle.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 SKELETON_MODIFICATION_2D_JIGGLE_H |
32 | #define SKELETON_MODIFICATION_2D_JIGGLE_H |
33 | |
34 | #include "scene/2d/skeleton_2d.h" |
35 | #include "scene/resources/skeleton_modification_2d.h" |
36 | |
37 | /////////////////////////////////////// |
38 | // SkeletonModification2DJIGGLE |
39 | /////////////////////////////////////// |
40 | |
41 | class SkeletonModification2DJiggle : public SkeletonModification2D { |
42 | GDCLASS(SkeletonModification2DJiggle, SkeletonModification2D); |
43 | |
44 | private: |
45 | struct Jiggle_Joint_Data2D { |
46 | int bone_idx = -1; |
47 | NodePath bone2d_node; |
48 | ObjectID bone2d_node_cache; |
49 | |
50 | bool override_defaults = false; |
51 | float stiffness = 3; |
52 | float mass = 0.75; |
53 | float damping = 0.75; |
54 | bool use_gravity = false; |
55 | Vector2 gravity = Vector2(0, 6.0); |
56 | |
57 | Vector2 force = Vector2(0, 0); |
58 | Vector2 acceleration = Vector2(0, 0); |
59 | Vector2 velocity = Vector2(0, 0); |
60 | Vector2 last_position = Vector2(0, 0); |
61 | Vector2 dynamic_position = Vector2(0, 0); |
62 | |
63 | Vector2 last_noncollision_position = Vector2(0, 0); |
64 | }; |
65 | |
66 | Vector<Jiggle_Joint_Data2D> jiggle_data_chain; |
67 | |
68 | NodePath target_node; |
69 | ObjectID target_node_cache; |
70 | void update_target_cache(); |
71 | |
72 | float stiffness = 3; |
73 | float mass = 0.75; |
74 | float damping = 0.75; |
75 | bool use_gravity = false; |
76 | Vector2 gravity = Vector2(0, 6); |
77 | |
78 | bool use_colliders = false; |
79 | uint32_t collision_mask = 1; |
80 | |
81 | void jiggle_joint_update_bone2d_cache(int p_joint_idx); |
82 | void _execute_jiggle_joint(int p_joint_idx, Node2D *p_target, float p_delta); |
83 | void _update_jiggle_joint_data(); |
84 | |
85 | protected: |
86 | static void _bind_methods(); |
87 | bool _set(const StringName &p_path, const Variant &p_value); |
88 | bool _get(const StringName &p_path, Variant &r_ret) const; |
89 | void _get_property_list(List<PropertyInfo> *p_list) const; |
90 | |
91 | public: |
92 | void _execute(float p_delta) override; |
93 | void _setup_modification(SkeletonModificationStack2D *p_stack) override; |
94 | |
95 | void set_target_node(const NodePath &p_target_node); |
96 | NodePath get_target_node() const; |
97 | |
98 | void set_stiffness(float p_stiffness); |
99 | float get_stiffness() const; |
100 | void set_mass(float p_mass); |
101 | float get_mass() const; |
102 | void set_damping(float p_damping); |
103 | float get_damping() const; |
104 | void set_use_gravity(bool p_use_gravity); |
105 | bool get_use_gravity() const; |
106 | void set_gravity(Vector2 p_gravity); |
107 | Vector2 get_gravity() const; |
108 | |
109 | void set_use_colliders(bool p_use_colliders); |
110 | bool get_use_colliders() const; |
111 | void set_collision_mask(int p_mask); |
112 | int get_collision_mask() const; |
113 | |
114 | int get_jiggle_data_chain_length(); |
115 | void set_jiggle_data_chain_length(int p_new_length); |
116 | |
117 | void set_jiggle_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node); |
118 | NodePath get_jiggle_joint_bone2d_node(int p_joint_idx) const; |
119 | void set_jiggle_joint_bone_index(int p_joint_idx, int p_bone_idx); |
120 | int get_jiggle_joint_bone_index(int p_joint_idx) const; |
121 | |
122 | void set_jiggle_joint_override(int p_joint_idx, bool p_override); |
123 | bool get_jiggle_joint_override(int p_joint_idx) const; |
124 | void set_jiggle_joint_stiffness(int p_joint_idx, float p_stiffness); |
125 | float get_jiggle_joint_stiffness(int p_joint_idx) const; |
126 | void set_jiggle_joint_mass(int p_joint_idx, float p_mass); |
127 | float get_jiggle_joint_mass(int p_joint_idx) const; |
128 | void set_jiggle_joint_damping(int p_joint_idx, float p_damping); |
129 | float get_jiggle_joint_damping(int p_joint_idx) const; |
130 | void set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity); |
131 | bool get_jiggle_joint_use_gravity(int p_joint_idx) const; |
132 | void set_jiggle_joint_gravity(int p_joint_idx, Vector2 p_gravity); |
133 | Vector2 get_jiggle_joint_gravity(int p_joint_idx) const; |
134 | |
135 | SkeletonModification2DJiggle(); |
136 | ~SkeletonModification2DJiggle(); |
137 | }; |
138 | |
139 | #endif // SKELETON_MODIFICATION_2D_JIGGLE_H |
140 | |