GameOverMonitor.cs
using Sandbox;
public interface IGameOverEvents : ISceneEvent<IGameOverEvents>
{
void OnGameOver(int wavesSurvived) { }
}
public sealed class GameOverMonitor : Component, IPlayerHealthEvent, IBaseHealthEvents
{
TimeSince gameLength;
protected override void OnEnabled()
{
base.OnEnabled();
gameLength = 0;
}
void IPlayerHealthEvent.OnPostDeath( GameObject player )
{
var alivePlayer = Scene.GetComponentInChildren<PlayerController>();
if ( alivePlayer is null ) GameOver();
}
void IBaseHealthEvents.OnHealthChanged( float currentHealth )
{
if ( currentHealth <= 0 ) GameOver();
}
void GameOver()
{
Scene.TimeScale = 0;
var waves = Scene.Get<EnemySpawner>().WaveCounter - 1;
GetComponentInChildren<GameOverScreen>( true ).Waves = waves;
foreach (var child in GameObject.Children)
{
child.Enabled = true;
}
// notify
IGameOverEvents.Post( x => x.OnGameOver( waves ) );
}
}