Blocks/ExplodingBlock.cs

A game component representing an exploding breakable block. When destroyed it marks itself broken, triggers a short hit-stop, and recursively destroys all other breakable Block components within BlastRadius, allowing chain explosions.

File Access
using Sandbox;

namespace Breakout;

/// <summary>
/// A brick that blows up when destroyed, taking out every other breakable brick within
/// BlastRadius. Those can be exploding bricks too, so one blast can chain into more.
/// </summary>
[Title( "Exploding Block" ), Category( "Breakout" ), Icon( "bolt" )]
public sealed class ExplodingBlock : Block
{
	[Property] public float BlastRadius { get; set; } = 80f;

	/// <summary>
	/// Breaks this brick and destroys every other breakable brick within BlastRadius, which can chain into further explosions.
	/// </summary>
	public override void OnDestroyed()
	{
		if ( Broken )
			return;

		var origin = WorldPosition;

		base.OnDestroyed();
		Game?.HitStop( 0.05f );

		foreach ( var other in Scene.GetAllComponents<Block>().ToList() )
		{
			if ( other == this || !other.IsValid() || !other.Breakable )
				continue;

			if ( other.WorldPosition.Distance( origin ) <= BlastRadius )
				other.OnDestroyed();
		}
	}
}