ScorchDecal.cs

A component that manages a scorch decal's lifetime and appearance. It tracks spawn time, increases a global decal counter on enable, updates decal size and alpha over time, and destroys its GameObject when expired.

using System;
using Sandbox;

public sealed class ScorchDecal : Component
{
	private TimeSince _timeSinceSpawn;
	public float Lifetime { get; set; }

	[Property] public Decal Decal { get; set; }

	public float StartTime { get; set; }
	public float Size { get; set; }

	protected override void OnEnabled()
	{
		base.OnEnabled();

		_timeSinceSpawn = 0f;
		Lifetime = 10f;

		Manager.Instance.NumDecals++;
	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		if ( _timeSinceSpawn > Lifetime )
		{
			Manager.Instance.NumDecals--;
			GameObject.Destroy();
		}
		else
		{
			Decal.Size = Utils.Map( _timeSinceSpawn, 0f, StartTime, 0f, Size, EasingType.ExpoOut );
			Decal.ColorTint = Color.Black.WithAlpha( Utils.Map( _timeSinceSpawn, 0f, Lifetime, 1f, 0f, EasingType.QuadIn ) );
		}
	}
}