Files
ProjectMD/Assets/Scripts/Tower/ConstructionSite.cs

47 lines
1.5 KiB
C#

using UnityEngine;
using Unity.Netcode;
public class ConstructionSite : NetworkBehaviour
{
private NetworkVariable<float> _currentTimer = new NetworkVariable<float>(0f);
private NetworkVariable<int> _syncTurretIndex = new NetworkVariable<int>();
private Vector3Int _gridPos;
private float _targetBuildTime;
private bool _isCompleted = false;
public void Initialize(int index, Vector3Int pos)
{
if (!IsServer) return;
_syncTurretIndex.Value = index;
_gridPos = pos;
// BuildManager로부터 데이터 참조
_targetBuildTime = BuildManager.Instance.GetTurretData(index).buildTime;
}
void Update()
{
if (!IsServer || _isCompleted) return;
_currentTimer.Value += Time.deltaTime;
if (_currentTimer.Value >= _targetBuildTime) CompleteBuild();
}
public void AdvanceConstruction(float amount) => AdvanceBuildRpc(amount);
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
private void AdvanceBuildRpc(float amount) => _currentTimer.Value += amount;
private void CompleteBuild()
{
_isCompleted = true;
var data = BuildManager.Instance.GetTurretData(_syncTurretIndex.Value);
// 고스트가 있던 그 위치 그대로 생성
Vector3 finalPos = transform.position;
GameObject finalObj = Instantiate(data.finalPrefab, finalPos, Quaternion.identity);
finalObj.GetComponent<NetworkObject>().Spawn();
GetComponent<NetworkObject>().Despawn();
}
}