maze-generator-assets-lib/stick/Knob.gd

39 lines
1.1 KiB
GDScript3
Raw Normal View History

2025-02-28 17:13:53 +01:00
extends Sprite2D
@onready var parent = $".."
var pressing = false
@export var maxLength = 50
var deadzone = 15
func _ready():
2025-02-28 23:05:41 +01:00
#deadzone = parent.deadzone
maxLength *= parent.scale.x
2025-02-28 17:13:53 +01:00
func _process(delta):
2025-02-28 23:05:41 +01:00
if pressing:
if get_global_mouse_position().distance_to(parent.global_position) <= maxLength:
global_position = get_global_mouse_position()
else:
var angle = parent.global_position.angle_to_point(get_global_mouse_position())
global_position.x = parent.global_position.x + cos(angle)*maxLength
global_position.y = parent.global_position.y + sin(angle)*maxLength
calculateVector()
else:
global_position = lerp(global_position, parent.global_position, delta*50)
parent.posVector = Vector2(0,0)
2025-02-28 17:13:53 +01:00
func calculateVector():
2025-02-28 23:05:41 +01:00
if abs((global_position.x - parent.global_position.x)) >= deadzone:
parent.posVector.x = (global_position.x - parent.global_position.x)/maxLength
if abs((global_position.y - parent.global_position.y)) >= deadzone:
parent.posVector.y = (global_position.y - parent.global_position.y)/maxLength
2025-02-28 17:13:53 +01:00
func _on_button_button_down():
2025-02-28 23:05:41 +01:00
pressing = true
2025-02-28 17:13:53 +01:00
func _on_button_button_up():
2025-02-28 23:05:41 +01:00
pressing = false