GameModeManager component for the game. Manages King of the Hill scoring by detecting a player in a capture zone and awarding periodic points, and monitors an ArenaShrinker to trigger a Sudden Death state that increases a force multiplier and notifies UI systems.
using Sandbox;
using System;
using System.Linq;
public sealed class GameModeManager : Component, Component.ITriggerListener
{
public static GameModeManager Instance { get; private set; }
[Property] public bool EnableKingOfTheHill { get; set; } = true;
[Property] public int PointsPerSecond { get; set; } = 2;
[Property] public float ZoneCheckInterval { get; set; } = 1.0f;
[Property] public bool EnableSuddenDeath { get; set; } = true;
[Property] public float SuddenDeathThresholdTime { get; set; } = 20f;
[Property] public float SuddenDeathForceMultiplier { get; set; } = 2.5f;
public bool IsSuddenDeathActive { get; private set; } = false;
private TimeSince _timeSinceZoneTick;
private GameObject _currentZoneOwner;
private PlayerHealth _currentZoneHealth;
private ArenaShrinker _arenaShrinker;
protected override void OnStart()
{
Instance = this;
_arenaShrinker = Scene.GetAllComponents<ArenaShrinker>().FirstOrDefault();
}
protected override void OnUpdate()
{
if ( EnableSuddenDeath && !IsSuddenDeathActive )
{
CheckSuddenDeathCondition();
}
if ( EnableKingOfTheHill && _timeSinceZoneTick >= ZoneCheckInterval )
{
_timeSinceZoneTick = 0;
AwardHillPoints();
}
}
private void CheckSuddenDeathCondition()
{
if ( _arenaShrinker.IsValid() && _arenaShrinker.TimeRemaining <= SuddenDeathThresholdTime && _arenaShrinker.TimeRemaining > 0f )
{
TriggerSuddenDeath();
}
}
public void TriggerSuddenDeath()
{
IsSuddenDeathActive = true;
CameraJuice.Instance?.Shake( 25f );
if ( Killfeed.Instance != null )
{
Killfeed.Instance.AddEntry( "СИСТЕМА", "ВСЕ ИГРОКИ", "ВНЕЗАПНАЯ СМЕРТЬ!" );
}
Log.Info( "[GameModeManager] Активирован режим SUDDEN DEATH!" );
}
public void OnTriggerEnter( Collider other )
{
if ( !other.IsValid() || !other.GameObject.IsValid() ) return;
var root = other.GameObject.Root;
var health = root.Components.Get<PlayerHealth>();
if ( health.IsValid() )
{
_currentZoneOwner = root;
_currentZoneHealth = health;
}
}
public void OnTriggerExit( Collider other )
{
if ( !other.IsValid() || !other.GameObject.IsValid() ) return;
var root = other.GameObject.Root;
if ( _currentZoneOwner == root )
{
_currentZoneOwner = null;
_currentZoneHealth = null;
}
}
private void AwardHillPoints()
{
if ( _currentZoneOwner.IsValid() && _currentZoneHealth.IsValid() )
{
if ( !_currentZoneHealth.IsDead )
{
if ( !_currentZoneOwner.IsProxy && ScoreManager.Instance != null )
{
ScoreManager.Instance.AddScore( PointsPerSecond );
}
}
}
}
public float GetForceMultiplier()
{
return IsSuddenDeathActive ? SuddenDeathForceMultiplier : 1.0f;
}
}