extends Camera3D

@export var target : Node3D
@export var delay : float
@export var easing : float
@export var waypoints : Array[Node3D]

var target_pos
var max_time
var t
var factor

# Called when the node enters the scene tree for the first time.
func _ready():
	t = $Timer
	t.autostart = false
	t.wait_time = delay
	target_pos = target.position
	t.start()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if len(waypoints) == 0:
		pass
	else:
		
		var wp1 = waypoints[0].position
		var wp2 = waypoints[1].position
		#var ratio = wp2.distance_to( target.position )/wp1.distance_to(wp2)
		var ratio = target.position.distance_to(wp2) / ( target.position.distance_to(wp1) + target.position.distance_to(wp2) )
		position = lerp( wp2 , wp1 ,  ratio )
	
	#if len(waypoints)<2:
		#pass
	#elif len(waypoints) == 1:
		#pass
	#else:
		#var wp1 = waypoints[0].position
		#var wp2 = waypoints[1].position
		##var ratio = wp2.distance_to( target.position )/wp1.distance_to(wp2)
		#var ratio = target.position.distance_to(wp2) / ( target.position.distance_to(wp1) + target.position.distance_to(wp2) )
		#position = lerp( wp2 , wp1 ,  ratio )
		
		
		for i in waypoints:
			pass
			#print( i , " name:" + i.name , " pos:" + str(target.position.distance_to(i.position)) )
		
	if target_pos != target.position:
		factor = t.time_left / t.wait_time 
		print( t.wait_time, factor )
	
	look_at( lerp( target.position , target_pos , pow( factor, easing )  ) + Vector3(0.0,0.70,0.0) )


func _on_timer_timeout():
	target_pos = target.position
	t.start()
