class_name GuardPath
extends Path2D

@onready var pathfollow : PathFollow2D = $PathFollow2D
@export var watchpoint_positions : Array[Node2D]

func _ready() -> void:
	if not curve:
		push_error("No curve defined!")
		assert(false)

func get_next_watchpoint(position_global : Vector2):
	if watchpoint_positions.is_empty():
		return null
	var current_progress = curve.get_closest_offset(to_local(position_global))
	var min_progress = watchpoint_positions[0] #if next progress already in overrun
	for point : Node2D in watchpoint_positions:
		if curve.get_closest_offset(to_local(point.global_position)) >= current_progress:
			return point
		if curve.get_closest_offset(to_local(point.global_position)) < curve.get_closest_offset(to_local(min_progress.global_position)) :
			min_progress = point
	return min_progress
