extends Node

@onready var sprite = get_parent()
@export var x_max = 10.0
@export var y_max = 10.0
@export var stop_threshold = 0.1
@export var tween_duration = 0.05
@export var recovery_factor = 8.0/9
@export var transition_type = Tween.TRANS_SINE

#@onready var tween = Tween.new()
@onready var rand = RandomNumberGenerator.new()
signal tween_completed

func start_increase():
	var x = stop_threshold
	var y = stop_threshold
	while x < x_max and y < y_max:
		var offset = Vector2(rand.randf_range(-x, x), rand.randf_range(-y, y))
		sprite.offset = offset
		if is_inside_tree():
			await get_tree().create_timer(tween_duration).timeout 
		x/=recovery_factor
		y/=recovery_factor
	tween_completed.emit()

func start_decrease(threshold = stop_threshold):
	var x = x_max
	var y = y_max
	while x > threshold and y > threshold:
		var offset = Vector2(rand.randf_range(-x, x), rand.randf_range(-y, y))
		sprite.offset = offset
		if is_inside_tree():
			await get_tree().create_timer(tween_duration).timeout 
		x*=recovery_factor
		y*=recovery_factor
	tween_completed.emit()
	if threshold != stop_threshold:
		start_steady(threshold, threshold)
	
func start_steady(x, y):
	while true:
		var offset = Vector2(rand.randf_range(-x, x), rand.randf_range(-y, y))
		sprite.offset = offset
		if is_inside_tree():
			await get_tree().create_timer(tween_duration).timeout 
