new spawning manager, spawner improvements - now with multiple enemies to spawn, KillTrigger

This commit is contained in:
2025-05-21 13:40:14 +02:00
parent 777c90ce50
commit 07eff8a72c
7 changed files with 705 additions and 155 deletions

View File

@@ -10,7 +10,7 @@ namespace Beyond
public class KillTrigger : Saver
{
[SerializeField] EnemySpawner[] m_spawners;
public UnityEvent<KillTrigger> OnTrigger;
[Serializable]
@@ -22,9 +22,13 @@ namespace Beyond
private SaveData m_data = new SaveData();
void Start()
{
if (m_spawners == null) return; // Guard clause
foreach (var s in m_spawners)
{
s.m_OnDead.AddListener(OnEnemyKilled);
if (s != null) // Good practice to check for null spawners in array
{
s.m_OnDead.AddListener(OnEnemyKilled);
}
}
}
@@ -32,24 +36,44 @@ namespace Beyond
{
if (m_data.wasTriggered)
return;
if (m_spawners == null) return; // Guard clause
foreach (var spawner in m_spawners)
{
if (!spawner.SpawnedAndDead())
return;
if (spawner == null) continue; // Skip if a spawner in the array is null
bool hasSpawnedAWave = spawner.GetWavesSpawnedCount() > 0; // We'll need to add this getter to EnemySpawner
bool noEnemiesCurrentlyAlive = !spawner.IsAnyEnemyAlive();
if (hasSpawnedAWave && noEnemiesCurrentlyAlive)
{
// This spawner has done its part for *this current engagement*
}
else
{
return; // If ANY spawner doesn't meet this, the trigger condition isn't met
}
}
OnTrigger?.Invoke(this);
m_data.wasTriggered = true;
}
private void OnEnemyKilled(EnemySpawner arg0)
{
// arg0 is the spawner from which an enemy died.
// We need to check all spawners associated with this trigger.
CheckSpawners();
}
private void OnDestroy()
{
if (m_spawners == null) return; // Guard clause
foreach (var s in m_spawners)
{
s.m_OnDead.RemoveListener(OnEnemyKilled);
if (s != null) // Good practice
{
s.m_OnDead.RemoveListener(OnEnemyKilled);
}
}
}
@@ -63,6 +87,8 @@ namespace Beyond
var data = SaveSystem.Deserialize<SaveData>(s);
if (data != null)
m_data = data;
// Optional: if not triggered, re-check on load in case state was met while inactive
// if (!m_data.wasTriggered) { CheckSpawners(); }
}
}
}
}