A finale layer that builds a long winding tunnel out of boxes, lights and ribbed frames, with three valves that only open when the whole procession is gathered.
using System;
using System.Collections.Generic;
/// <summary>
/// Layer one, THE THROAT: a long winding tunnel going down. It starts as wet rock and turns,
/// rib by rib, into something that is not rock: warm, ribbed, swelling in slow waves that run
/// down it the way a swallow does.
///
/// The play idea is the procession. Three valves close the tunnel, and a valve does not open for
/// one creature: it opens when everybody still walking is gathered in front of it. Run ahead and
/// you wait; walk with them and it opens as you arrive. It is the first hint of what the planet
/// counts as "one thing".
///
/// Signals die here, where you can see it: words out of the misfits' lost orders drift down the
/// tunnel as glowing motes, curve into the walls and are gone, and the rib that took them warms.
/// </summary>
public sealed class FinaleThroat : FinaleLayer
{
// ---- Tuning ---------------------------------------------------------------------------------
public int Segments = 20;
/// <summary>Length of one piece of tunnel over the ground, and how far it drops.</summary>
public float SegmentLength = 520f, SegmentDrop = 105f;
/// <summary>How far the tunnel swings from side to side, in degrees.</summary>
public float Meander = 20f;
/// <summary>
/// Half the tunnel's width, and its height. Wide and tall enough that the camera (which sits
/// about 190 inches back in the open, and closer in here: see <see cref="CameraZoom"/>) has
/// room behind the lizard whichever way it is turned from the middle of the floor.
/// </summary>
public float HalfWidth = 290f, Height = 370f;
/// <summary>The rock gives way to something else from this segment on.</summary>
public int FleshFrom = 6;
/// <summary>Which joints of the tunnel have a valve across them.</summary>
public int[] ValveAt = { 6, 12, 18 };
/// <summary>A valve feels for the procession from this far off, and wants everybody within <see cref="GatherRadius"/> of the player.</summary>
public float ValveReach = 420f, GatherRadius = 520f;
/// <summary>Seconds everybody has to be gathered before it opens, and seconds it takes to open.</summary>
public float GatherSeconds = 1.2f, ValveOpenSeconds = 1.4f;
public int Motes = 14;
/// <summary>How many of the motes carry readable words.</summary>
public int WordMotes = 5;
public float MoteSpeed = 150f;
/// <summary>Size of a signal's glowing ball, and of the letters of its words, in inches.</summary>
public float MoteRadius = 4f, WordHeight = 13f;
static readonly Color Rock = new( 0.13f, 0.15f, 0.17f );
static readonly Color Flesh = new( 0.5f, 0.17f, 0.13f );
static readonly Color FleshHot = new( 0.95f, 0.36f, 0.2f );
static readonly Color CaveLight = new( 0.25f, 0.6f, 0.65f );
static readonly Color WarmLight = new( 1f, 0.42f, 0.2f );
public override string Title => "I. THE THROAT";
public override string CardLine => "Signals die here.";
public override string Objective => "Lead them down. Keep together.";
public override IReadOnlyList<string> Chatter => FinaleWriting.ThroatChatter;
/// <summary>A tunnel: the camera comes in closer, so it is not forever being pushed in by the walls.</summary>
public override float CameraZoom => 0.72f;
sealed class Valve
{
public GameObject Wall;
public FinalePulser Pulser;
public Vector3 At;
public float Along; // distance down the tunnel
public float Gathered; // seconds everybody has been here
public float Opening = -1f; // seconds since it started to open, below 0 while shut
public bool Open;
public Vector3 Scale;
}
sealed class Mote
{
public GameObject Go;
public TextRenderer Words;
public PointLight Light;
public Vector3 Position, Drift;
public Color Colour;
public float Life, Dying = -1f;
}
readonly List<Vector3> _nodes = new();
readonly List<float> _along = new(); // distance down the tunnel at each node
readonly List<Valve> _valves = new();
readonly List<Mote> _motes = new();
float _total;
bool _saidNotRock, _saidShut, _saidOpened, _saidSignals;
float _sinceSwallow;
// ---- Building -------------------------------------------------------------------------------
public override void Build()
{
var s = Space;
s.Air( new Color( 0.05f, 0.05f, 0.06f ), new Color( 0.02f, 0.012f, 0.012f ), 250f, 2600f );
s.WaveSpeed = 1.7f;
// The path: joints going steadily down, swinging gently from side to side.
var at = s.Origin;
float distance = 0f;
for ( int i = 0; i <= Segments; i++ )
{
_nodes.Add( at );
_along.Add( distance );
float yaw = Meander * MathF.Sin( i * 0.55f );
var step = Rotation.FromYaw( yaw ).Forward * SegmentLength + Vector3.Down * SegmentDrop;
at += step;
distance += step.Length;
}
_total = _along[Segments];
for ( int i = 0; i < Segments; i++ ) BuildSegment( i );
BuildEnds();
foreach ( int joint in ValveAt )
if ( joint > 0 && joint < Segments ) BuildValve( joint );
BuildMotes();
// Far enough in that the whole column has room to stand behind the player.
Start = PointAt( 160f + Procession.Count * Procession.Spacing, out var facing ) + Vector3.Up * 4f;
StartYaw = MathF.Atan2( facing.y, facing.x ) * (180f / MathF.PI);
PulseFrom = _nodes[Segments];
}
/// <summary>How far the rock has turned into something else at a segment, 0 to 1.</summary>
float Turned( int segment ) => Math.Clamp( (segment - FleshFrom + 2) / 6f, 0f, 1f );
void Frame( int segment, out Vector3 middle, out Vector3 forward, out Vector3 left, out Vector3 up, out float length )
{
var a = _nodes[segment];
var b = _nodes[segment + 1];
middle = (a + b) * 0.5f;
length = (b - a).Length;
forward = (b - a) / length;
left = Vector3.Cross( Vector3.Up, forward ).Normal;
up = Vector3.Cross( forward, left ).Normal;
}
void BuildSegment( int i )
{
var s = Space;
Frame( i, out var middle, out var forward, out var left, out var up, out float length );
var turn = Rotation.LookAt( forward, up );
float turned = Turned( i );
float reach = length + 110f; // overlaps its neighbours, so the bends have no gaps
Color Skin( float shade ) => Color.Lerp( Rock, Flesh, turned ) * (shade * s.R( 0.85f, 1.1f ));
s.Box( "Throat Floor", middle - up * 20f, new Vector3( reach, HalfWidth * 2f + 120f, 40f ), Skin( 0.8f ).WithAlpha( 1f ), true, turn );
s.Box( "Throat Roof", middle + up * (Height + 20f), new Vector3( reach, HalfWidth * 2f + 120f, 40f ), Skin( 0.7f ).WithAlpha( 1f ), true, turn );
s.Box( "Throat Wall", middle + left * (HalfWidth + 20f) + up * (Height * 0.5f), new Vector3( reach, 40f, Height + 80f ), Skin( 1f ).WithAlpha( 1f ), true, turn );
s.Box( "Throat Wall", middle - left * (HalfWidth + 20f) + up * (Height * 0.5f), new Vector3( reach, 40f, Height + 80f ), Skin( 1f ).WithAlpha( 1f ), true, turn );
// Up here it is still a cave: lumps of rock against the walls, and something dripping.
if ( turned < 1f )
{
int lumps = turned < 0.3f ? 3 : 1;
for ( int n = 0; n < lumps; n++ )
{
float side = s.Rng.Next( 2 ) == 0 ? 1f : -1f;
var spot = middle + forward * s.R( -length * 0.4f, length * 0.4f ) + left * side * (HalfWidth - s.R( 0f, 30f ));
s.Ball( "Rock Lump", spot + up * s.R( 0f, 40f ), new Vector3( s.R( 40f, 80f ), s.R( 30f, 60f ), s.R( 30f, 70f ) ), (Rock * s.R( 0.8f, 1.3f )).WithAlpha( 1f ) );
}
}
// Further down: ribs. A frame across the tunnel that swells and warms as the wave passes.
if ( turned > 0f )
{
int ribs = turned >= 1f ? 2 : 1;
for ( int n = 0; n < ribs; n++ )
{
float t = ribs == 1 ? 0f : n - 0.5f;
var centre = middle + forward * (t * length * 0.5f);
float phase = (_along[i] + (t + 0.5f) * length * 0.5f) / 260f;
var cold = (Color.Lerp( Rock, Flesh, turned ) * 1.25f).WithAlpha( 1f );
var hot = Color.Lerp( cold, FleshHot, 0.35f + 0.65f * turned );
float thick = 26f + 14f * turned;
foreach ( float side in new[] { 1f, -1f } )
s.Pulse( s.Box( "Rib", centre + left * side * (HalfWidth - thick * 0.4f) + up * (Height * 0.5f), new Vector3( thick, thick, Height ), cold, false, turn ), hot, phase, 0.1f + 0.1f * turned );
s.Pulse( s.Box( "Rib Arch", centre + up * (Height - thick * 0.4f), new Vector3( thick, HalfWidth * 2f, thick ), cold, false, turn ), hot, phase, 0.1f + 0.1f * turned );
s.Pulse( s.Box( "Rib Ridge", centre + up * 1f, new Vector3( thick, HalfWidth * 2f, 5f ), cold, false, turn ), hot, phase, 0.05f );
}
}
// A light every other segment: cave-cold at the top, warm and beating further down.
if ( i % 2 == 0 )
{
var colour = Color.Lerp( CaveLight, WarmLight, turned ) * (0.55f + 0.6f * turned);
s.Light( "Throat Light", middle + up * (Height * 0.7f), colour, 900f, 0.2f + 0.6f * turned );
}
}
/// <summary>A wall behind the start, and at the bottom the dark hole everything goes down.</summary>
void BuildEnds()
{
var s = Space;
Frame( 0, out _, out var forward, out _, out var up, out _ );
s.Box( "Throat Back", _nodes[0] - forward * 40f + up * (Height * 0.5f), new Vector3( 60f, HalfWidth * 2f + 160f, Height + 120f ), Rock, true, Rotation.LookAt( forward, up ) );
Frame( Segments - 1, out _, out forward, out _, out up, out _ );
var end = _nodes[Segments];
s.Box( "Throat End", end + forward * 60f + up * (Height * 0.5f), new Vector3( 60f, HalfWidth * 2f + 160f, Height + 120f ), new Color( 0.02f, 0.005f, 0.005f ), true, Rotation.LookAt( forward, up ) );
var gullet = s.Ball( "Gullet", end - forward * 40f + up * 6f, new Vector3( 150f, 150f, 6f ), new Color( 0.01f, 0f, 0f ) );
s.Pulse( gullet, new Color( 0.5f, 0.08f, 0.04f ), 0f, 0.15f, 1f );
s.Light( "Gullet Light", end - forward * 60f + up * 120f, WarmLight * 1.4f, 1100f, 1f );
}
void BuildValve( int joint )
{
var s = Space;
Frame( joint, out _, out var forward, out _, out var up, out _ );
var turn = Rotation.LookAt( forward, up );
var at = _nodes[joint];
float turned = Turned( joint );
var skin = (Color.Lerp( Rock, Flesh, MathF.Max( turned, 0.5f ) ) * 1.15f).WithAlpha( 1f );
var wall = s.Box( "Valve", at + up * (Height * 0.5f), new Vector3( 46f, HalfWidth * 2f + 30f, Height + 30f ), skin, true, turn );
// The seam it will open along: a dark line down the face that looks up the tunnel.
// (The wall is a stretched 50 unit cube, so its face is at 25 in its own space.)
var seam = s.Box( "Valve Seam", wall.WorldPosition, Vector3.One, Color.Black, false, turn, wall );
seam.LocalPosition = new Vector3( -25.5f, 0f, 0f );
seam.LocalRotation = Rotation.Identity;
seam.LocalScale = new Vector3( 0.02f, 0.03f, 0.9f );
var pulser = s.Pulse( wall, FleshHot, _along[joint] / 260f, 0.02f );
_valves.Add( new Valve { Wall = wall, Pulser = pulser, At = at, Along = _along[joint], Scale = wall.LocalScale } );
}
void BuildMotes()
{
for ( int i = 0; i < Motes; i++ )
{
var go = Space.Ball( "Signal", Space.Origin, MoteRadius, Color.White );
var mote = new Mote { Go = go };
if ( i < WordMotes )
{
// The ball is a 32 unit sphere scaled down to MoteRadius; the words hang off it, so they are sized back up.
mote.Words = Space.Words( "Signal Words", Space.Origin, "...", WordHeight * 32f / MoteRadius, Color.White, go );
mote.Words.GameObject.LocalPosition = Vector3.Up * (WordHeight * 0.9f * 32f / MoteRadius);
}
if ( i % 4 == 0 )
{
var lamp = new GameObject( go, true, "Signal Light" );
lamp.Flags |= GameObjectFlags.NotSaved;
mote.Light = lamp.Components.Create<PointLight>();
mote.Light.Radius = 220f;
mote.Light.Shadows = false;
}
go.Enabled = false;
_motes.Add( mote );
}
}
// ---- Where things are ---------------------------------------------------------------------------
/// <summary>
/// Finds the piece of tunnel a point is in: how far down the tunnel it is, how far to the left
/// of the middle, and the height of the floor there.
/// </summary>
void Locate( Vector3 point, out float along, out float aside, out float floor, out int segment )
{
float best = float.MaxValue;
along = 0f; aside = 0f; floor = _nodes[0].z; segment = 0;
for ( int i = 0; i < Segments; i++ )
{
var a = _nodes[i];
var b = _nodes[i + 1];
var flat = (b - a).WithZ( 0f );
float length = flat.Length;
var direction = flat / length;
var to = (point - a).WithZ( 0f );
float t = Math.Clamp( Vector3.Dot( to, direction ) / length, 0f, 1f );
var nearest = a + (b - a) * t;
float d = (point - nearest).WithZ( 0f ).LengthSquared;
if ( d >= best ) continue;
best = d;
segment = i;
along = _along[i] + (_along[i + 1] - _along[i]) * t;
aside = Vector3.Dot( to, Vector3.Cross( Vector3.Up, direction ) );
floor = nearest.z;
}
}
/// <summary>The middle of the tunnel floor a distance down it, and the way the tunnel runs there.</summary>
Vector3 PointAt( float along, out Vector3 forward )
{
along = Math.Clamp( along, 0f, _total );
for ( int i = 0; i < Segments; i++ )
{
if ( along > _along[i + 1] && i < Segments - 1 ) continue;
float t = (along - _along[i]) / (_along[i + 1] - _along[i]);
forward = (_nodes[i + 1] - _nodes[i]).Normal;
return Vector3.Lerp( _nodes[i], _nodes[i + 1], Math.Clamp( t, 0f, 1f ) );
}
forward = Vector3.Forward;
return _nodes[Segments];
}
public override float Floor( Vector3 point )
{
Locate( point, out _, out _, out float floor, out _ );
return floor;
}
public override Vector3 Contain( Vector3 point )
{
Locate( point, out float along, out float aside, out float floor, out int segment );
float limit = HalfWidth - 14f;
bool inside = MathF.Abs( aside ) <= limit + 30f && point.z > floor - 60f && point.z < floor + Height + 60f;
if ( inside ) return point;
var middle = PointAt( along, out var forward );
var left = Vector3.Cross( Vector3.Up, forward.WithZ( 0f ).Normal );
return middle + left * Math.Clamp( aside, -limit, limit ) + Vector3.Up * 4f;
}
public override (Vector3 Feet, float Yaw) Checkpoint
{
get
{
// Just above the last valve that has opened.
Valve last = null;
foreach ( var valve in _valves )
if ( valve.Open ) last = valve;
if ( last is null ) return (Start, StartYaw);
var at = PointAt( last.Along + 120f, out var forward );
return (at + Vector3.Up * 4f, MathF.Atan2( forward.y, forward.x ) * (180f / MathF.PI));
}
}
// ---- Playing ------------------------------------------------------------------------------------
public override void Begin()
{
Procession.SayNearest( Player.WorldPosition, FinaleWriting.ThroatBegin, 6f );
}
public override void Update( float dt )
{
var player = Player.WorldPosition;
Locate( player, out float along, out _, out _, out int segment );
UpdateValves( dt, player, along );
UpdateMotes( dt, player, along );
if ( !_saidNotRock && Turned( segment ) >= 0.5f )
{
_saidNotRock = true;
Procession.SayNearest( player, FinaleWriting.ThroatNotRock );
}
// The bottom: the last swallow.
if ( along > _total - 170f ) Done = true;
}
void UpdateValves( float dt, Vector3 player, float along )
{
foreach ( var valve in _valves )
{
if ( valve.Open ) continue;
if ( valve.Opening >= 0f )
{
// It draws down into the floor.
valve.Opening += dt;
float open = Math.Clamp( valve.Opening / MathF.Max( ValveOpenSeconds, 0.1f ), 0f, 1f );
float e = open * open * (3f - 2f * open);
if ( valve.Wall.IsValid() )
{
// (Through its pulser, which is what sets the wall's size every frame.)
valve.Pulser.Scale = new Vector3( valve.Scale.x, valve.Scale.y, valve.Scale.z * (1f - e * 0.97f) );
valve.Wall.WorldPosition = valve.At + Vector3.Up * (Height * 0.5f * (1f - e));
}
if ( open >= 1f )
{
valve.Open = true;
if ( valve.Wall.IsValid() ) valve.Wall.Enabled = false;
if ( !_saidOpened ) { _saidOpened = true; Procession.SayNearest( player, FinaleWriting.ThroatValveOpened, 6.5f ); }
}
continue;
}
float before = valve.Along - along;
if ( before < -40f || before > ValveReach ) { valve.Gathered = 0f; continue; }
// "Gathered" has to leave room for a long column: its tail is a fair way back even when tidy.
float radius = MathF.Max( GatherRadius, Procession.Gap + Procession.Count * Procession.Spacing + 160f );
bool gathered = Procession.Gathered( player, radius );
valve.Gathered = gathered ? valve.Gathered + dt : 0f;
if ( !gathered )
{
Sump.Prompt = "It will not open for one. Wait for the others.";
if ( !_saidShut && before < 220f ) { _saidShut = true; Procession.SayNearest( player, FinaleWriting.ThroatValveShut, 5f ); }
}
else if ( valve.Gathered >= GatherSeconds )
{
valve.Opening = 0f;
Sump.Sfx( "throat_valve", valve.At, 1f );
var rib = Space.NearestPulser( valve.At );
if ( rib is not null ) rib.Flash = 1f;
}
else Sump.Prompt = "It is counting you...";
}
}
/// <summary>The furthest down the tunnel anything can be right now: the nearest shut valve below a point.</summary>
float ShutBelow( float along )
{
float limit = _total;
foreach ( var valve in _valves )
if ( !valve.Open && valve.Along > along && valve.Along < limit ) limit = valve.Along;
return limit;
}
// ---- The signals --------------------------------------------------------------------------------
void UpdateMotes( float dt, Vector3 player, float along )
{
_sinceSwallow += dt;
var followers = Procession.Followers;
foreach ( var mote in _motes )
{
if ( !mote.Go.IsValid() ) continue;
if ( !mote.Go.Enabled )
{
// A new signal: somebody's words, starting in the air a little ahead of or behind the player.
if ( followers.Count == 0 || Space.Rng.NextDouble() > dt * 0.9f ) continue;
var owner = followers[Space.Rng.Next( followers.Count )].Profile;
float from = Math.Clamp( along + Space.R( -350f, 500f ), 60f, ShutBelow( along ) - 60f );
var middle = PointAt( from, out var forward );
var left = Vector3.Cross( Vector3.Up, forward.WithZ( 0f ).Normal );
float side = Space.Rng.Next( 2 ) == 0 ? 1f : -1f;
mote.Position = middle + left * Space.R( -70f, 70f ) + Vector3.Up * Space.R( 70f, 220f );
mote.Drift = forward * MoteSpeed + left * side * Space.R( 28f, 60f ) + Vector3.Up * Space.R( -8f, 14f );
mote.Colour = Color.Lerp( owner?.Primary ?? Color.White, Color.White, 0.35f );
mote.Life = 0f;
mote.Dying = -1f;
mote.Go.Enabled = true;
mote.Go.LocalScale = new Vector3( MoteRadius / 32f );
var renderer = mote.Go.Components.Get<ModelRenderer>();
if ( renderer.IsValid() ) renderer.Tint = mote.Colour;
if ( mote.Words.IsValid() ) { mote.Words.Text = FinaleWriting.Snippet( owner, Space.Rng ); mote.Words.Color = mote.Colour; }
if ( mote.Light.IsValid() ) mote.Light.LightColor = (mote.Colour * 0.8f).WithAlpha( 1f );
continue;
}
mote.Life += dt;
mote.Position += mote.Drift * dt;
mote.Go.WorldPosition = mote.Position + Vector3.Up * (MathF.Sin( mote.Life * 2.3f ) * 5f);
if ( mote.Dying < 0f )
{
// Reached a wall (or drifted too long): the wall takes it.
Locate( mote.Position, out float moteAlong, out float aside, out _, out _ );
if ( MathF.Abs( aside ) > HalfWidth - 28f || mote.Life > 9f || moteAlong > ShutBelow( along ) - 30f )
{
mote.Dying = 0f;
mote.Drift *= 0.15f;
var rib = Space.NearestPulser( mote.Position );
if ( rib is not null ) rib.Flash = 1f;
if ( _sinceSwallow > 1.6f && (mote.Position - player).Length < 900f )
{
_sinceSwallow = 0f;
Sump.Sfx( "signal_swallow", mote.Position, 0.5f );
if ( !_saidSignals && mote.Words.IsValid() ) { _saidSignals = true; Procession.SayNearest( player, FinaleWriting.ThroatSignals, 6f ); }
}
}
continue;
}
// Absorbed: it shrinks to nothing and its words go out.
mote.Dying += dt;
float left01 = MathF.Max( 1f - mote.Dying / 0.7f, 0f );
mote.Go.LocalScale = new Vector3( MoteRadius / 32f * left01 );
if ( mote.Words.IsValid() ) mote.Words.Color = mote.Colour.WithAlpha( left01 );
if ( mote.Light.IsValid() ) mote.Light.LightColor = (mote.Colour * (0.8f * left01)).WithAlpha( 1f );
if ( left01 <= 0f ) mote.Go.Enabled = false;
}
}
}