# PlayerMovement.gd
extends Resource
class_name PlayerMovement

var player: CharacterBody2D
var animated_sprite: AnimatedSprite2D
var speed: float
var last_direction: Vector2 = Vector2.ZERO

func process(_delta):
  # Get input direction
	var direction = Vector2.ZERO
 
	if Input.is_action_pressed("move_right"):
		direction.x += 1
	if Input.is_action_pressed("move_left"):
		direction.x -= 1
	if Input.is_action_pressed("move_down"):
		direction.y += 1
	if Input.is_action_pressed("move_up"):
		direction.y -= 1
 
  # Normalize the direction
	if direction.length() > 0:
		direction = direction.normalized()
		last_direction = direction
 
  # Set velocity
	player.velocity = direction * speed
 
  # Move the character
	player.move_and_slide()
 
  # Update animation
	update_animation(direction)

func update_animation(direction):
	var anim_name = "idle" # Default animation

	if animated_sprite.animation == "throw":
		return # Don't change animation if throwing
 
	if direction == Vector2.ZERO:
	# Character is idle
		anim_name = "idle"
	else:
		# Character is moving
		anim_name = "walk"
 
	if animated_sprite.animation != anim_name:
		animated_sprite.play(anim_name)
