터널 연장 로직 개선

This commit is contained in:
2026-01-15 22:46:40 +09:00
parent 2e57fe09ce
commit 394bbe64a2
10 changed files with 935 additions and 618 deletions

View File

@@ -1,96 +1,56 @@
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 GameObject _finalPrefab;
private float _buildTime;
private float _currentProgress = 0f;
private Vector2Int _size;
private float _timer;
private Vector3Int _gridPos;
private bool _isCompleted = false; // 중복 완공 방지 플래그
public void Initialize(GameObject finalPrefab, float time, Vector2Int size)
public void Initialize(GameObject final, float time, Vector3Int pos)
{
_finalPrefab = finalPrefab;
_finalPrefab = final;
_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;
}
}
_gridPos = pos;
_timer = 0;
_isCompleted = false;
}
void Update()
{
if (_isCompleted) return;
// 매 프레임 자동으로 시간이 흐름
_timer += Time.deltaTime;
if (_timer >= _buildTime) Complete();
}
// [핵심] 플레이어가 상호작용 버튼을 꾹 누를 때 호출되는 함수
public void AdvanceConstruction(float amount)
{
_currentProgress += amount;
float ratio = Mathf.Clamp01(_currentProgress / _buildTime);
if (_isCompleted) return;
// 슬라이더 업데이트
if (_progressSlider != null)
{
_progressSlider.value = ratio;
}
if (_currentProgress >= _buildTime)
{
CompleteBuild();
}
_timer += amount;
if (_timer >= _buildTime) Complete();
}
private void CompleteBuild()
private void Complete()
{
// 1. 실제 타워 생성
GameObject finalTurret = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
// 1. 터널 생성
GameObject tunnel = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
TunnelNode node = tunnel.GetComponentInChildren<TunnelNode>();
// 2. BuildManager를 통한 지면 정렬 (스케일 반영을 위해 호출)
if (BuildManager.Instance != null)
if (node != null)
{
BuildManager.Instance.AlignToGround(finalTurret, 0f);
// 2. [수정] 완공된 터널의 루트 위치로 좌표 계산
Vector3Int myGridPos = BuildManager.Instance.WorldToGrid3D(tunnel.transform.position);
// 3. 레지스트리 등록 및 연결
BuildManager.Instance.RegisterTunnel(myGridPos, node);
node.LinkVertical();
}
// 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} 터널 연결 갱신 완료");
}
}
}