11@tool
2- extends Marker2D
2+ extends BaseRopeTool2D
33class_name RopeHandle
44
55## Can be used to control, animate or fixate points on a target rope.
66
77## Gets emitted just before applying the position. This happens always during _physics_process().
88signal on_before_update ()
99
10- ## Enable or disable
11- @export var enable : bool = true : get = get_enable , set = set_enable
12- ## Target rope node.
13- @export_node_path ("Rope" ) var rope_path : NodePath : set = set_rope_path
14- ## Position on the rope between 0 and 1.
15- @export_range (0.0 , 1.0 ) var rope_position : float = 1.0 : set = set_rope_position
10+
1611## Whether to smoothly snap to RopeHandle's position instead of instantly.
1712@export var smoothing : bool = false
13+
1814## Smoothing speed
1915@export var position_smoothing_speed : float = 0.5
20- ## If false, only affect the nearest vertex on the rope. Otherwise, affect both surrounding points when applicable.
21- @export var precise : bool = false
16+
2217## Determines how much the target point is allowed to move. A value of 0.0 sets the point's position
2318## but it is still fully affected by simulation and constraining.
2419## A value of 1.0 completely fixates the point at the handle's position and allows no further movement.
2520@export_range (0.0 , 1.0 ) var strength : float = 0.0 : set = set_strength
2621
27- var _helper : RopeToolHelper
2822var _target_idx : int = 0
2923
3024
3125func _init () -> void :
32- if not _helper :
33- _helper = RopeToolHelper .new (RopeToolHelper .UPDATE_HOOK_PRE , self , "_on_pre_update" )
34- _helper .on_rope_assigned .connect (_on_rope_assigned )
35- add_child (_helper )
36-
37-
38- func _ready () -> void :
39- set_rope_path (rope_path )
40- set_enable (enable )
26+ super ._init (RopeToolHelper .new (RopeToolHelper .UPDATE_HOOK_PRE , self , "_on_pre_update" ))
27+ _helper .on_rope_assigned .connect (_on_rope_assigned )
4128
4229
4330func _enter_tree () -> void :
@@ -50,6 +37,10 @@ func _exit_tree() -> void:
5037
5138func _on_pre_update () -> void :
5239 on_before_update .emit ()
40+ _update ()
41+
42+
43+ func _update () -> void :
5344 var rope : Rope = _helper .target_rope
5445
5546 # The point weight is set here instead of _update_state() to ensure that handles do not
@@ -78,28 +69,13 @@ func _move_point(idx: int, from: Vector2, to: Vector2) -> void:
7869 _helper .target_rope .set_point (idx , to )
7970
8071
81- func set_rope_path (value : NodePath ) -> void :
82- rope_path = value
83-
84- if is_inside_tree ():
85- _helper .set_target_rope_path (rope_path , self )
86-
87-
8872func set_enable (value : bool ) -> void :
89- if enable == value :
90- return
73+ super .set_enable (value )
9174
92- enable = value
93- _helper .enable = value
94-
95- if not enable :
96- _restore_state (_helper .target_rope )
97- else :
75+ if enable :
9876 _update_state (null )
99-
100-
101- func get_enable () -> bool :
102- return _helper .enable
77+ else :
78+ _restore_state (_helper .target_rope )
10379
10480
10581func set_strength (value : float ) -> void :
@@ -108,25 +84,10 @@ func set_strength(value: float) -> void:
10884
10985
11086func set_rope_position (value : float ) -> void :
111- rope_position = value
87+ super . set_rope_position ( value )
11288 _update_state_current_rope ()
11389
11490
115- ## Determine the nearest position on the rope and use it as [member RopeHandle.rope_position].
116- func use_nearest_position () -> void :
117- use_nearest_position_to_point (global_position )
118-
119-
120- ## Determine the nearest position on the rope to the given point and use it as [member RopeHandle.rope_position].
121- func use_nearest_position_to_point (point : Vector2 ) -> void :
122- var rope := _helper .target_rope
123- if rope :
124- # TODO: Determine precise percentage, not just nearest index
125- var idx := rope .get_nearest_point_index (point )
126- var perc := rope .get_point_perc (idx )
127- rope_position = perc
128-
129-
13091func _on_rope_assigned (old : Rope ) -> void :
13192 _update_state (old )
13293
@@ -137,6 +98,10 @@ func _on_rope_assigned(old: Rope) -> void:
13798 _helper .target_rope .on_point_count_changed .connect (_on_point_count_changed )
13899
139100
101+ func _on_point_count_changed () -> void :
102+ _update_state_current_rope ()
103+
104+
140105func _update_state_current_rope () -> void :
141106 _update_state (_helper .target_rope )
142107
@@ -156,7 +121,3 @@ func _update_state(old_rope: Rope) -> void:
156121func _restore_state (rope : Rope ) -> void :
157122 if rope :
158123 rope .set_point_simulation_weight (_target_idx , 1.0 )
159-
160-
161- func _on_point_count_changed () -> void :
162- _update_state_current_rope ()
0 commit comments