extends CharacterBody2D

const SPEED = 400.0

var direction := Vector2.ZERO
var lastdir = "down"

@onready var sprite = $AnimatedSprite2D

func _physics_process(delta: float) -> void:
	direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")

	if direction != Vector2.ZERO:
		velocity = direction * SPEED
		sprite.play("marche_" + lastdir)
		match direction:
			Vector2.UP:
				lastdir = "up"
			Vector2.DOWN:
				lastdir = "down"
			Vector2.RIGHT:
				lastdir = "right"
			Vector2.LEFT:
				lastdir = "left"
		
	else:
		velocity = Vector2.ZERO
		sprite.play("idle_" + lastdir)

	move_and_slide()
