127 lines
3.6 KiB
C#
127 lines
3.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class Level1 : Level
|
|
{
|
|
[Header("Wave Flow")]
|
|
public WaveSpawner spawner; // Assign in Inspector
|
|
[Min(0.05f)] public float checkInterval = 0.25f;
|
|
|
|
private bool _running;
|
|
private Coroutine _monitorCo;
|
|
|
|
// Optional: simple counters for UI hooks
|
|
private int _currentWaveIdx = -1;
|
|
private int _totalWaves = 0;
|
|
private int _currentAlive = 0;
|
|
private void OnEnable()
|
|
{
|
|
if (spawner != null)
|
|
{
|
|
spawner.OnWaveStarted += HandleWaveStarted;
|
|
spawner.OnWaveCompleted += HandleWaveCompleted;
|
|
spawner.OnAllWavesCompleted += HandleAllWavesCompleted;
|
|
spawner.OnAliveCountChanged += HandleAliveChanged;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (spawner != null)
|
|
{
|
|
spawner.OnWaveStarted -= HandleWaveStarted;
|
|
spawner.OnWaveCompleted -= HandleWaveCompleted;
|
|
spawner.OnAllWavesCompleted -= HandleAllWavesCompleted;
|
|
spawner.OnAliveCountChanged -= HandleAliveChanged;
|
|
}
|
|
}
|
|
|
|
public override void OnLevelStart()
|
|
{
|
|
base.OnLevelStart();
|
|
|
|
if (spawner == null)
|
|
{
|
|
Debug.LogError("[Level1] No WaveSpawner assigned.");
|
|
CompleteLevelImmediate();
|
|
return;
|
|
}
|
|
|
|
_totalWaves = spawner.TotalWaves;
|
|
_running = true;
|
|
|
|
// Kick off waves
|
|
spawner.StartSpawning();
|
|
|
|
// As an extra safeguard, monitor completion state
|
|
_monitorCo = StartCoroutine(MonitorForCompletion());
|
|
Debug.Log("[Level1] Level started, waves spawning…");
|
|
}
|
|
|
|
public override void OnLevelEnd()
|
|
{
|
|
base.OnLevelEnd();
|
|
_running = false;
|
|
|
|
if (_monitorCo != null)
|
|
{
|
|
StopCoroutine(_monitorCo);
|
|
_monitorCo = null;
|
|
}
|
|
Debug.Log("OnLevelEndCalled");
|
|
}
|
|
|
|
private IEnumerator MonitorForCompletion()
|
|
{
|
|
var wait = new WaitForSeconds(checkInterval);
|
|
while (_running)
|
|
{
|
|
// Complete when spawner reports all waves completed AND no alive remain
|
|
if (spawner.AllWavesCompleted && spawner.CurrentAlive == 0)
|
|
{
|
|
CompleteLevelImmediate();
|
|
yield break;
|
|
}
|
|
yield return wait;
|
|
}
|
|
}
|
|
|
|
private void CompleteLevelImmediate()
|
|
{
|
|
_running = false;
|
|
Debug.Log("[Level1] All waves cleared. Level complete!");
|
|
|
|
if (GameplayManager.Instance != null)
|
|
GameplayManager.Instance.CompleteCurrentLevel();
|
|
else
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
// ===== 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”
|
|
}
|
|
}
|