A component that creates and updates a blood splat decal entity. It randomizes lifetime, start delay and size on enable, increments a global decal count, then on update lerps the decal size and fades its tint until lifetime expires and destroys the GameObject.
using System;
using Sandbox;
public sealed class BloodSplatDecal : Component
{
private TimeSince _timeSinceSpawn;
public float Lifetime { get; set; }
[Property] public Decal Decal { get; set; }
private float _personalStartTime;
private Vector2 _personalSize;
[Property] public Color Color { get; set; }
public float FullAlpha { get; set; } = 1.0f;
public EasingType ColorFadeEasingType { get; set; } = EasingType.ExpoIn;
protected override void OnEnabled()
{
base.OnEnabled();
_timeSinceSpawn = 0f;
Lifetime = Game.Random.Float( 1.5f, 2.75f );
_personalStartTime = Game.Random.Float( 0.05f, 0.3f );
var size = Game.Random.Float( 2f, 3f );
_personalSize = new Vector2( size * Game.Random.Float( 0.9f, 1.2f ), size * Game.Random.Float( 0.9f, 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.WithAlpha( Utils.Map( _timeSinceSpawn, 0f, Lifetime, FullAlpha, 0f, ColorFadeEasingType ) );
}
}
}