타워 선택 건설 및 몹 웨이브 생성
Defence용 Scene 별도 생성
This commit is contained in:
@@ -26,6 +26,11 @@ public class BuildManager : MonoBehaviour
|
||||
[SerializeField] private TurretData selectedTurret; // 현재 선택된 타워 데이터
|
||||
[SerializeField] private bool isBuildMode = false;
|
||||
|
||||
[SerializeField] private LayerMask playerLayer; // 플레이어의 레이어를 지정하세요.
|
||||
|
||||
[Header("Turret Library")]
|
||||
[SerializeField] private List<TurretData> turretLibrary; // 인스펙터에서 여러 타워 등록
|
||||
|
||||
private GameObject _ghostInstance;
|
||||
private Material _ghostMaterial;
|
||||
private HashSet<Vector2Int> _occupiedNodes = new HashSet<Vector2Int>();
|
||||
@@ -52,9 +57,24 @@ public class BuildManager : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (isBuildMode)
|
||||
if (!isBuildMode || _ghostInstance == null) return;
|
||||
|
||||
Vector2 mousePos = Mouse.current.position.ReadValue();
|
||||
Ray ray = Camera.main.ScreenPointToRay(mousePos);
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, groundLayer))
|
||||
{
|
||||
UpdateGhost();
|
||||
Vector2Int gridPos = WorldToGrid(hit.point);
|
||||
_ghostInstance.transform.position = GridToWorld(gridPos, selectedTurret.size);
|
||||
|
||||
// 건설 가능 여부 실시간 체크
|
||||
bool canPlace = CanBuild(gridPos, selectedTurret.size);
|
||||
|
||||
// 고스트 색상 변경 (Material의 _Color 속성이 있다고 가정)
|
||||
if (_ghostMaterial != null)
|
||||
{
|
||||
_ghostMaterial.color = canPlace ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.5f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,9 +113,14 @@ public class BuildManager : MonoBehaviour
|
||||
|
||||
private void CreateGhost()
|
||||
{
|
||||
if (selectedTurret.ghostPrefab == null) return;
|
||||
|
||||
if (_ghostInstance != null) Destroy(_ghostInstance);
|
||||
|
||||
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
|
||||
_ghostMaterial = _ghostInstance.GetComponentInChildren<Renderer>().material;
|
||||
|
||||
// [추가] 고스트의 크기도 데이터에 맞게 조정
|
||||
_ghostInstance.transform.localScale = new Vector3(selectedTurret.size.x, 5f, selectedTurret.size.y);
|
||||
}
|
||||
|
||||
private void DestroyGhost()
|
||||
@@ -148,11 +173,15 @@ public class BuildManager : MonoBehaviour
|
||||
// 3. 토대 생성
|
||||
GameObject siteObj = Instantiate(constructionSitePrefab, GridToWorld(gridPos, data.size), Quaternion.identity);
|
||||
|
||||
// [추가] 토대의 비주얼 크기를 타워 사이즈에 맞게 조정
|
||||
// x와 z는 타워의 가로/세로 사이즈를 따르고, y(높이)는 1로 유지합니다.
|
||||
siteObj.transform.localScale = new Vector3(data.size.x, 5f, data.size.y);
|
||||
|
||||
// 4. 컴포넌트 존재 여부 체크
|
||||
ConstructionSite siteScript = siteObj.GetComponent<ConstructionSite>();
|
||||
if (siteScript != null)
|
||||
{
|
||||
siteScript.Initialize(data.finalPrefab, data.buildTime);
|
||||
siteScript.Initialize(data.finalPrefab, data.buildTime, data.size);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -167,11 +196,24 @@ public class BuildManager : MonoBehaviour
|
||||
{
|
||||
for (int x = 0; x < size.x; x++)
|
||||
for (int y = 0; y < size.y; y++)
|
||||
if (_occupiedNodes.Contains(new Vector2Int(startPos.x + x, startPos.y + y))) return false;
|
||||
return true;
|
||||
if (_occupiedNodes.Contains(new Vector2Int(startPos.x + x, startPos.y + y)))
|
||||
return false;
|
||||
|
||||
// 2. 플레이어와 겹치는지 물리적 박스 검사
|
||||
Vector3 center = GridToWorld(startPos, size);
|
||||
// 타워 높이의 절반 정도 위에서 검사 (y축 위치 보정)
|
||||
center.y = 1f;
|
||||
|
||||
// 타워 사이즈보다 아주 살짝 작게 설정하여 여유 공간 부여 (0.45f)
|
||||
Vector3 halfExtents = new Vector3(size.x * 0.45f, 0.5f, size.y * 0.45f);
|
||||
|
||||
// 해당 영역에 playerLayer를 가진 콜라이더가 있는지 확인
|
||||
bool isPlayerOverlapping = Physics.CheckBox(center, halfExtents, Quaternion.identity, playerLayer);
|
||||
|
||||
return !isPlayerOverlapping;
|
||||
}
|
||||
|
||||
private Vector2Int WorldToGrid(Vector3 worldPos) => new Vector2Int(Mathf.RoundToInt(worldPos.x / cellSize), Mathf.RoundToInt(worldPos.z / cellSize));
|
||||
private Vector2Int WorldToGrid(Vector3 worldPos) => new Vector2Int(Mathf.FloorToInt(worldPos.x / cellSize), Mathf.FloorToInt(worldPos.z / cellSize));
|
||||
|
||||
private Vector3 GridToWorld(Vector2Int gridPos, Vector2Int size)
|
||||
{
|
||||
@@ -185,4 +227,20 @@ public class BuildManager : MonoBehaviour
|
||||
for (int z = -10; z <= 10; z++)
|
||||
Gizmos.DrawWireCube(new Vector3(x * cellSize, 0, z * cellSize), new Vector3(cellSize, 0.01f, cellSize));
|
||||
}
|
||||
|
||||
// 현재 선택된 타워를 변경하는 공용 메서드
|
||||
public void SelectTurret(int index)
|
||||
{
|
||||
if (index >= 0 && index < turretLibrary.Count)
|
||||
{
|
||||
selectedTurret = turretLibrary[index];
|
||||
Debug.Log($"{selectedTurret.turretName} 선택됨!");
|
||||
|
||||
// 만약 건설 모드 중이라면 고스트도 즉시 교체
|
||||
if (isBuildMode)
|
||||
{
|
||||
CreateGhost();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,16 @@ public class ConstructionSite : MonoBehaviour
|
||||
private GameObject _finalTurretPrefab;
|
||||
private float _buildTime;
|
||||
private float _currentProgress = 0f;
|
||||
private Vector2Int _size; // 사이즈를 저장할 변수 추가
|
||||
|
||||
public void Initialize(GameObject finalPrefab, float time)
|
||||
public void Initialize(GameObject finalPrefab, float time, Vector2Int size) // 매개변수 추가)
|
||||
{
|
||||
_finalTurretPrefab = finalPrefab;
|
||||
_buildTime = time;
|
||||
_size = size; // 사이즈 저장
|
||||
|
||||
// 토대 자체의 크기 조절 (BuildManager에서 해도 되지만 여기서 하면 더 확실합니다)
|
||||
transform.localScale = new Vector3(size.x, 5f, size.y);
|
||||
|
||||
// UI 생성 및 초기화
|
||||
if (uiPrefab != null)
|
||||
@@ -49,7 +54,12 @@ public class ConstructionSite : MonoBehaviour
|
||||
|
||||
private void CompleteBuild()
|
||||
{
|
||||
Instantiate(_finalTurretPrefab, transform.position, transform.rotation);
|
||||
// 1. 실제 타워 생성
|
||||
GameObject turret = Instantiate(_finalTurretPrefab, transform.position, transform.rotation);
|
||||
|
||||
// 2. 생성된 타워의 크기를 저장해둔 사이즈로 변경!
|
||||
turret.transform.localScale = new Vector3(_size.x, 5f, _size.y);
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
@@ -4,20 +4,21 @@ using System;
|
||||
public class Core : MonoBehaviour, IDamageable
|
||||
{
|
||||
[SerializeField] private float maxHealth = 100f;
|
||||
private float _currentHealth;
|
||||
[SerializeField] private float currentHealth = 100f;
|
||||
private float CurrentHealth;
|
||||
|
||||
// 체력이 변경될 때 UI 등에 알리기 위한 이벤트 (Observer 패턴)
|
||||
public static event Action<float> OnHealthChanged;
|
||||
public static event Action OnCoreDestroyed;
|
||||
|
||||
void Awake() => _currentHealth = maxHealth;
|
||||
void Awake() => currentHealth = maxHealth;
|
||||
|
||||
public void TakeDamage(float amount)
|
||||
{
|
||||
_currentHealth -= amount;
|
||||
OnHealthChanged?.Invoke(_currentHealth / maxHealth);
|
||||
currentHealth -= amount;
|
||||
OnHealthChanged?.Invoke(currentHealth / maxHealth);
|
||||
|
||||
if (_currentHealth <= 0)
|
||||
if (currentHealth <= 0)
|
||||
OnCoreDestroyed?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,15 @@ using System;
|
||||
public class Gate : MonoBehaviour, IDamageable
|
||||
{
|
||||
[SerializeField] private float maxHealth = 50f;
|
||||
private float _currentHealth;
|
||||
[SerializeField] private float currentHealth = 50f;
|
||||
private float CurrentHealth;
|
||||
|
||||
void Awake() => _currentHealth = maxHealth;
|
||||
void Awake() => currentHealth = maxHealth;
|
||||
|
||||
public void TakeDamage(float amount)
|
||||
{
|
||||
_currentHealth -= amount;
|
||||
if (_currentHealth <= 0)
|
||||
currentHealth -= amount;
|
||||
if (currentHealth <= 0)
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
@@ -32,30 +32,25 @@ public class WaveManager : MonoBehaviour
|
||||
|
||||
IEnumerator StartWaveRoutine()
|
||||
{
|
||||
// 모든 웨이브를 순회
|
||||
// 실행하는 오브젝트의 이름과 고유 ID를 출력
|
||||
Debug.Log($"[{gameObject.name} : {gameObject.GetInstanceID()}] 코루틴 가동 시작. 총 웨이브: {waves.Count}");
|
||||
|
||||
while (_currentWaveIndex < waves.Count)
|
||||
{
|
||||
Wave currentWave = waves[_currentWaveIndex];
|
||||
Debug.Log($"현재 인덱스: {_currentWaveIndex}, 웨이브명: {currentWave.waveName}");
|
||||
|
||||
Debug.Log($"Wave {_currentWaveIndex + 1}: {currentWave.waveName} 시작!");
|
||||
|
||||
// 1. 적 소환 로직
|
||||
for (int i = 0; i < currentWave.count; i++)
|
||||
{
|
||||
SpawnEnemy(currentWave.enemyPrefab);
|
||||
|
||||
// 지정된 간격만큼 대기 (이게 코루틴의 핵심입니다)
|
||||
yield return new WaitForSeconds(currentWave.spawnRate);
|
||||
}
|
||||
|
||||
// 2. 다음 웨이브 전까지 대기
|
||||
Debug.Log("웨이브 종료. 다음 웨이브 대기 중...");
|
||||
_currentWaveIndex++; // 여기서 인덱스를 올림
|
||||
yield return new WaitForSeconds(timeBetweenWaves);
|
||||
|
||||
_currentWaveIndex++;
|
||||
}
|
||||
|
||||
Debug.Log("모든 웨이브가 종료되었습니다!");
|
||||
Debug.Log($"[{gameObject.name}] 모든 웨이브 종료");
|
||||
}
|
||||
|
||||
void SpawnEnemy(GameObject enemyPrefab)
|
||||
|
||||
Reference in New Issue
Block a user