Enhancement: Expected behavior achieved
This commit is contained in:
@@ -4,41 +4,67 @@ class_name SpaceNode
|
|||||||
var initial_pos: Vector2
|
var initial_pos: Vector2
|
||||||
var offset: Vector2
|
var offset: Vector2
|
||||||
var draggable: bool = false
|
var draggable: bool = false
|
||||||
|
var velocity: float = 0
|
||||||
|
|
||||||
|
var mouse_first_location: Vector2
|
||||||
|
var mouse_release_location: Vector2
|
||||||
|
var time_since_first_location: float = -1
|
||||||
|
|
||||||
|
@export var velocity_coeffecient: int = 500
|
||||||
|
@export var max_x: int = 1000
|
||||||
|
@export var min_x: int = 0
|
||||||
|
@export var ease_out_time: float = 0.5
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
draggable = true
|
draggable = true
|
||||||
return
|
return
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
global_position.x += velocity
|
||||||
|
|
||||||
if draggable:
|
if draggable:
|
||||||
|
if (Input.is_action_just_pressed("click")):
|
||||||
|
mouse_first_location = get_global_mouse_position()
|
||||||
|
time_since_first_location = 0
|
||||||
|
|
||||||
if Input.is_action_just_pressed("click"):
|
if Input.is_action_pressed("click"):
|
||||||
initial_pos = global_position
|
time_since_first_location += delta
|
||||||
return
|
|
||||||
|
|
||||||
if Input.is_action_pressed("click", true):
|
elif Input.is_action_just_released("click"):
|
||||||
is_dragging.is_dragging = true
|
time_since_first_location += delta
|
||||||
offset = global_position - get_global_mouse_position()
|
mouse_release_location = get_global_mouse_position()
|
||||||
global_position = Vector2(offset.x, initial_pos.y)
|
if mouse_release_location.x < mouse_first_location.x:
|
||||||
|
var distance = mouse_release_location.x - mouse_first_location.x
|
||||||
elif Input.is_action_just_released("click") && is_dragging.is_dragging:
|
velocity = (distance /
|
||||||
is_dragging.is_dragging = false
|
(velocity_coeffecient * time_since_first_location))
|
||||||
var tween = get_tree().create_tween()
|
|
||||||
tween.tween_property(self, "global_position", Vector2(get_global_mouse_position().x, initial_pos.y), 0.5).set_ease(Tween.EASE_OUT)
|
|
||||||
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
|
|
||||||
func reset_draggable():
|
|
||||||
if not is_dragging.is_dragging:
|
|
||||||
is_dragging.is_dragging = false
|
|
||||||
draggable = false
|
draggable = false
|
||||||
|
|
||||||
|
elif mouse_release_location.x > mouse_first_location.x:
|
||||||
|
var distance = abs(mouse_first_location.x
|
||||||
|
- mouse_release_location.x)
|
||||||
|
velocity = (distance /
|
||||||
|
(velocity_coeffecient * time_since_first_location))
|
||||||
|
draggable = false
|
||||||
|
|
||||||
func _on_mouse_exited() -> void:
|
elif not draggable:
|
||||||
reset_draggable()
|
var tween = get_tree().create_tween()
|
||||||
|
tween.tween_property(self, "velocity", 0, ease_out_time).set_ease(Tween.EASE_OUT)
|
||||||
|
mouse_first_location = Vector2(0,0)
|
||||||
func _on_mouse_entered() -> void:
|
mouse_release_location = Vector2(0,0)
|
||||||
if not is_dragging.is_dragging:
|
|
||||||
draggable = true
|
draggable = true
|
||||||
|
|
||||||
|
if global_position.x > max_x:
|
||||||
|
push_warning("too far to the pos_x")
|
||||||
|
velocity = 0
|
||||||
|
global_position.x = 999
|
||||||
|
time_since_first_location = -1
|
||||||
|
draggable = true
|
||||||
|
return
|
||||||
|
|
||||||
|
elif global_position.x < min_x:
|
||||||
|
push_warning("too far to the neg_y")
|
||||||
|
velocity = 0
|
||||||
|
global_position.x = 1
|
||||||
|
time_since_first_location = -1
|
||||||
|
draggable = true
|
||||||
|
return
|
||||||
|
|||||||
@@ -23,6 +23,4 @@ offset_top = 299.0
|
|||||||
offset_right = 455.0
|
offset_right = 455.0
|
||||||
offset_bottom = 299.0
|
offset_bottom = 299.0
|
||||||
script = ExtResource("2_mej84")
|
script = ExtResource("2_mej84")
|
||||||
|
velocity_coeffecient = 735
|
||||||
[connection signal="mouse_entered" from="Sliding Box/EmptySpaceThing" to="Sliding Box/EmptySpaceThing" method="_on_mouse_entered"]
|
|
||||||
[connection signal="mouse_exited" from="Sliding Box/EmptySpaceThing" to="Sliding Box/EmptySpaceThing" method="_on_mouse_exited"]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user