멀티플레이어 지원

이동, 건설, 인터랙션, 공격 등
This commit is contained in:
2026-01-16 19:30:26 +09:00
parent 5d37aedc93
commit d6292b6879
36 changed files with 1967 additions and 492 deletions

View File

@@ -1,58 +1,34 @@
using UnityEngine;
using Unity.Netcode;
public class TunnelNode : MonoBehaviour, IInteractable
public class TunnelNode : NetworkBehaviour, IInteractable
{
public TunnelNode aboveNode;
public TunnelNode belowNode;
// [중요] 이 노드가 속한 터널의 최상단 부모를 캐싱합니다.
private Transform _tunnelRoot;
void Awake()
public override void OnNetworkSpawn()
{
// 이름이 "Tunnel"로 시작하는 부모를 찾거나, 단순히 부모의 부모를 참조합니다.
// 여기서는 이미지 구조에 맞게 부모(Visual)의 부모(Tunnel (1))를 찾습니다.
_tunnelRoot = transform.parent.parent;
// 모든 클라이언트에서 각자 장부에 등록
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(transform.position);
BuildManager.Instance.RegisterTunnel(myPos, this);
// 모든 노드가 등록될 시간을 벌기 위해 서버에서 약간 지연 후 연결 명령
if (IsServer) Invoke(nameof(SyncLinks), 0.2f);
}
private void SyncLinks() => LinkVerticalRpc();
[Rpc(SendTo.Everyone)]
private void LinkVerticalRpc() => LinkVertical();
public void LinkVertical()
{
// 루트(Tunnel 1)의 위치로 내 위치 파악
Transform myRoot = transform.parent.parent;
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(myRoot.position);
Vector3Int myPos = BuildManager.Instance.WorldToGrid3D(transform.position);
aboveNode = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.up);
belowNode = BuildManager.Instance.GetTunnelAt(myPos + Vector3Int.down);
// 위/아래 터널 찾기
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);
}
if (aboveNode != null) aboveNode.belowNode = this;
if (belowNode != null) belowNode.aboveNode = this;
}
public void Interact(GameObject user)