class_name PlayerControler
extends CharacterBody2D

@export var speed = 450 # How fast the player will move (pixels/sec).

# intensions of the player turner into a boolean
@export var wants_to_grab = false;

@onready var animation_tree := $AnimationTree
@onready var state_machine := animation_tree.get("parameters/HumanState/playback") as AnimationNodeStateMachinePlayback

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

func readInputs():
	wants_to_grab = Input.is_action_pressed("grab");
	if state_machine.get_current_node() == "grabing":
		velocity = Vector2(0,0);
	else:
		velocity = Input.get_vector("move_left", "move_right", "move_up", "move_down") * speed

func updateFacingDirectionInAnimationTree():
	animation_tree.set("parameters/HumanState/grabing/blend_position", last_facing_direction)
	animation_tree.set("parameters/HumanState/idling/blend_position", last_facing_direction)
	animation_tree.set("parameters/HumanState/walking/blend_position", last_facing_direction)

func _physics_process(delta):
	readInputs()

	# move the caracter
	move_and_slide()

	# compute the direction the player wants to look at
	if velocity:
		last_facing_direction = velocity.normalized()

	updateFacingDirectionInAnimationTree()

func _on_area_2d_body_entered(body: Node2D) -> void:
	print(body)
