멀티플레이어 지원
이동, 건설, 인터랙션, 공격 등
This commit is contained in:
@@ -1,56 +1,45 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class ConstructionSite : MonoBehaviour
|
||||
public class ConstructionSite : NetworkBehaviour
|
||||
{
|
||||
private GameObject _finalPrefab;
|
||||
private float _buildTime;
|
||||
private float _timer;
|
||||
private NetworkVariable<float> _currentTimer = new NetworkVariable<float>(0f);
|
||||
private NetworkVariable<int> _syncTurretIndex = new NetworkVariable<int>();
|
||||
private Vector3Int _gridPos;
|
||||
private bool _isCompleted = false; // 중복 완공 방지 플래그
|
||||
private float _targetBuildTime;
|
||||
private bool _isCompleted = false;
|
||||
|
||||
public void Initialize(GameObject final, float time, Vector3Int pos)
|
||||
public void Initialize(int index, Vector3Int pos)
|
||||
{
|
||||
_finalPrefab = final;
|
||||
_buildTime = time;
|
||||
if (!IsServer) return;
|
||||
_syncTurretIndex.Value = index;
|
||||
_gridPos = pos;
|
||||
_timer = 0;
|
||||
_isCompleted = false;
|
||||
_targetBuildTime = BuildManager.Instance.GetTurretData(index).buildTime;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_isCompleted) return;
|
||||
if (!IsServer || _isCompleted) return;
|
||||
|
||||
// 매 프레임 자동으로 시간이 흐름
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer >= _buildTime) Complete();
|
||||
_currentTimer.Value += Time.deltaTime;
|
||||
if (_currentTimer.Value >= _targetBuildTime) CompleteBuild();
|
||||
}
|
||||
|
||||
// [핵심] 플레이어가 상호작용 버튼을 꾹 누를 때 호출되는 함수
|
||||
public void AdvanceConstruction(float amount)
|
||||
public void AdvanceConstruction(float amount) => AdvanceBuildRpc(amount);
|
||||
|
||||
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
|
||||
private void AdvanceBuildRpc(float amount) => _currentTimer.Value += amount;
|
||||
|
||||
private void CompleteBuild()
|
||||
{
|
||||
if (_isCompleted) return;
|
||||
_isCompleted = true;
|
||||
var data = BuildManager.Instance.GetTurretData(_syncTurretIndex.Value);
|
||||
|
||||
_timer += amount;
|
||||
if (_timer >= _buildTime) Complete();
|
||||
}
|
||||
// [중요] 저장된 그리드 좌표로부터 정확한 월드 좌표 복원
|
||||
Vector3 finalPos = BuildManager.Instance.GridToWorld(_gridPos);
|
||||
GameObject finalObj = Instantiate(data.finalPrefab, finalPos, Quaternion.identity);
|
||||
|
||||
private void Complete()
|
||||
{
|
||||
// 1. 터널 생성
|
||||
GameObject tunnel = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
||||
TunnelNode node = tunnel.GetComponentInChildren<TunnelNode>();
|
||||
|
||||
if (node != null)
|
||||
{
|
||||
// 2. [수정] 완공된 터널의 루트 위치로 좌표 계산
|
||||
Vector3Int myGridPos = BuildManager.Instance.WorldToGrid3D(tunnel.transform.position);
|
||||
|
||||
// 3. 레지스트리 등록 및 연결
|
||||
BuildManager.Instance.RegisterTunnel(myGridPos, node);
|
||||
node.LinkVertical();
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
finalObj.GetComponent<NetworkObject>().Spawn();
|
||||
GetComponent<NetworkObject>().Despawn();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user