126 lines
3.5 KiB
C#
Raw Normal View History

2025-08-22 20:43:18 +05:00
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
2025-08-22 20:43:18 +05:00
public class Level1 : Level
{
2025-08-22 21:40:50 +05:00
[Header("Wave Flow")]
public WaveSpawner spawner; // Assign in Inspector
[Min(0.05f)] public float checkInterval = 0.25f;
2025-08-22 20:43:18 +05:00
private bool _running;
private Coroutine _monitorCo;
2025-08-22 21:40:50 +05:00
// Optional: simple counters for UI hooks
private int _currentWaveIdx = -1;
private int _totalWaves = 0;
private int _currentAlive = 0;
private void OnEnable()
{
if (spawner != null)
2025-08-22 20:43:18 +05:00
{
2025-08-22 21:40:50 +05:00
spawner.OnWaveStarted += HandleWaveStarted;
spawner.OnWaveCompleted += HandleWaveCompleted;
spawner.OnAllWavesCompleted += HandleAllWavesCompleted;
spawner.OnAliveCountChanged += HandleAliveChanged;
2025-08-22 20:43:18 +05:00
}
2025-08-22 21:40:50 +05:00
}
2025-08-22 20:43:18 +05:00
2025-08-22 21:40:50 +05:00
private void OnDisable()
{
if (spawner != null)
2025-08-22 20:43:18 +05:00
{
2025-08-22 21:40:50 +05:00
spawner.OnWaveStarted -= HandleWaveStarted;
spawner.OnWaveCompleted -= HandleWaveCompleted;
spawner.OnAllWavesCompleted -= HandleAllWavesCompleted;
spawner.OnAliveCountChanged -= HandleAliveChanged;
2025-08-22 20:43:18 +05:00
}
2025-08-22 21:40:50 +05:00
}
public override void OnLevelStart()
{
base.OnLevelStart();
2025-08-22 20:43:18 +05:00
2025-08-22 21:40:50 +05:00
if (spawner == null)
2025-08-22 20:43:18 +05:00
{
2025-08-22 21:40:50 +05:00
Debug.LogError("[Level1] No WaveSpawner assigned.");
CompleteLevelImmediate();
2025-08-22 20:43:18 +05:00
return;
}
2025-08-22 21:40:50 +05:00
_totalWaves = spawner.TotalWaves;
2025-08-22 20:43:18 +05:00
_running = true;
2025-08-22 21:40:50 +05:00
// Kick off waves
spawner.StartSpawning();
// As an extra safeguard, monitor completion state
_monitorCo = StartCoroutine(MonitorForCompletion());
Debug.Log("[Level1] Level started, waves spawning…");
2025-08-22 20:43:18 +05:00
}
public override void OnLevelEnd()
{
2025-08-22 20:43:18 +05:00
base.OnLevelEnd();
_running = false;
2025-08-22 21:40:50 +05:00
2025-08-22 20:43:18 +05:00
if (_monitorCo != null)
{
StopCoroutine(_monitorCo);
_monitorCo = null;
}
}
2025-08-22 21:40:50 +05:00
private IEnumerator MonitorForCompletion()
2025-08-22 20:43:18 +05:00
{
var wait = new WaitForSeconds(checkInterval);
while (_running)
{
2025-08-22 21:40:50 +05:00
// Complete when spawner reports all waves completed AND no alive remain
if (spawner.AllWavesCompleted && spawner.CurrentAlive == 0)
2025-08-22 20:43:18 +05:00
{
2025-08-22 21:40:50 +05:00
CompleteLevelImmediate();
2025-08-22 20:43:18 +05:00
yield break;
}
yield return wait;
}
}
2025-08-22 21:40:50 +05:00
private void CompleteLevelImmediate()
2025-08-22 20:43:18 +05:00
{
_running = false;
2025-08-22 21:40:50 +05:00
Debug.Log("[Level1] All waves cleared. Level complete!");
2025-08-22 20:43:18 +05:00
if (GameplayManager.Instance != null)
GameplayManager.Instance.CompleteCurrentLevel();
else
gameObject.SetActive(false);
2025-08-22 21:40:50 +05:00
}
// ===== Event handlers (useful for UI/logs/hook-ins) =====
private void HandleWaveStarted(int waveIdx, WaveSpawner.Wave wave)
{
_currentWaveIdx = waveIdx;
Debug.Log($"[Level1] Wave {waveIdx + 1}/{_totalWaves} started. Count={wave.count}, Rate={wave.spawnRate:F2}/s");
// TODO: Update UI: “Wave X/Y starting”
}
private void HandleWaveCompleted(int waveIdx)
{
Debug.Log($"[Level1] Wave {waveIdx + 1}/{_totalWaves} completed.");
// TODO: UI “Wave complete”
}
private void HandleAllWavesCompleted()
{
Debug.Log("[Level1] All waves spawned. Waiting for last enemies to die…");
// Completion is finalized in MonitorForCompletion once alive == 0
}
private void HandleAliveChanged(int currentAlive)
{
_currentAlive = currentAlive;
// TODO: UI “Enemies Left: currentAlive”
2025-08-22 20:43:18 +05:00
}
}