터널 연장 로직 개선

This commit is contained in:
2026-01-15 22:46:40 +09:00
parent 2e57fe09ce
commit 394bbe64a2
10 changed files with 935 additions and 618 deletions

View File

@@ -2,41 +2,64 @@ using UnityEngine;
public class TunnelNode : MonoBehaviour, IInteractable
{
public TunnelNode connectedNode;
public TunnelNode otherEndOfThisSegment;
public float detectionRadius = 0.5f;
public LayerMask tunnelLayer;
public TunnelNode aboveNode;
public TunnelNode belowNode;
// [중요] 이 노드가 속한 터널의 최상단 부모를 캐싱합니다.
private Transform _tunnelRoot;
void Awake()
{
// 이름이 "Tunnel"로 시작하는 부모를 찾거나, 단순히 부모의 부모를 참조합니다.
// 여기서는 이미지 구조에 맞게 부모(Visual)의 부모(Tunnel (1))를 찾습니다.
_tunnelRoot = transform.parent.parent;
}
public void LinkVertical()
{
// 루트(Tunnel 1)의 위치로 내 위치 파악
Transform myRoot = transform.parent.parent;
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(myRoot.position);
// 위/아래 터널 찾기
TunnelNode targetAbove = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.up);
TunnelNode targetBelow = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.down);
if (targetAbove != null)
{
this.aboveNode = targetAbove;
targetAbove.belowNode = this; // 상대방의 아래도 나로 설정 (양방향)
}
if (targetBelow != null)
{
this.belowNode = targetBelow;
targetBelow.aboveNode = this; // 상대방의 위도 나로 설정 (양방향)
}
}
private void OnDrawGizmos()
{
// 연결되었다면 Scene 뷰에서 선을 그림
if (aboveNode != null)
{
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, aboveNode.transform.position);
Gizmos.DrawSphere(aboveNode.transform.position, 0.2f);
}
if (belowNode != null)
{
Gizmos.color = Color.cyan;
Gizmos.DrawLine(transform.position, belowNode.transform.position);
Gizmos.DrawSphere(belowNode.transform.position, 0.2f);
}
}
// --- IInteractable 구현부 ---
public void Interact(GameObject user)
{
// 상호작용한 플레이어의 TunnelTraveler를 찾아 이동 시작!
TunnelTraveler traveler = user.GetComponent<TunnelTraveler>();
if (traveler != null)
{
traveler.StartTravel(this);
}
var traveler = user.GetComponent<TunnelTraveler>();
if (traveler && !traveler.IsTraveling) traveler.StartTravel(this);
}
public string GetInteractionText()
{
return "통로 진입";
}
// ----------------------------
void Start() => FindNeighborNode();
public void FindNeighborNode()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, detectionRadius, tunnelLayer);
foreach (var col in colliders)
{
TunnelNode neighbor = col.GetComponent<TunnelNode>();
if (neighbor != null && neighbor != this && neighbor != otherEndOfThisSegment)
{
connectedNode = neighbor;
break;
}
}
}
public string GetInteractionText() => "터널 이용 [E]";
}