extends StateMachine

onready var anim_tree = $"../FlyerSprite/AnimationTree"
onready var anim_state_playback = $"../FlyerSprite/AnimationTree".get("parameters/playback")

func _ready() -> void:
	add_state("searching")
	add_state("hunting")
	add_state("fleeing")
	add_state("sleeping")
	state = states.searching
	set_state(states.searching)
	print(self.state)
	for state in states:
		if state_matching_method_exists(state):
			continue
		else:
			printerr("StateMachine -> State: " + state + " has no matching method in parent.")
			push_error("StateMachine -> State: " + state + " has no matching method in parent.")
		

# Game logic consequences of state
func _state_logic(delta): 
	var state_action_ref = funcref(parent, self.state)
	parent.path_direction = state_action_ref.call_func()
	parent.execute_movement(delta)


func _get_transition(_delta):
	parent.get_node("StateLabel").text = self.state
	_animation_logic()
	var new_state
	if parent.target == null:
		new_state = states.searching
	if parent.target != null:
		new_state = states.hunting
	if parent.is_hurt:
		new_state = states.sleeping
	if new_state != self.state:
		return new_state
	return null


func _enter_state(_new_state, _previous_state):
	pass


func _exit_state(_previous_state, _new_state):
	pass


func _animation_logic():
	_set_blendspaces_direction()
	if self.state == "searching":
		anim_state_playback.travel("searching")
	else:
		anim_state_playback.travel("hunting")


func _set_blendspaces_direction():
	var value = parent.get_facing_direction()
	anim_tree.set("parameters/searching/blend_position", value)
	anim_tree.set("parameters/hunting/blend_position", value)
