Story/LizardSense.cs
using System;

/// <summary>
/// The lizard's feel for the dark: a faint, short-range glow that follows it around, so the
/// player can make out the rock right next to them while the cave is still pitch black.
/// Goes on the lizard. The light itself is a child object made at runtime and never saved.
/// </summary>
[Title( "Lizard Sense" ), Category( "Story" ), Icon( "blur_on" )]
public sealed class LizardSense : Component
{
	/// <summary>How far the glow reaches, in inches.</summary>
	[Property] public float Radius { get; set; } = 240f;

	/// <summary>Tint of the glow. Keep it dim so the sun beams stay the bright thing in the cave.</summary>
	[Property] public Color Color { get; set; } = new Color( 0.55f, 0.85f, 0.7f );

	/// <summary>Brightness multiplier of the glow.</summary>
	[Property] public float Brightness { get; set; } = 0.35f;

	/// <summary>How far above the lizard's origin the glow sits, in inches.</summary>
	[Property] public float Height { get; set; } = 45f;

	/// <summary>How much the brightness swells and fades, as a fraction (0 = steady).</summary>
	[Property, Range( 0f, 1f )] public float PulseAmount { get; set; } = 0.15f;

	/// <summary>Seconds for one full pulse.</summary>
	[Property] public float PulseSeconds { get; set; } = 4f;

	GameObject _glow;
	/// <summary>Input action that sends out a sense pulse. "Menu" is bound to Q.</summary>
	[Property, Group( "Sense Pulse" )] public string PulseAction { get; set; } = "Menu";

	/// <summary>How far a sense pulse reaches, in inches.</summary>
	[Property, Group( "Sense Pulse" )] public float PulseReach { get; set; } = 1500f;

	/// <summary>Seconds a pulse takes to spread out and fade.</summary>
	[Property, Group( "Sense Pulse" )] public float PulseDuration { get; set; } = 1.8f;

	/// <summary>Seconds before another pulse can be sent.</summary>
	[Property, Group( "Sense Pulse" )] public float PulseCooldown { get; set; } = 5f;

	[Property, Group( "Sense Pulse" )] public float PulseBrightness { get; set; } = 16f;

	[Property, Group( "Sense Pulse" )] public string PulseSound { get; set; } = "sounds/opening/drip.sound";

	/// <summary>0 when idle, rising to 1 across a sense pulse.</summary>
	public float PulseProgress => _sincePulse < PulseDuration ? _sincePulse / PulseDuration : 0f;

	/// <summary>Sends out a pulse now, optionally in another colour (the fragment's, when it is picked up).</summary>
	public void Pulse( Color? colour = null, float strength = 1f )
	{
		_sincePulse = 0;
		_pulseColour = colour ?? Color;
		_pulseStrength = strength;
	}

	Color _pulseColour;
	float _pulseStrength = 1f;

	TimeSince _sincePulse = 100f;
	bool _hintShown;

	PointLight _light;
	float _time;

	protected override void OnEnabled()
	{
		if ( Scene.IsEditor ) return;

		_glow = new GameObject( GameObject, true, "Lizard Sense Glow" );
		_glow.Flags |= GameObjectFlags.NotSaved;
		_glow.LocalPosition = Vector3.Up * Height;

		_light = _glow.Components.Create<PointLight>();
		_light.Shadows = false;
		Apply();
	}

	protected override void OnDisabled()
	{
		if ( _glow.IsValid() ) _glow.Destroy();
		_glow = null;
		_light = null;
	}

	protected override void OnUpdate()
	{
		if ( !_light.IsValid() ) return;
		_time += Time.Delta;

		// Sense pulse: for a moment the lizard "feels" the whole space around it.
		if ( !string.IsNullOrEmpty( PulseAction ) && Input.Pressed( PulseAction ) && _sincePulse > PulseCooldown )
		{
			Pulse();
			if ( !string.IsNullOrEmpty( PulseSound ) )
			{
				var sound = Sound.Play( PulseSound, WorldPosition );
				if ( sound.IsValid() ) { sound.Volume = 1.2f; sound.Pitch = 0.5f; }
			}
		}

		// Tell the player once, a little way into the dark.
		if ( !_hintShown && _time > 11f )
		{
			_hintShown = true;
			var director = StoryDirector.Get( Scene );
			if ( director is not null && !director.MessageVisible ) director.ShowMessage( "Q  sense the dark", 5f );
		}

		Apply();
	}

	// Pushes the properties to the light every frame, so they can be tuned while playing.
	void Apply()
	{
		_glow.LocalPosition = Vector3.Up * Height;

		float pulse = 1f + PulseAmount * MathF.Sin( _time * MathF.PI * 2f / MathF.Max( PulseSeconds, 0.1f ) );

		// During a sense pulse the glow rushes outward and dies away as it goes.
		float p = PulseProgress;
		float spread = p > 0f ? 1f - (1f - p) * (1f - p) : 0f;
		float fade = p > 0f ? (1f - p) * (1f - p) : 0f;

		_light.Radius = Radius + (PulseReach - Radius) * spread;
		_light.LightColor = p > 0f ? Color.Lerp( Color, _pulseColour, fade ) : Color;
		_light.Intensity = Brightness * pulse + PulseBrightness * _pulseStrength * fade;
	}
}