A story collectible: a glowing fragment hidden behind the cave wall. Creates a ModelRenderer and PointLight if the scene has none, wakes when a beam from AsteroidSky passes near, then increases brightness and radius once the CaveGenerator trace between lizard and fragment is clear. Walking into range reports to StoryDirector and destroys itself.
using System;
using System.Linq;
/// <summary>
/// A glowing piece of the lizard's orders, buried in the rock just behind the cave wall.
/// Its light has no shadows, so a faint glow leaks through the rock as a hint. Once the player
/// has dug the wall away (nothing of the cave between lizard and fragment) it flares up, and
/// walking close picks it up and tells the <see cref="StoryDirector"/>.
/// </summary>
[Title( "Order Fragment" ), Category( "Story" ), Icon( "auto_awesome" )]
public sealed class OrderFragment : Component
{
[Property, Group( "Glow" )] public Color GlowColor { get; set; } = new Color( 0.35f, 1f, 0.85f );
/// <summary>How far the light reaches, in inches.</summary>
[Property, Group( "Glow" )] public float GlowRadius { get; set; } = 320f;
/// <summary>Light intensity while buried. One is a normal light.</summary>
[Property, Group( "Glow" )] public float Brightness { get; set; } = 1.5f;
/// <summary>Brightness and radius are multiplied by this once the fragment has been dug out.</summary>
[Property, Group( "Glow" )] public float ExposedBoost { get; set; } = 3f;
/// <summary>Pulses per second.</summary>
[Property, Group( "Glow" )] public float PulseSpeed { get; set; } = 0.5f;
/// <summary>Diameter of the glowing ball, in inches (only used when the visuals are made here).</summary>
[Property, Group( "Glow" )] public float Size { get; set; } = 14f;
/// <summary>Exposed and the player this close: it is picked up.</summary>
[Property, Group( "Pickup" )] public float PickupRange { get; set; } = 140f;
/// <summary>Buried and the player this close: they are near enough to be hunting for it.</summary>
[Property, Group( "Pickup" )] public float HintRange { get; set; } = 500f;
[Property, Group( "Pickup" )] public string Message { get; set; } = "ORDERS... INCOMPLETE";
[Property, Group( "Pickup" )] public SoundEvent PickupSound { get; set; }
/// <summary>True once nothing of the cave is between the player and the fragment.</summary>
public bool Exposed { get; private set; }
/// <summary>True while still buried with the player inside <see cref="HintRange"/>.</summary>
public bool PlayerNearby { get; private set; }
public bool Collected { get; private set; }
/// <summary>
/// The fragment lies dark until a sunbeam passes over it and wakes it. Turn off to have it
/// glow from the start.
/// </summary>
[Property, Group( "Wake" )] public bool WakeInSunbeam { get; set; } = true;
/// <summary>How close a beam's centre must pass to wake it, in inches.</summary>
[Property, Group( "Wake" )] public float WakeDistance { get; set; } = 150f;
/// <summary>Sound played as it wakes.</summary>
[Property, Group( "Wake" )] public string WakeSound { get; set; } = "sounds/opening/creak.sound";
/// <summary>True once it is glowing and can be picked up.</summary>
public bool Awake { get; private set; }
float _wake;
// The dev sphere model is about 64 inches across.
const float SphereModelSize = 64f;
const float CheckInterval = 0.2f;
ModelRenderer _ball;
PointLight _light;
ReptileController _player;
float _nextCheck;
float _boost = 1f;
protected override void OnStart()
{
if ( Scene.IsEditor ) return;
// Use whatever visuals were placed in the scene; make our own only if there are none.
_ball = Components.Get<ModelRenderer>( FindMode.EverythingInSelfAndDescendants );
_light = Components.Get<PointLight>( FindMode.EverythingInSelfAndDescendants );
if ( !_ball.IsValid() )
{
var go = NewChild( "Fragment Glow" );
go.LocalScale = Size / SphereModelSize;
_ball = go.Components.Create<ModelRenderer>();
_ball.Model = Model.Load( "models/dev/sphere.vmdl" );
_ball.Tint = GlowColor;
}
if ( !_light.IsValid() )
{
_light = NewChild( "Fragment Light" ).Components.Create<PointLight>();
_light.Shadows = false; // so the glow leaks through the rock
}
}
GameObject NewChild( string name )
{
var go = new GameObject( true, name );
go.Flags |= GameObjectFlags.NotSaved;
go.SetParent( GameObject, false );
return go;
}
protected override void OnUpdate()
{
if ( Scene.IsEditor || Collected ) return;
_nextCheck -= Time.Delta;
if ( _nextCheck <= 0f )
{
_nextCheck = CheckInterval;
CheckPlayer();
if ( Collected ) return;
}
if ( !Awake ) CheckWake();
_wake = _wake.LerpTo( Awake ? 1f : 0f, Time.Delta * 0.8f );
// Ease towards the stronger glow once exposed, then pulse slowly around it.
_boost = _boost.LerpTo( Exposed ? ExposedBoost : 1f, Time.Delta * 3f );
float pulse = (0.8f + 0.2f * MathF.Sin( Time.Now * PulseSpeed * MathF.PI * 2f )) * _wake;
if ( _light.IsValid() )
{
_light.LightColor = GlowColor;
_light.Intensity = Brightness * _boost * pulse;
_light.Radius = GlowRadius * (Exposed ? MathF.Sqrt( _boost ) : 1f);
}
if ( _ball.IsValid() ) _ball.Tint = Awake ? GlowColor * (0.6f + 0.4f * pulse) : new Color( 0.02f, 0.02f, 0.02f );
}
/// <summary>Wakes when the centre line of a lit sunbeam passes close by.</summary>
void CheckWake()
{
var sky = WakeInSunbeam ? Scene.GetAllComponents<AsteroidSky>().FirstOrDefault() : null;
if ( !sky.IsValid() )
{
Awake = true; // no sky to wait for
return;
}
foreach ( var beam in sky.LitBeams )
{
var to = WorldPosition - beam.Origin;
float along = Vector3.Dot( to, beam.Direction );
if ( along < 0f ) continue;
if ( (to - beam.Direction * along).Length > WakeDistance ) continue;
Awake = true;
if ( !string.IsNullOrEmpty( WakeSound ) )
{
var sound = Sound.Play( WakeSound, WorldPosition );
if ( sound.IsValid() ) { sound.Volume = 1.6f; sound.Pitch = 1.8f; }
}
return;
}
}
/// <summary>Looks from the lizard to the fragment: is rock still in the way, and is it close enough to grab?</summary>
void CheckPlayer()
{
if ( !_player.IsValid() ) _player = Scene.GetAllComponents<ReptileController>().FirstOrDefault();
if ( !_player.IsValid() ) return;
// Slightly above the lizard's feet. It clings to walls, so "above" is its own up.
var eye = _player.WorldPosition + _player.Up * 14f;
float distance = eye.Distance( WorldPosition );
var tr = Scene.Trace.Ray( eye, WorldPosition )
.IgnoreGameObjectHierarchy( _player.GameObject )
.IgnoreGameObjectHierarchy( GameObject )
.Run();
// Buried while the cave itself is in the way. Anything else in between doesn't count.
bool buried = tr.Hit && tr.GameObject.IsValid()
&& tr.GameObject.Components.Get<CaveGenerator>( FindMode.EverythingInSelfAndAncestors ).IsValid();
Exposed = !buried;
PlayerNearby = buried && distance <= HintRange;
if ( Awake && Exposed && distance <= PickupRange ) Collect();
}
void Collect()
{
if ( Collected ) return;
Collected = true;
StoryDirector.Get( Scene )?.CollectFragment( Message );
if ( PickupSound is not null ) Sound.Play( PickupSound, WorldPosition );
// The fragment's light floods out through the lizard as it takes it in.
var sense = _player.IsValid() ? _player.Components.Get<LizardSense>() : null;
sense?.Pulse( GlowColor, 1.6f );
_player?.Kick( Vector3.Up * 6f );
if ( !string.IsNullOrEmpty( WakeSound ) )
{
var sound = Sound.Play( WakeSound, WorldPosition );
if ( sound.IsValid() ) { sound.Volume = 2f; sound.Pitch = 2.4f; }
}
GameObject.Destroy();
}
}