75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI; // 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 _finalTurretPrefab;
|
|
private float _buildTime;
|
|
private float _currentProgress = 0f;
|
|
private Vector2Int _size; // 사이즈를 저장할 변수 추가
|
|
|
|
public void Initialize(GameObject finalPrefab, float time, Vector2Int size) // 매개변수 추가)
|
|
{
|
|
_finalTurretPrefab = 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.transform.localScale = Vector3.one; // 슬라이더 크기 조절
|
|
_progressSlider.maxValue = 1f;
|
|
_progressSlider.value = 0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AdvanceConstruction(float amount)
|
|
{
|
|
_currentProgress += amount;
|
|
float ratio = _currentProgress / _buildTime;
|
|
|
|
// 슬라이더 업데이트
|
|
if (_progressSlider != null)
|
|
{
|
|
_progressSlider.value = ratio;
|
|
}
|
|
|
|
if (_currentProgress >= _buildTime)
|
|
{
|
|
CompleteBuild();
|
|
}
|
|
}
|
|
|
|
private void CompleteBuild()
|
|
{
|
|
GameObject finalTurret = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
|
BuildManager.Instance.AlignToGround(finalTurret, 0f);
|
|
|
|
// [추가] 새로 생긴 터널의 노드들에게 주변 연결을 찾으라고 명령
|
|
TunnelNode[] newNodes = finalTurret.GetComponentsInChildren<TunnelNode>();
|
|
foreach (var node in newNodes)
|
|
{
|
|
node.FindNeighborNode();
|
|
}
|
|
|
|
// [추가] 주변에 있던 기존 노드들도 새 노드를 찾도록 주변 검색 실행
|
|
Collider[] neighbors = Physics.OverlapSphere(transform.position, 5f, LayerMask.GetMask("Tunnel"));
|
|
foreach (var col in neighbors)
|
|
{
|
|
TunnelNode neighborNode = col.GetComponent<TunnelNode>();
|
|
if (neighborNode != null) neighborNode.FindNeighborNode();
|
|
}
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
} |