extends PathFollow2D

export (float) var speed = 0.125

var position_on_track := 0.0
var target_on_track := 0.0

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


func _process(delta):
	# Move the player until target_on_track
	if target_on_track - position_on_track > 0.01:
		# Speed up car as the target_on_track goes further
		var speed_mod = int(clamp((target_on_track - position_on_track) / speed, 1.0, 4.0))
		position_on_track += speed * delta * speed_mod
		set_unit_offset(position_on_track)
	else:
		position_on_track = target_on_track


func _on_RaceTrack_player_moved(track_offset):
	target_on_track = track_offset