extends Node2D

export (String, "bus", "police", "car") var skin
export (float) var speed = 0.125

enum { RIGHT, DOWN_RIGHT, DOWN, DOWN_LEFT, LEFT, UP_LEFT, UP, UP_RIGHT }

# TODO:
# - Move track related logic inside TrackPlayer script ?
onready var track = get_node("/root/Track2/Track/TrackPlayer/")
var position_on_track := 0.0
var target_on_track := 0.0

func _ready():
	# Set player sprite
	$CarSprite.set_animation(skin)


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
		track.set_unit_offset(position_on_track)
	else:
		position_on_track = target_on_track


func _on_TileMap_player_moved(track_offset):
	target_on_track = track_offset
