@tool
class_name SetNavTarget extends ActionLeaf
## Set the nav agent target position to the global position at the given blackboard key.
##
## The target value may be a Vector3 position in space,
## or a Node3D, in which case the global position of the node will be used as the target position.
##
## Returns failure if there is no valid target under the given blackboard key.

## Blackboard key of the Node3D to set as a target
@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 value: Variant = blackboard.get_value(blackboard_key)
	if value is Vector3:
		@warning_ignore("unsafe_cast")
		nav_agent.target_position = value as Vector3
	elif value is Node3D and is_instance_valid(value):
		@warning_ignore("unsafe_cast")
		nav_agent.target_position = (value as Node3D).global_position
	else:
		return FAILURE
	return SUCCESS
