터널 제약 조건 추가
다른 터널과 반드시 이어질 것
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI; // UI 사용을 위해 추가
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ConstructionSite : MonoBehaviour
|
||||
{
|
||||
[Header("UI Settings")]
|
||||
[SerializeField] private GameObject uiPrefab; // 방금 만든 Canvas 프리팹
|
||||
[SerializeField] private GameObject uiPrefab; // Canvas 프리팹
|
||||
[SerializeField] private Vector3 uiOffset = new Vector3(0, 2f, 0); // 머리 위 높이
|
||||
|
||||
private Slider _progressSlider;
|
||||
private GameObject _finalTurretPrefab;
|
||||
private GameObject _finalPrefab; // 이름을 _finalPrefab으로 통일하여 에러 방지
|
||||
private float _buildTime;
|
||||
private float _currentProgress = 0f;
|
||||
private Vector2Int _size; // 사이즈를 저장할 변수 추가
|
||||
private Vector2Int _size;
|
||||
|
||||
public void Initialize(GameObject finalPrefab, float time, Vector2Int size) // 매개변수 추가)
|
||||
public void Initialize(GameObject finalPrefab, float time, Vector2Int size)
|
||||
{
|
||||
_finalTurretPrefab = finalPrefab;
|
||||
_finalPrefab = finalPrefab;
|
||||
_buildTime = time;
|
||||
_size = size; // 사이즈 저장
|
||||
_size = size;
|
||||
|
||||
// UI 생성 및 초기화
|
||||
if (uiPrefab != null)
|
||||
@@ -26,7 +26,6 @@ public class ConstructionSite : MonoBehaviour
|
||||
_progressSlider = uiObj.GetComponentInChildren<Slider>();
|
||||
if (_progressSlider != null)
|
||||
{
|
||||
_progressSlider.transform.localScale = Vector3.one; // 슬라이더 크기 조절
|
||||
_progressSlider.maxValue = 1f;
|
||||
_progressSlider.value = 0f;
|
||||
}
|
||||
@@ -36,7 +35,7 @@ public class ConstructionSite : MonoBehaviour
|
||||
public void AdvanceConstruction(float amount)
|
||||
{
|
||||
_currentProgress += amount;
|
||||
float ratio = _currentProgress / _buildTime;
|
||||
float ratio = Mathf.Clamp01(_currentProgress / _buildTime);
|
||||
|
||||
// 슬라이더 업데이트
|
||||
if (_progressSlider != null)
|
||||
@@ -52,24 +51,46 @@ public class ConstructionSite : MonoBehaviour
|
||||
|
||||
private void CompleteBuild()
|
||||
{
|
||||
// 1. 실제 타워 생성
|
||||
GameObject finalTurret = Instantiate(_finalPrefab, transform.position, Quaternion.identity);
|
||||
BuildManager.Instance.AlignToGround(finalTurret, 0f);
|
||||
|
||||
// [추가] 새로 생긴 터널의 노드들에게 주변 연결을 찾으라고 명령
|
||||
TunnelNode[] newNodes = finalTurret.GetComponentsInChildren<TunnelNode>();
|
||||
foreach (var node in newNodes)
|
||||
// 2. BuildManager를 통한 지면 정렬 (스케일 반영을 위해 호출)
|
||||
if (BuildManager.Instance != null)
|
||||
{
|
||||
node.FindNeighborNode();
|
||||
BuildManager.Instance.AlignToGround(finalTurret, 0f);
|
||||
}
|
||||
|
||||
// [추가] 주변에 있던 기존 노드들도 새 노드를 찾도록 주변 검색 실행
|
||||
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();
|
||||
}
|
||||
// 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} 터널 연결 갱신 완료");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user