extends Node2D

signal attack_finished
signal direction_changed(new_direction)

enum States {IDLE, MOVE, ATTACK, DEAD}

var state = null
var direction = 0
var is_attacking = false


onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get("parameters/playback")


func _ready() -> void:
	animationTree.active = true


func attack() -> void:
	animationState.travel("Attack")
	if not is_attacking:
		is_attacking = true


func change_direction(new_direction: Vector2) -> void:
	direction = new_direction
	animationTree.set("parameters/Idle/blend_position", direction)
	animationTree.set("parameters/Walk/blend_position", direction)
	animationTree.set("parameters/Attack/blend_position", direction)
	emit_signal("direction_changed", new_direction)


func change_state(new_state) -> void:
	state = new_state
	match state:
		States.MOVE:
			animationState.travel("Walk")
			
		States.IDLE:
			animationState.travel("Idle")
		States.ATTACK:
			attack()


func attack_animation_finished() -> void:
	emit_signal("attack_finished")
	is_attacking = false

