class_name BeastIKTarget extends Marker3D

const CRITICAL_ANGLE := 0.1

@export var step_target: Node3D

@export var neighbor: BeastIKTarget
@export var diagonal: BeastIKTarget

var stepping := false
var _resting := false

@onready var parent := owner as GrunkBeast


func step() -> void:
	# Tween a rough arc
	var up := parent.basis.y
	var midpoint := 0.5 * (global_position + step_target.global_position) + up * parent.step_height
	var time := 0.5 * parent.step_time

	stepping = true

	# TODO: tween this?
	global_basis = step_target.global_basis

	var tween := create_tween()
	tween.tween_property(self, "global_position", midpoint, time)
	tween.tween_property(self, "global_position", step_target.global_position, time)
	tween.tween_callback(_end_step)


func _end_step() -> void:
	stepping = false
	_resting = true


func _process(_delta: float) -> void:
	if _resting:
		_resting = false
		return

	if (
		!stepping
		and !neighbor.stepping
		and global_position.distance_to(step_target.global_position) >= parent.step_distance
	):
		step()
		diagonal.step()
