ArenaShrinker.cs

Component that manages a timed arena shrink mechanic. It tracks round time, periodically warns outer tiles and then drops them to shrink the playable area by selecting outer tiles based on distance from the arena center.

File Access
using Sandbox;
using System;
using System.Collections.Generic;
using System.Linq;

public sealed class ArenaShrinker : Component
{
    [Property] public float RoundTime { get; set; } = 120f;
    [Property] public float ShrinkInterval { get; set; } = 15f;
    [Property] public float WarningDuration { get; set; } = 3f;

    public float TimeRemaining { get; private set; }

    private float nextShrinkTime;
    private bool isWarningActive = false;
    private List<ArenaTile> activeWarningTiles = new();
    private List<ArenaTile> arenaTiles = new();

    protected override void OnStart()
    {
        arenaTiles = Scene.GetAllComponents<ArenaTile>().Where( tile => tile.IsValid() ).ToList();
        StartRound();
    }

    public void StartRound()
    {
        TimeRemaining = RoundTime;
        nextShrinkTime = RoundTime - ShrinkInterval;
        isWarningActive = false;
        activeWarningTiles.Clear();

        foreach ( var tile in arenaTiles )
        {
            tile.ResetTile();
        }

        Log.Info( "[ArenaShrinker] Round started!" );
    }

    protected override void OnUpdate()
    {
        if ( TimeRemaining <= 0 )
        {
            if ( RoundManager.Instance != null && RoundManager.Instance.IsInRound )
            {
                _ = RoundManager.Instance.EndRound();
            }
            return;
        }

        TimeRemaining -= Time.Delta;

        if ( !isWarningActive && TimeRemaining <= (nextShrinkTime + WarningDuration) )
        {
            TriggerWarning();
        }

        if ( isWarningActive && TimeRemaining <= nextShrinkTime )
        {
            DropWarnedTiles();
        }
    }

    private void TriggerWarning()
    {
        isWarningActive = true;
        activeWarningTiles.Clear();

        var availableTiles = arenaTiles
            .Where( t => t.IsValid() && !t.IsFalling )
            .ToList();

        if ( availableTiles.Count <= 2 ) return;

        Vector3 center = Vector3.Zero;
        foreach ( var tile in availableTiles )
        {
            center += tile.WorldPosition;
        }
        center /= availableTiles.Count;

        var sortedTiles = availableTiles
            .OrderByDescending( t => Vector3.DistanceBetween( t.WorldPosition, center ) )
            .ToList();

        int tilesToDropCount = Math.Max( 1, (int)(sortedTiles.Count * 0.25f) );

        if ( sortedTiles.Count - tilesToDropCount < 2 )
        {
            tilesToDropCount = Math.Max( 0, sortedTiles.Count - 2 );
        }

        if ( tilesToDropCount <= 0 ) return;

        activeWarningTiles = sortedTiles.Take( tilesToDropCount ).ToList();

        foreach ( var tile in activeWarningTiles )
        {
            tile.HighlightWarning( true );
        }

        Log.Info( $"[ArenaShrinker] Warning started for {activeWarningTiles.Count} outer tiles!" );
    }

    private void DropWarnedTiles()
    {
        foreach ( var tile in activeWarningTiles )
        {
            tile.HighlightWarning( false );
            tile.Drop();
        }

        activeWarningTiles.Clear();
        isWarningActive = false;
        nextShrinkTime -= ShrinkInterval;

        Log.Info( "[ArenaShrinker] Outer ring dropped successfully!" );
    }
}