class_name Car
extends CharacterBody2D

@export var speed = 750 # How fast the car will move (pixels/sec).
@onready var animation_tree := $AnimationTree
@onready var state_machine := animation_tree.get("parameters/driving/playback") as AnimationNodeStateMachinePlayback

var last_facing_direction = Vector2(0,-1) # facing north

var targetPosition = null

func moveTo(position: Vector2) -> void:
	targetPosition = position;

func updateFacingDirectionInAnimationTree():
	animation_tree.set("parameters/CarStates/driving/blend_position", last_facing_direction)
	animation_tree.set("parameters/CarStates/idling/blend_position", last_facing_direction)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if (targetPosition != null):
		velocity = (targetPosition - position) * speed * delta;
	move_and_slide()
	if velocity:
		last_facing_direction = velocity.normalized()

	updateFacingDirectionInAnimationTree()
