BloodDecal.cs

A Component that creates and manages a blood decal animation. It initializes lifetime, randomized start time and size, increments a global decal count on spawn, updates decal size and alpha over time, and destroys the GameObject when expired.

File Access
using System;
using Sandbox;

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

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

	private float _personalStartTime;
	private Vector2 _personalSize;

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

		_timeSinceSpawn = 0f;
		Lifetime = 10f;

		_personalStartTime = Game.Random.Float( 0.025f, 0.15f );
		var size = Game.Random.Float( 1f, 3f );
		_personalSize = new Vector2( size * Game.Random.Float( 0.8f, 1.2f ), size * Game.Random.Float( 0.8f, 1.2f ) );

		Manager.Instance.NumDecals++;
	}

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

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