Story/BreachLights.cs
using System;
using System.Collections.Generic;

/// <summary>
/// The work lights the owners shine in through the breach: a few harsh spotlights just outside
/// the hole, aimed into the cave, so whoever walks in is a black shape against the glare and
/// throws a long shadow across the floor. The object's forward (+X) is the way into the cave.
/// The lights are built in play mode only and are never saved.
/// </summary>
[Title( "Breach Lights" ), Category( "Story" ), Icon( "flare" )]
public sealed class BreachLights : Component
{
	/// <summary>How many spotlights, side by side across the breach.</summary>
	[Property, Range( 1, 5 )] public int LightCount { get; set; } = 3;

	[Property] public float Intensity { get; set; } = 80f;
	[Property] public Color LightColor { get; set; } = new( 0.78f, 0.88f, 1f );

	/// <summary>Full outer cone angle of each light, in degrees.</summary>
	[Property] public float ConeAngle { get; set; } = 50f;

	[Property] public float Range { get; set; } = 6000f;

	/// <summary>How strongly the beams light up the dust in the air.</summary>
	[Property] public float FogStrength { get; set; } = 4f;

	/// <summary>How far the lights sit behind the breach (outside the cave), in inches.</summary>
	[Property] public float Setback { get; set; } = 30f;

	/// <summary>Sideways distance between neighbouring lights, in inches.</summary>
	[Property] public float Spacing { get; set; } = 70f;

	/// <summary>How far the lights tip down from level, in degrees.</summary>
	[Property] public float DownTilt { get; set; } = 7f;

	/// <summary>How far each light is turned away from its neighbour, in degrees.</summary>
	[Property] public float SpreadDegrees { get; set; } = 9f;

	/// <summary>The first light swings this far to either side, in degrees.</summary>
	[Property] public float SweepDegrees { get; set; } = 5f;

	/// <summary>Seconds for one full swing there and back.</summary>
	[Property] public float SweepSeconds { get; set; } = 9f;

	/// <summary>Seconds of stutter as the lights come on.</summary>
	[Property] public float FlickerSeconds { get; set; } = 0.8f;

	readonly List<SpotLight> _lights = new();
	readonly List<Angles> _aim = new();
	TimeSince _sinceOn;

	protected override void OnEnabled()
	{
		if ( Scene.IsEditor ) return;
		_sinceOn = 0f;
	}

	protected override void OnDisabled() => Clear();

	void Clear()
	{
		foreach ( var light in _lights )
			if ( light.IsValid() ) light.GameObject.Destroy();
		_lights.Clear();
		_aim.Clear();
	}

	void Build()
	{
		Clear();
		int count = Math.Clamp( LightCount, 1, 5 );
		for ( int i = 0; i < count; i++ )
		{
			// -1 .. 1 across the row; a single light sits in the middle.
			float side = count == 1 ? 0f : i / (count - 1f) * 2f - 1f;

			var go = new GameObject( GameObject, true, "Breach Light" );
			go.Flags |= GameObjectFlags.NotSaved;
			// Alternate heights a little so the beams don't lie in one flat sheet.
			go.LocalPosition = new Vector3( -Setback, side * Spacing * (count - 1) * 0.5f, i % 2 == 0 ? 25f : 55f );

			var light = go.Components.Create<SpotLight>();
			light.Shadows = true;
			light.FogMode = Light.FogInfluence.Enabled;

			// Visible shafts through the dust, starting just inside the breach.
			var shaft = go.Components.Create<LightShaft>();
			shaft.StartOffset = Setback + 60f;
			shaft.MaxLength = 1100f;  // haze stays near the breach, behind the team, so they read as silhouettes
			shaft.Sprites = 34;
			shaft.Brightness = 0.0009f;
			shaft.MaxBrightness = 0.07f;

			_lights.Add( light );
			_aim.Add( new Angles( DownTilt, side * SpreadDegrees, 0f ) ); // positive pitch looks down
		}
	}

	protected override void OnUpdate()
	{
		if ( Scene.IsEditor ) return;
		if ( _lights.Count != Math.Clamp( LightCount, 1, 5 ) ) Build();

		for ( int i = 0; i < _lights.Count; i++ )
		{
			var light = _lights[i];
			if ( !light.IsValid() ) continue;

			// Coming on: each light stutters on and off for a moment, the later ones a bit longer.
			float on = 1f;
			float flickerFor = FlickerSeconds * (0.6f + 0.4f * i);
			if ( _sinceOn < flickerFor ) on = Game.Random.Float( 0f, 1f ) < 0.45f ? Game.Random.Float( 0.5f, 1f ) : 0f;

			light.LightColor = LightColor;
			light.Intensity = Intensity * on;
			light.ConeOuter = ConeAngle * 0.5f;
			light.ConeInner = ConeAngle * 0.3f;
			light.Radius = Range;
			light.FogStrength = FogStrength;

			var aim = _aim[i];
			if ( i == 0 && SweepSeconds > 0.01f )
				aim.yaw += MathF.Sin( _sinceOn * MathF.PI * 2f / SweepSeconds ) * SweepDegrees;
			light.GameObject.LocalRotation = aim.ToRotation();
		}
	}
}