65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using UnityEngine;
|
|
|
|
public class TunnelNode : MonoBehaviour, IInteractable
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
public void Interact(GameObject user)
|
|
{
|
|
var traveler = user.GetComponent<TunnelTraveler>();
|
|
if (traveler && !traveler.IsTraveling) traveler.StartTravel(this);
|
|
}
|
|
|
|
public string GetInteractionText() => "터널 이용 [E]";
|
|
} |