타워 기능 추가 및 개선
This commit is contained in:
66
Assets/Scripts/Tower/ConstructionSite.cs
Normal file
66
Assets/Scripts/Tower/ConstructionSite.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
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; // 사이즈 저장
|
||||
|
||||
// 토대 자체의 크기 조절 (BuildManager에서 해도 되지만 여기서 하면 더 확실합니다)
|
||||
transform.localScale = new Vector3(size.x, 1f, size.y);
|
||||
|
||||
// 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()
|
||||
{
|
||||
// 1. 실제 타워 생성
|
||||
GameObject turret = Instantiate(_finalTurretPrefab, transform.position, transform.rotation);
|
||||
|
||||
// 2. 생성된 타워의 크기를 저장해둔 사이즈로 변경!
|
||||
turret.transform.localScale = new Vector3(_size.x, 1f, _size.y);
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user