@tool
class_name PursueTarget extends ActionLeaf
## Wait until the navigation target is reached (or cannot navigate further)
##
## This action will update the target's position each tick, allowing for tracking moving targets.
## Returns FAILURE if there isn't a Node3D at the given blackboard key.

@export var blackboard_key := "target"

@onready var nav_agent: NavigationAgent3D = %NavAgent


func tick(actor: Node, blackboard: Blackboard) -> int:
	# Bug workaround -- do not update nav target while inside a link!
	var beast := actor as GrunkBeast
	if beast.traversing_link:
		return RUNNING

	var target: Variant = blackboard.get_value(blackboard_key)
	if target is Vector3:
		@warning_ignore("unsafe_cast")
		nav_agent.target_position = target as Vector3
	elif target is Node3D and is_instance_valid(target):
		@warning_ignore("unsafe_cast")
		nav_agent.target_position = (target as Node3D).global_position
	else:
		return FAILURE

	return SUCCESS if nav_agent.is_navigation_finished() else RUNNING
