Transposer/Entities/Trail.cs
namespace Sandbox.Transposer;

/// <summary>
/// Blood trail left by <see cref="BloodParticle"/> when it lands.
/// Fades out over ~4.75 seconds, with optional random spread.
/// Drawn as 2px wide with chance of extra pixels to the sides/above.
/// </summary>
public class Trail : Entity
{
	private float _lifetime;
	private const float LIFETIME_MAX = 4.75f;

	private bool _spreadLeft;
	private bool _spreadRight;
	private bool _spreadUp;
	private float _startingOpacity;

	public Trail( int x, int y, float startingOpacity, TransposerScene scene )
	{
		PixelPosition = new PixelPoint( x, y );
		_scene = scene;
		_type = "trail";
		_layer = Globals.DEPTH_TRAIL;
		_lifetime = LIFETIME_MAX * Game.Random.Float( 0.9f, 1f );
		_spreadLeft = Game.Random.Next( 0, 3 ) == 0;
		_spreadRight = Game.Random.Next( 0, 3 ) == 0;
		_spreadUp = Game.Random.Next( 0, 6 ) == 0;
		_startingOpacity = startingOpacity;
	}

	public override void UpdateEntity( float deltaTime )
	{
		base.UpdateEntity( deltaTime );

		_lifetime -= deltaTime;
		if ( _lifetime <= 0f )
			_scene.RemoveEntity( this );
	}

	public override void Draw()
	{
		float opacity = PixelUtils.Map( _lifetime, LIFETIME_MAX, 0f, _startingOpacity, 0f, EaseType.SineOut );
		Color32 color = new( 255, 0, 0, (byte)(opacity * 255f) );

		Screen.AddPixel( PixelX, PixelY, color );
		Screen.AddPixel( PixelX + 1, PixelY, color );

		if ( _spreadLeft ) Screen.AddPixel( PixelX - 1, PixelY, color );
		if ( _spreadRight ) Screen.AddPixel( PixelX + 2, PixelY, color );
		if ( _spreadUp )
		{
			Screen.AddPixel( PixelX, PixelY + 1, color );
			Screen.AddPixel( PixelX + 1, PixelY + 1, color );
		}
	}
}