BloodDecal.cs

Component that represents a blood decal entity. It spawns with a random size and start delay, increments a global decal counter, animates size and fade over its Lifetime, then decrements the counter and destroys itself.

Native Interop
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 ) );
		}
	}
}