96 lines
3.2 KiB
C#
96 lines
3.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ConstructionSite : MonoBehaviour
|
|
{
|
|
[Header("UI Settings")]
|
|
[SerializeField] private GameObject uiPrefab; // Canvas 프리팹
|
|
[SerializeField] private Vector3 uiOffset = new Vector3(0, 2f, 0); // 머리 위 높이
|
|
|
|
private Slider _progressSlider;
|
|
private GameObject _finalPrefab; // 이름을 _finalPrefab으로 통일하여 에러 방지
|
|
private float _buildTime;
|
|
private float _currentProgress = 0f;
|
|
private Vector2Int _size;
|
|
|
|
public void Initialize(GameObject finalPrefab, float time, Vector2Int size)
|
|
{
|
|
_finalPrefab = finalPrefab;
|
|
_buildTime = time;
|
|
_size = size;
|
|
|
|
// UI 생성 및 초기화
|
|
if (uiPrefab != null)
|
|
{
|
|
GameObject uiObj = Instantiate(uiPrefab, transform.position + uiOffset, Quaternion.identity, transform);
|
|
_progressSlider = uiObj.GetComponentInChildren<Slider>();
|
|
if (_progressSlider != null)
|
|
{
|
|
_progressSlider.maxValue = 1f;
|
|
_progressSlider.value = 0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AdvanceConstruction(float amount)
|
|
{
|
|
_currentProgress += amount;
|
|
float ratio = Mathf.Clamp01(_currentProgress / _buildTime);
|
|
|
|
// 슬라이더 업데이트
|
|
if (_progressSlider != null)
|
|
{
|
|
_progressSlider.value = ratio;
|
|
}
|
|
|
|
if (_currentProgress >= _buildTime)
|
|
{
|
|
CompleteBuild();
|
|
}
|
|
}
|
|
|
|
private void CompleteBuild()
|
|
{
|
|
// 1. 실제 타워 생성
|
|
GameObject finalTurret = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
|
|
|
// 2. BuildManager를 통한 지면 정렬 (스케일 반영을 위해 호출)
|
|
if (BuildManager.Instance != null)
|
|
{
|
|
BuildManager.Instance.AlignToGround(finalTurret, 0f);
|
|
}
|
|
|
|
// 3. 터널 노드 연결 갱신 (터널인 경우에만 작동)
|
|
UpdateTunnelConnections(finalTurret);
|
|
|
|
// 4. 토대 제거
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void UpdateTunnelConnections(GameObject newTurret)
|
|
{
|
|
// 3-1. 새로 생성된 타워 내부의 모든 TunnelNode를 찾아 주변 탐색 실행
|
|
TunnelNode[] newNodes = newTurret.GetComponentsInChildren<TunnelNode>();
|
|
if (newNodes.Length > 0)
|
|
{
|
|
foreach (var node in newNodes)
|
|
{
|
|
node.FindNeighborNode();
|
|
}
|
|
|
|
// 3-2. 주변에 이미 설치된 다른 터널 노드들도 새로운 터널을 인식하도록 재탐색 명령
|
|
// 5f는 격자 크기에 맞춰 적절히 조절 (일반적으로 격자 한 칸 이상이면 충분)
|
|
Collider[] neighbors = Physics.OverlapSphere(transform.position, 5f, LayerMask.GetMask("Tunnel"));
|
|
foreach (var col in neighbors)
|
|
{
|
|
TunnelNode neighborNode = col.GetComponentInParent<TunnelNode>();
|
|
if (neighborNode != null && !System.Array.Exists(newNodes, x => x == neighborNode))
|
|
{
|
|
neighborNode.FindNeighborNode();
|
|
}
|
|
}
|
|
|
|
Debug.Log($"[Construction] {newTurret.name} 터널 연결 갱신 완료");
|
|
}
|
|
}
|
|
} |