extends Node

@onready var leftEngine: Sprite2D = $MotorIzquierdo
@onready var rightEngine: Sprite2D = $MotorDerecho

var leftPowerLevel: float
var currentLeftPowerLevel: float
var rightPowerLevel: float
var currentRightPowerLevel: float

@export var leftPowerSprites: Array[Texture2D]
@export var rightPowerSprites: Array[Texture2D]

@export_range(0, 2) var transitionDelta: float


func _transition(curr: float, goal: float, delta: float) -> float:
	return curr + (goal - curr) * transitionDelta * delta


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	currentLeftPowerLevel = _transition(currentLeftPowerLevel, leftPowerLevel, delta)
	currentRightPowerLevel = _transition(currentRightPowerLevel, rightPowerLevel, delta)
	
	if not leftPowerSprites.is_empty():
		var index = roundf(currentLeftPowerLevel * leftPowerSprites.size())
		index = clamp(index, 0, leftPowerSprites.size() - 1)
		leftEngine.texture = leftPowerSprites[index]
	if not rightPowerSprites.is_empty():
		var index = roundf(currentRightPowerLevel * rightPowerSprites.size())
		index = clamp(index, 0, rightPowerSprites.size() - 1)
		rightEngine.texture = rightPowerSprites[index]


func _change_left_power_level(level: float):
	leftPowerLevel = level


func _change_right_power_level(level: float):
	rightPowerLevel = level


func _on_left_engine_off():
	_change_left_power_level(0)
func _on_left_engine_on():
	_change_left_power_level(1)
func _on_right_engine_off():
	_change_right_power_level(0)
func _on_right_engine_on():
	_change_right_power_level(1)
