@tool
extends Node3D
class_name MechaLeg

const STEP_DURATION = 0.15
signal step

@export_node_path("Node3D") var step_target_path : NodePath
@onready var local_target : Node3D = %LocalTarget

@export var step_distance: float = 5

@export var can_step = true

func _process(_delta):
    if step_target_path and can_step:
        var step_target : Node3D = get_node(step_target_path) as Node3D
        var dist := step_target.global_position.distance_to(local_target.global_position)
        if dist > step_distance:
            step.emit()
            tween_step(get_node(step_target_path).global_position)


func tween_step(target_pos: Vector3):
    var tween = create_tween().set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_OUT)

    tween.set_parallel(true)
    tween.tween_property(local_target, "global_position:x", target_pos.x, STEP_DURATION * 0.7)
    tween.tween_property(local_target, "global_position:z", target_pos.z, STEP_DURATION * 0.7)
    tween.tween_property(local_target, "global_position:y", target_pos.y + 1.5, STEP_DURATION * 0.5)
    tween.set_parallel(false)
    tween.tween_property(local_target, "global_position:y", target_pos.y, STEP_DURATION * 0.3)