using UnityEngine;
using System.Collections;

public class TunnelTraveler : MonoBehaviour
{
    public float travelSpeed = 20f;
    private bool _isTraveling;
    public bool IsTraveling => _isTraveling;

    public void StartTravel(Vector3 targetPos, bool isBottom)
    {
        if (_isTraveling) return;

        // 하단 노드가 목적지라면 캐릭터 중심 좌표를 고려해 약간 더 아래(바닥)로 설정
        Vector3 finalDestination = isBottom ? targetPos + Vector3.down * 0.5f : targetPos;
        StartCoroutine(TravelRoutine(finalDestination));
    }

    private IEnumerator TravelRoutine(Vector3 destination)
    {
        _isTraveling = true;
        var cc = GetComponent<CharacterController>();
        if (cc) cc.enabled = false;

        // X, Z는 유지한 채 Y축 중심으로 이동
        float halfHeight = cc ? cc.height / 2f : 0f;
        Vector3 target = new Vector3(destination.x, destination.y - halfHeight, destination.z);

        while (Vector3.Distance(transform.position, target) > 0.05f)
        {
            transform.position = Vector3.MoveTowards(transform.position, target, travelSpeed * Time.deltaTime);
            yield return null;
        }

        transform.position = target;
        if (cc) cc.enabled = true;
        _isTraveling = false;
    }
}