Story director for the opening sequence. Holds a one-way StoryStage enum, drives timed beats (fragment pickup, orbit capture jolt with camera shake and rumble, knocks, envoy arrival), and polls Envoy and SkinWearer components to advance to the ending messages and end card, then loads the next scene.
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>Where the opening has got to. Stages only ever move forward.</summary>
public enum StoryStage
{
/// <summary>Drifting in the dark; beams sweep the cave and brighten.</summary>
Drifting,
/// <summary>The first order fragment has been picked up.</summary>
FragmentFound,
/// <summary>The asteroid has been captured into Earth orbit: jolt, spin slows, Earth visible.</summary>
Captured,
/// <summary>The owner envoy arrives.</summary>
Envoy,
/// <summary>The lizard has killed the envoy.</summary>
EnvoyDead,
/// <summary>The lizard is wearing the envoy's skin. End of the opening.</summary>
Skinned,
}
/// <summary>
/// One per scene. Sequences the opening, and is the shared place for on-screen messages and
/// camera shake. Other components find it with <see cref="Get"/> and react to <see cref="StageChanged"/>.
/// </summary>
[Title( "Story Director" ), Category( "Story" ), Icon( "movie" )]
public sealed class StoryDirector : Component
{
public static StoryDirector Get( Scene scene ) => scene?.GetAllComponents<StoryDirector>().FirstOrDefault();
/// <summary>Seconds between picking up the fragment and the orbit-capture jolt.</summary>
[Property] public float CaptureDelay { get; set; } = 5f;
/// <summary>Seconds between the jolt and the envoy arriving.</summary>
[Property] public float EnvoyDelay { get; set; } = 14f;
[Property] public float JoltStrength { get; set; } = 14f;
[Property] public float JoltSeconds { get; set; } = 3.5f;
[Property] public SoundEvent RumbleSound { get; set; }
/// <summary>Line shown a moment after play starts. Empty for none.</summary>
[Property] public string OpeningLine { get; set; } = "2028. Something has been drifting toward Earth for a very long time. It is awake now, and it is not wearing trousers.";
/// <summary>Seconds after the jolt at which something knocks on the rock from outside.</summary>
[Property] public List<float> KnockTimes { get; set; } = new() { 5f, 8.5f, 11f, 12.5f };
/// <summary>Sound of the knocks (heard through the rock).</summary>
[Property] public string KnockSound { get; set; } = "sounds/opening/latch_thud.sound";
public StoryStage Stage { get; private set; } = StoryStage.Drifting;
/// <summary>Raised once each time the stage moves forward.</summary>
public event Action<StoryStage> StageChanged;
/// <summary>Seconds since the scene started playing.</summary>
public float Elapsed { get; private set; }
public float TimeInStage { get; private set; }
// ---- Messages -------------------------------------------------------------------------
public string Message { get; private set; }
public string Speaker { get; private set; }
public bool MessageVisible => _messageLeft > 0f && !string.IsNullOrEmpty( Message );
float _messageLeft;
bool _openingShown;
int _knocks;
/// <summary>Shows a line of text on screen for a while. A new message replaces the old one.</summary>
public void ShowMessage( string text, float seconds = 5f, string speaker = null )
{
Message = text;
Speaker = speaker;
_messageLeft = seconds;
}
public void ClearMessage() => _messageLeft = 0f;
// ---- Camera shake ---------------------------------------------------------------------
float _shakeStrength, _shakeSeconds, _shakeLeft;
/// <summary>Offset the player camera should add this frame, in inches.</summary>
public Vector3 ShakeOffset { get; private set; }
public void Shake( float strength, float seconds )
{
_shakeStrength = strength;
_shakeSeconds = MathF.Max( seconds, 0.01f );
_shakeLeft = seconds;
}
// ---- Sequence -------------------------------------------------------------------------
/// <summary>Moves to a later stage. Calls that would go backwards or repeat are ignored.</summary>
public void Advance( StoryStage stage )
{
if ( stage <= Stage ) return;
Stage = stage;
TimeInStage = 0f;
if ( stage == StoryStage.Captured )
{
Shake( JoltStrength, JoltSeconds );
if ( RumbleSound is not null ) Sound.Play( RumbleSound, Scene.Camera.IsValid() ? Scene.Camera.WorldPosition : WorldPosition );
}
StageChanged?.Invoke( stage );
}
/// <summary>Called by the order fragment when the player picks it up.</summary>
public void CollectFragment( string message = "ORDERS... INCOMPLETE" )
{
ShowMessage( message, 6f, "FRAGMENT" );
Advance( StoryStage.FragmentFound );
}
protected override void OnUpdate()
{
if ( Scene.IsEditor ) return;
Elapsed += Time.Delta;
TimeInStage += Time.Delta;
if ( _messageLeft > 0f ) _messageLeft -= Time.Delta;
if ( _shakeLeft > 0f )
{
_shakeLeft -= Time.Delta;
float amount = _shakeStrength * MathF.Max( _shakeLeft, 0f ) / _shakeSeconds;
ShakeOffset = new Vector3( Game.Random.Float( -1f, 1f ), Game.Random.Float( -1f, 1f ), Game.Random.Float( -1f, 1f ) ) * amount;
}
else
{
ShakeOffset = Vector3.Zero;
}
if ( !_openingShown && Elapsed > 2.5f )
{
_openingShown = true;
if ( !string.IsNullOrEmpty( OpeningLine ) && !MessageVisible ) ShowMessage( OpeningLine, 6f );
}
// Between the jolt and the drilling: knocks from outside, closer together each time.
if ( Stage == StoryStage.Captured && KnockTimes is not null && _knocks < KnockTimes.Count && TimeInStage >= KnockTimes[_knocks] )
{
_knocks++;
Shake( 1.2f + _knocks * 0.5f, 0.25f );
if ( !string.IsNullOrEmpty( KnockSound ) && Scene.Camera.IsValid() )
{
var knock = Sound.Play( KnockSound, Scene.Camera.WorldPosition + Vector3.Forward * 600f );
if ( knock.IsValid() ) { knock.Volume = 1.5f; knock.Pitch = 0.6f; }
}
}
UpdateEnding();
if ( Stage == StoryStage.FragmentFound && TimeInStage >= CaptureDelay ) Advance( StoryStage.Captured );
else if ( Stage == StoryStage.Captured && TimeInStage >= EnvoyDelay ) Advance( StoryStage.Envoy );
}
// ---- Ending: kill, then skin -----------------------------------------------------------
/// <summary>What an escort shouts when the envoy goes down.</summary>
[Property, Group( "Ending" )] public string PanicLine { get; set; } = "Contact! It bit the envoy, right in the portfolio! Back to the breach!";
/// <summary>Nudge toward the body if the player hasn't taken the skin yet.</summary>
[Property, Group( "Ending" )] public string BodyHint { get; set; } = "The envoy is still warm. Wear him.";
/// <summary>What an escort says to the lizard once it walks back wearing the envoy.</summary>
[Property, Group( "Ending" )] public string FooledLine { get; set; } = "...Sir? We thought it had you. You look dreadful. Very on brand. This way.";
/// <summary>Last line of the opening.</summary>
[Property, Group( "Ending" )] public string ClosingLine { get; set; } = "They cannot tell. He was always a bit lumpy.";
[Property, Group( "Ending" )] public string EscortVoice { get; set; } = "sounds/opening/voice_garble.sound";
/// <summary>Title shown on the card that closes the opening. Empty for no card.</summary>
[Property, Group( "Ending" )] public string EndCardTitle { get; set; } = "BINGLE";
[Property, Group( "Ending" )] public string EndCardLine { get; set; } = "They take you to meet the public. You have prepared nothing.";
/// <summary>Seconds the card stays up before the next scene loads.</summary>
[Property, Group( "Ending" )] public float EndCardSeconds { get; set; } = 4f;
/// <summary>Scene loaded when the opening ends. Empty to stay in the cave.</summary>
[Property, Group( "Ending" )] public string NextScene { get; set; } = "scenes/stadium.scene";
/// <summary>True while the closing card is on screen. The HUD draws it.</summary>
public bool EndCardVisible { get; private set; }
TimeSince _endCardShown;
TimeSince _sinceEndingCheck = 10f;
int _endingBeat;
void UpdateEnding()
{
if ( Stage < StoryStage.Envoy ) return;
// The envoy and the skin are other components' business; just watch for the outcome.
if ( _sinceEndingCheck > 0.25f )
{
_sinceEndingCheck = 0;
if ( Stage == StoryStage.Envoy && Scene.GetAllComponents<Envoy>().Any( e => e.IsDead || (e.Components.Get<Health>()?.IsDead ?? false) ) ) Advance( StoryStage.EnvoyDead );
if ( Stage == StoryStage.EnvoyDead && Scene.GetAllComponents<SkinWearer>().Any( w => w.IsWearing ) ) Advance( StoryStage.Skinned );
}
if ( Stage == StoryStage.EnvoyDead )
{
if ( _endingBeat == 0 && TimeInStage > 1.2f ) { _endingBeat = 1; EscortSays( PanicLine, 4.5f ); }
if ( _endingBeat == 1 && TimeInStage > 9f ) { _endingBeat = 2; if ( !MessageVisible ) ShowMessage( BodyHint, 5f ); }
}
else if ( Stage == StoryStage.Skinned )
{
// They only welcome "the envoy" back if nothing has given the disguise away.
if ( _endingBeat < 3 && TimeInStage > 4.5f ) { _endingBeat = 3; if ( !Escort.CoverBlown ) EscortSays( FooledLine, 5.5f ); }
if ( _endingBeat == 3 && TimeInStage > 12f ) { _endingBeat = 4; ShowMessage( ClosingLine, 7f ); }
if ( _endingBeat == 4 && TimeInStage > 21f ) { _endingBeat = 5; EndCardVisible = true; _endCardShown = 0; }
if ( _endingBeat == 5 && _endCardShown > EndCardSeconds )
{
_endingBeat = 6;
// On to the next scene, if there is one. The card stays up until it has loaded.
if ( !string.IsNullOrEmpty( NextScene ) ) Game.ChangeScene( NextScene );
else EndCardVisible = false;
}
}
}
void EscortSays( string line, float seconds )
{
if ( string.IsNullOrEmpty( line ) ) return;
ShowMessage( line, seconds, "ESCORT" );
var escort = Scene.GetAllComponents<Escort>().FirstOrDefault( e => !e.IsDead );
if ( escort.IsValid() ) StorySound.Play( EscortVoice, escort.WorldPosition + Vector3.Up * 64f );
}
}