extends Sequence
class_name SequenceWalkBezierCurve

@export var _curve : BezierCurve
@export var _start_time : int  # in milliseconds
@export var _end_time : int # in milliseconds

static func apply_to_node(node : Node, curve : BezierCurve, time_to_destination : float = 1.0, play_immediately : bool = true) -> SequenceWalkBezierCurve:

	var seq = SequenceWalkBezierCurve.new()

	node.add_child(seq)
	seq.set_path(curve, time_to_destination)
	
	if play_immediately:
		seq.play()


	return seq

func set_path(pathToFollow : BezierCurve, time_to_destination : float) -> SequenceWalkBezierCurve:
	_curve = pathToFollow
	_start_time = Time.get_ticks_msec()
	_end_time = _start_time + int(time_to_destination * 1000)

	# go to start of path
	_update_position_on_path(0)
	
	return self

# Called every frame. 'delta' is the elapsed time since the previous frame.
func advance_sequence(_delta: float) -> Sequence:
	if _curve == null:
		return self

	var t = float(Time.get_ticks_msec() - _start_time) / float(_end_time - _start_time)

	_update_position_on_path(t)

	if t >= 1:
		complete_sequence()

	return self

func _update_position_on_path(t : float) -> void:
	if _curve == null:
		return

	get_parent().position = _curve.eval2D(t)
