50 lines
1.3 KiB
GDScript
50 lines
1.3 KiB
GDScript
extends Area2D
|
|
|
|
@export var active := false
|
|
@export var rotation_step_degrees := 10.0
|
|
@export var target_rotation_degrees := 0.0
|
|
|
|
@onready var sfx_gear = $"../../SfxGear" as AudioStreamPlayer
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if not active:
|
|
return
|
|
if event.is_action("up"):
|
|
# rotate the lock counterclockwise
|
|
rotate_lock(-1)
|
|
elif event.is_action("down"):
|
|
# rotate the lock clockwise
|
|
rotate_lock(1)
|
|
|
|
|
|
var last_direction = 0
|
|
var current_angular_velocity = 0.0
|
|
|
|
|
|
func init_rotation(deg: float) -> void:
|
|
rotation = deg_to_rad(deg)
|
|
target_rotation_degrees = deg
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var diff = target_rotation_degrees - rotation_degrees
|
|
if abs(diff) > 0.1:
|
|
var direction = sign(diff)
|
|
var target_angular_velocity = direction * 50.0
|
|
current_angular_velocity = lerp(current_angular_velocity, target_angular_velocity, 0.1)
|
|
rotation_degrees += current_angular_velocity * delta
|
|
if active and not sfx_gear.playing:
|
|
sfx_gear.play()
|
|
elif active and sfx_gear.playing:
|
|
sfx_gear.stop()
|
|
|
|
|
|
func rotate_lock(direction: int) -> void:
|
|
var new_target = snappedf(
|
|
rotation_degrees + direction * rotation_step_degrees, rotation_step_degrees
|
|
)
|
|
if abs(new_target - target_rotation_degrees) > rotation_step_degrees * 1.2:
|
|
return
|
|
target_rotation_degrees = new_target
|