멀티플레이어 지원
이동, 건설, 인터랙션, 공격 등
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.EventSystems;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class BuildManager : MonoBehaviour
|
||||
public class BuildManager : NetworkBehaviour
|
||||
{
|
||||
public static BuildManager Instance;
|
||||
|
||||
@@ -11,113 +12,81 @@ public class BuildManager : MonoBehaviour
|
||||
public struct TurretData
|
||||
{
|
||||
public string turretName;
|
||||
public bool isTunnel;
|
||||
public GameObject finalPrefab;
|
||||
public GameObject ghostPrefab;
|
||||
public GameObject finalPrefab; // NetworkObject 필수
|
||||
public GameObject ghostPrefab; // 로컬 프리뷰용
|
||||
public float buildTime;
|
||||
public Vector2Int size;
|
||||
}
|
||||
|
||||
[Header("Settings")]
|
||||
[Header("Grid Settings")]
|
||||
public float cellSize = 1f;
|
||||
public float tunnelHeight = 3f;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[SerializeField] private float yOffset = 0.5f; // 터널 중심점 오프셋
|
||||
|
||||
[Header("Prefabs & Layers")]
|
||||
[SerializeField] private GameObject constructionSitePrefab;
|
||||
[SerializeField] private List<TurretData> turretLibrary = new List<TurretData>();
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[SerializeField] private LayerMask playerLayer;
|
||||
|
||||
[Header("Turret Library")]
|
||||
[SerializeField] private List<TurretData> turretLibrary;
|
||||
[SerializeField] private TurretData selectedTurret;
|
||||
|
||||
private GameObject _ghostInstance;
|
||||
private Material _ghostMaterial;
|
||||
private int _selectedTurretIndex = 0;
|
||||
private bool _isBuildMode = false;
|
||||
public bool IsBuildMode => _isBuildMode;
|
||||
private GameObject _ghostInstance;
|
||||
private Vector3Int _currentGridPos;
|
||||
private PlayerInputActions _inputActions;
|
||||
|
||||
// 좌표 레지스트리 (물리 탐색 대체)
|
||||
// 데이터 레지스트리 (다른 코드에서 참조)
|
||||
private Dictionary<Vector3Int, TunnelNode> _tunnelRegistry = new Dictionary<Vector3Int, TunnelNode>();
|
||||
private HashSet<Vector3Int> _occupiedNodes = new HashSet<Vector3Int>();
|
||||
|
||||
private PlayerInputActions _inputActions;
|
||||
private Vector3Int _currentGridPos;
|
||||
private float _currentY;
|
||||
|
||||
|
||||
// 게임 시작 시 기존 터널들 등록 (반드시 필요!)
|
||||
void Start()
|
||||
{
|
||||
// 1. 씬에 배치된 모든 터널을 찾아 등록하고 연결합니다.
|
||||
RegisterAllExistingTunnels();
|
||||
}
|
||||
|
||||
public void RegisterTunnel(Vector3Int pos, TunnelNode node)
|
||||
{
|
||||
if (!_tunnelRegistry.ContainsKey(pos))
|
||||
{
|
||||
_tunnelRegistry.Add(pos, node);
|
||||
_occupiedNodes.Add(pos);
|
||||
Debug.Log($"<color=green>[Registry]</color> {pos} 좌표에 {node.name} 등록 완료");
|
||||
}
|
||||
}
|
||||
|
||||
// 씬에 이미 배치된 터널들을 등록하는 함수 (Start에서 호출)
|
||||
public void RegisterAllExistingTunnels()
|
||||
{
|
||||
_tunnelRegistry.Clear();
|
||||
_occupiedNodes.Clear();
|
||||
|
||||
TunnelNode[] nodes = FindObjectsByType<TunnelNode>(FindObjectsSortMode.None);
|
||||
HashSet<Transform> roots = new HashSet<Transform>();
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
Transform root = node.transform.parent.parent; // Tunnel (1)
|
||||
if (!roots.Contains(root))
|
||||
{
|
||||
Vector3Int pos = WorldToGrid3D(root.position);
|
||||
RegisterTunnel(pos, node);
|
||||
roots.Add(root);
|
||||
}
|
||||
}
|
||||
|
||||
// 모든 등록이 끝난 후 '동시에' 연결
|
||||
foreach (var node in nodes) node.LinkVertical();
|
||||
}
|
||||
|
||||
// [핵심 수정] 노드의 위치가 아닌, 터널 부모의 위치를 넣어도 정확한 격자가 나오도록 함
|
||||
public Vector3Int WorldToGrid3D(Vector3 worldPos)
|
||||
{
|
||||
int x = Mathf.FloorToInt(worldPos.x / cellSize);
|
||||
int z = Mathf.FloorToInt(worldPos.z / cellSize);
|
||||
// Y값은 tunnelHeight로 나눈 뒤 반올림(Round)하여 오차 극복
|
||||
int y = Mathf.RoundToInt(worldPos.y / tunnelHeight);
|
||||
return new Vector3Int(x, y, z);
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
if (Instance == null) Instance = this;
|
||||
else Destroy(gameObject);
|
||||
|
||||
_inputActions = new PlayerInputActions();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
// 최신 Input System 콜백 등록
|
||||
// 최신 Action-based 이벤트 바인딩
|
||||
_inputActions.Player.ToggleBuild.performed += ctx => ToggleBuildMode();
|
||||
_inputActions.Player.Build.performed += ctx => OnBuildRequested();
|
||||
_inputActions.Player.Cancel.performed += ctx => ExitBuildMode();
|
||||
_inputActions.Player.ToggleBuild.performed += ctx => ToggleBuildMode();
|
||||
_inputActions.Enable();
|
||||
}
|
||||
|
||||
void OnDisable() => _inputActions.Disable();
|
||||
// 숫자키 슬롯 선택 (Input Action 에셋 설정에 따라 Select1, Select2... 등 사용)
|
||||
_inputActions.Player.Select1.performed += ctx => SelectTurret(0);
|
||||
_inputActions.Player.Select2.performed += ctx => SelectTurret(1);
|
||||
_inputActions.Player.Select3.performed += ctx => SelectTurret(2);
|
||||
|
||||
_inputActions.Enable();
|
||||
|
||||
// 시작 시 씬에 배치된 터널 자동 등록
|
||||
RegisterAllExistingTunnels();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!_isBuildMode || _ghostInstance == null) return;
|
||||
|
||||
UpdateGhostPosition();
|
||||
}
|
||||
|
||||
#region Selection & Build Logic
|
||||
|
||||
public void SelectTurret(int index)
|
||||
{
|
||||
if (index < 0 || index >= turretLibrary.Count) return;
|
||||
|
||||
_selectedTurretIndex = index;
|
||||
Debug.Log($"타워 선택: {turretLibrary[_selectedTurretIndex].turretName}");
|
||||
|
||||
if (_isBuildMode)
|
||||
{
|
||||
if (_ghostInstance != null) Destroy(_ghostInstance);
|
||||
_ghostInstance = Instantiate(turretLibrary[_selectedTurretIndex].ghostPrefab);
|
||||
_ghostInstance.transform.position = GridToWorld(_currentGridPos);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateGhostPosition()
|
||||
{
|
||||
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
@@ -125,61 +94,112 @@ public class BuildManager : MonoBehaviour
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer | tunnelMask))
|
||||
{
|
||||
Vector2Int xz = WorldToGrid(hit.point);
|
||||
float targetY = 0.05f;
|
||||
int floor = 0;
|
||||
Vector3Int gridPos = WorldToGrid3D(hit.point);
|
||||
|
||||
// 터널 조준 시 지하로 스냅
|
||||
// 터널 조준 시 해당 터널의 한 칸 아래로 스냅
|
||||
if (((1 << hit.collider.gameObject.layer) & tunnelMask) != 0)
|
||||
{
|
||||
xz = WorldToGrid(hit.collider.transform.position);
|
||||
targetY = hit.collider.transform.position.y - tunnelHeight;
|
||||
floor = Mathf.RoundToInt(targetY / tunnelHeight);
|
||||
gridPos = WorldToGrid3D(hit.collider.transform.position) + Vector3Int.down;
|
||||
}
|
||||
|
||||
_currentGridPos = new Vector3Int(xz.x, floor, xz.y);
|
||||
_currentY = targetY;
|
||||
|
||||
_ghostInstance.transform.position = new Vector3(xz.x * cellSize, targetY, xz.y * cellSize);
|
||||
|
||||
bool canBuild = CanBuildVertical(_currentGridPos);
|
||||
_ghostMaterial.color = canBuild ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.5f);
|
||||
_currentGridPos = gridPos;
|
||||
_ghostInstance.transform.position = GridToWorld(gridPos);
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanBuildVertical(Vector3Int pos)
|
||||
{
|
||||
if (_occupiedNodes.Contains(pos)) return false;
|
||||
// 지하 건설 시 위층 터널 존재 여부 확인
|
||||
if (pos.y < 0 && !_occupiedNodes.Contains(pos + Vector3Int.up)) return false;
|
||||
|
||||
// 플레이어와 겹치는지 확인
|
||||
return !Physics.CheckBox(new Vector3(pos.x * cellSize, pos.y * tunnelHeight + 1f, pos.z * cellSize),
|
||||
new Vector3(0.45f, 0.5f, 0.45f), Quaternion.identity, playerLayer);
|
||||
}
|
||||
|
||||
private void OnBuildRequested()
|
||||
{
|
||||
if (!_isBuildMode || EventSystem.current.IsPointerOverGameObject()) return;
|
||||
if (!CanBuildVertical(_currentGridPos)) return;
|
||||
|
||||
_occupiedNodes.Add(_currentGridPos);
|
||||
GameObject site = Instantiate(constructionSitePrefab, _ghostInstance.transform.position, Quaternion.identity);
|
||||
site.GetComponent<ConstructionSite>().Initialize(selectedTurret.finalPrefab, selectedTurret.buildTime, _currentGridPos);
|
||||
// 서버에 건설 가능 여부 확인 및 생성 요청
|
||||
RequestBuildRpc(_selectedTurretIndex, _currentGridPos);
|
||||
|
||||
ExitBuildMode();
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
private void RequestBuildRpc(int index, Vector3Int gridPos)
|
||||
{
|
||||
// 서버 측 중복 점유 확인
|
||||
if (_occupiedNodes.Contains(gridPos)) return;
|
||||
|
||||
Vector3 spawnPos = GridToWorld(gridPos);
|
||||
|
||||
// 1. 토대 생성 및 네트워크 스폰
|
||||
GameObject siteObj = Instantiate(constructionSitePrefab, spawnPos, Quaternion.identity);
|
||||
siteObj.GetComponent<NetworkObject>().Spawn();
|
||||
|
||||
// 2. 토대 데이터 초기화
|
||||
ConstructionSite site = siteObj.GetComponent<ConstructionSite>();
|
||||
if (site != null)
|
||||
{
|
||||
site.Initialize(index, gridPos);
|
||||
}
|
||||
|
||||
_occupiedNodes.Add(gridPos);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Functions (External References)
|
||||
|
||||
// ConstructionSite에서 프리팹 정보를 가져갈 때 사용
|
||||
public TurretData GetTurretData(int index) => turretLibrary[index];
|
||||
|
||||
// TunnelNode가 스폰될 때 자신을 등록하기 위해 사용
|
||||
public void RegisterTunnel(Vector3Int pos, TunnelNode node)
|
||||
{
|
||||
if (!_tunnelRegistry.ContainsKey(pos))
|
||||
{
|
||||
_tunnelRegistry.Add(pos, node);
|
||||
_occupiedNodes.Add(pos); // 건설된 구역으로 마킹
|
||||
}
|
||||
}
|
||||
|
||||
// TunnelNode가 위아래 노드를 찾기 위해 사용
|
||||
public TunnelNode GetTunnelAt(Vector3Int pos) => _tunnelRegistry.GetValueOrDefault(pos);
|
||||
|
||||
public Vector2Int WorldToGrid(Vector3 pos) => new Vector2Int(Mathf.FloorToInt(pos.x / cellSize), Mathf.FloorToInt(pos.z / cellSize));
|
||||
// 좌표 변환: 월드 -> 격자 인덱스 (0.5 오프셋 반영)
|
||||
public Vector3Int WorldToGrid3D(Vector3 worldPos)
|
||||
{
|
||||
return new Vector3Int(
|
||||
Mathf.FloorToInt(worldPos.x / cellSize),
|
||||
Mathf.RoundToInt((worldPos.y - yOffset) / tunnelHeight),
|
||||
Mathf.FloorToInt(worldPos.z / cellSize)
|
||||
);
|
||||
}
|
||||
|
||||
private void ToggleBuildMode() { if (_isBuildMode) ExitBuildMode(); else EnterBuildMode(); }
|
||||
// 좌표 변환: 격자 인덱스 -> 월드 (0.5 오프셋 반영)
|
||||
public Vector3 GridToWorld(Vector3Int gridPos)
|
||||
{
|
||||
return new Vector3(
|
||||
gridPos.x * cellSize,
|
||||
(gridPos.y * tunnelHeight) + yOffset,
|
||||
gridPos.z * cellSize
|
||||
);
|
||||
}
|
||||
|
||||
// 씬에 미리 배치된 터널들을 한꺼번에 등록
|
||||
public void RegisterAllExistingTunnels()
|
||||
{
|
||||
TunnelNode[] nodes = FindObjectsByType<TunnelNode>(FindObjectsSortMode.None);
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
Vector3Int pos = WorldToGrid3D(node.transform.position);
|
||||
RegisterTunnel(pos, node);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Mode Switching
|
||||
|
||||
public void ToggleBuildMode() { if (_isBuildMode) ExitBuildMode(); else EnterBuildMode(); }
|
||||
private void EnterBuildMode()
|
||||
{
|
||||
if (turretLibrary.Count == 0) return;
|
||||
_isBuildMode = true;
|
||||
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
|
||||
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
|
||||
_ghostInstance = Instantiate(turretLibrary[_selectedTurretIndex].ghostPrefab);
|
||||
}
|
||||
private void ExitBuildMode()
|
||||
{
|
||||
@@ -187,27 +207,7 @@ public class BuildManager : MonoBehaviour
|
||||
if (_ghostInstance) Destroy(_ghostInstance);
|
||||
}
|
||||
|
||||
// BuildManager.cs 내부
|
||||
public void SelectTurret(int index)
|
||||
{
|
||||
if (index >= 0 && index < turretLibrary.Count)
|
||||
{
|
||||
selectedTurret = turretLibrary[index];
|
||||
#endregion
|
||||
|
||||
// 현재 건설 모드가 아니라면 건설 모드로 진입
|
||||
if (!_isBuildMode)
|
||||
{
|
||||
EnterBuildMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 이미 건설 모드라면 고스트만 교체
|
||||
if (_ghostInstance) Destroy(_ghostInstance);
|
||||
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
|
||||
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
|
||||
}
|
||||
|
||||
Debug.Log($"{selectedTurret.turretName} 선택됨");
|
||||
}
|
||||
}
|
||||
public override void OnNetworkDespawn() => _inputActions.Disable();
|
||||
}
|
||||
Reference in New Issue
Block a user