Second layer of the Sump finale. Builds a run of chambers with columns, nests, membrane gates and a final wall of cells, then runs the layer: a belonging meter derived from player movement and nearby followers, cell waves that notice and grab the player, a hunt system that marks stragglers and carries them to nests, and gates that open only while the player stands still. Sends the player back to the chamber start when pinned too long.
using System;
using System.Collections.Generic;
/// <summary>
/// Layer two, THE DEEP LAYERS: three wide chambers in a row, and in them the planet's cells.
/// They are people: pale, silent, drifting, not angry at anything. They are the stadium crowd
/// (<see cref="CrowdPerson"/>, hosted by <see cref="FinaleCellHost"/>), so when they do come they
/// grab, pin and pile on exactly as the stadium did. But they only come for what does not belong.
///
/// The play idea is BELONGING, kept as one number (<see cref="SumpFinale.Foreign"/>, 0 belongs
/// to 1 does not). Running raises it fast, walking a little, standing still drops it to nothing;
/// walking with the procession close keeps it low; being unseen cuts it. The further from
/// belonging the player is, the further off the cells notice them. Held by them, the way out is
/// to stop: when the number reaches the bottom everybody simply lets go. Fighting works too, and
/// teaches nothing. Pinned for long enough, the player is put back at the start of the chamber.
///
/// The cells also take misfits: whoever falls behind the column is noticed, surrounded, carried
/// to a nest by the wall and left there until the player fetches them. Nobody goes on without
/// them: the membranes between chambers stay shut while anybody is missing, and open only for
/// somebody standing still in front of them. The last doorway is a wall of cells, and it parts
/// only for somebody standing calmly among them. That is the answer the heart wants, learned
/// here first.
///
/// The grafts all count: unseen (<see cref="Patchwork.HiddenFrom"/>) hides the player from a
/// cell, a price tag freezes one (tag "sump_priced"), the magnet point draws them off to stare at
/// nothing (<see cref="Patchwork.MagnetPoint"/>), and the body door goes where it likes.
/// </summary>
public sealed class FinaleDeep : FinaleLayer
{
// ---- Tuning: the place ------------------------------------------------------------------------
public int Chambers = 3;
public float ChamberLength = 2200f, WallThick = 200f, HalfWidth = 900f, Height = 520f;
public float DoorHalf = 150f, DoorHeight = 270f, ExitLength = 520f;
// ---- Tuning: belonging ------------------------------------------------------------------------
/// <summary>Slower than this, with no keys held, is standing still.</summary>
public float StillSpeed = 25f;
/// <summary>Faster than this is running.</summary>
public float RunSpeed = 345f;
/// <summary>Where the number settles while walking (0 belongs, 1 does not).</summary>
public float WalkForeign = 0.45f;
/// <summary>How fast it rises and falls, per second.</summary>
public float RiseRate = 0.55f, FallRate = 0.5f;
/// <summary>Each follower close by takes this much off, up to a limit. More healed, easier.</summary>
public float PerFollower = 0.05f, FollowerLimit = 0.3f, FollowerRadius = 380f;
/// <summary>Unseen (a graft), the number is multiplied by this.</summary>
public float UnseenFactor = 0.3f;
/// <summary>A cell notices the player from this far at 0 and at 1.</summary>
public float NoticeNear = 80f, NoticeFar = 720f;
/// <summary>Below this nobody new comes; below <see cref="LetGoBelow"/> everybody lets go.</summary>
public float NoticeAbove = 0.12f, LetGoBelow = 0.08f;
// ---- Tuning: the cells ------------------------------------------------------------------------
/// <summary>Cells in the first and second wave of each chamber.</summary>
public int[] FirstWave = { 5, 6, 7 }, SecondWave = { 4, 5, 6 };
/// <summary>The second wave comes this long after the first, or when the player is halfway.</summary>
public float SecondWaveAfter = 26f;
public float DriftSpeed = 38f, ChaseSpeed = 125f, FetchSpeed = 105f;
/// <summary>Pinned this long, the player is carried back to the start of the chamber.</summary>
public float PinSeconds = 9f;
public int ClotCells = 9;
// ---- Tuning: taking misfits -------------------------------------------------------------------
/// <summary>Seconds between one misfit being noticed and the next.</summary>
public float HuntEvery = 16f;
/// <summary>A follower this much further back than the end of a tidy column has fallen behind.</summary>
public float LonelySlack = 330f;
/// <summary>Two cells this close for this long, and it is taken (twice as long with a friend beside it).</summary>
public float TakeRadius = 78f, TakeSeconds = 1.6f;
/// <summary>Reach a noticed misfit within this and the cells think better of it; reach a taken one within <see cref="FreeRadius"/> to free it.</summary>
public float RescueRadius = 190f, FreeRadius = 135f;
// ---- Tuning: the doors ------------------------------------------------------------------------
public float MembraneRadius = 240f, MembraneSeconds = 2.2f;
public float ClotRadius = 200f, ClotSeconds = 4.5f, ClotCalm = 0.15f;
static readonly Color Wall = new( 0.42f, 0.2f, 0.19f );
static readonly Color WallHot = new( 0.85f, 0.4f, 0.32f );
static readonly Color FloorTint = new( 0.2f, 0.09f, 0.09f );
static readonly Color Pale = new( 0.93f, 0.91f, 0.88f );
static readonly Color PaleLight = new( 0.95f, 0.75f, 0.72f );
public override string Title => "II. THE DEEP LAYERS";
public override string CardLine => "They are not angry. They are thorough.";
public override IReadOnlyList<string> Chatter => FinaleWriting.DeepChatter;
public override string Objective
{
get
{
int filled = (int)MathF.Round( Math.Clamp( Finale.Foreign, 0f, 1f ) * 10f );
return $"Cross the deep layers. Walk. Leave nobody. | NOT BELONGING [{new string( '#', filled )}{new string( '-', 10 - filled )}]";
}
}
sealed class Gate
{
public GameObject Membrane;
public Vector3 Front; // where to stand
public float X; // far side of the wall, local
public float Held;
public bool Open, Clot;
}
sealed class Hunt
{
public FinaleFollower Victim;
public readonly List<FinaleCell> Cells = new();
public Vector3 Nest;
public float Grip;
}
FinaleCellHost _host;
readonly List<FinaleCell> _cells = new();
readonly List<FinaleCell> _clot = new();
readonly List<Gate> _gates = new();
readonly List<Vector3> _nests = new();
Hunt _hunt;
float _nextHunt = 10f;
int _chamber = -1;
int _wave; // waves sent in the current chamber
float _sinceWave;
bool _saidNoticed, _saidGrabbed, _saidCalmed, _saidMembrane, _saidClot;
bool _sending;
float Length => Chambers * (ChamberLength + WallThick) + ExitLength;
float X0( int chamber ) => chamber * (ChamberLength + WallThick);
float X1( int chamber ) => X0( chamber ) + ChamberLength;
Vector3 P( float x, float y, float z = 0f ) => Space.Origin + new Vector3( x, y, z );
// ---- Building -------------------------------------------------------------------------------
public override void Build()
{
var s = Space;
s.Air( new Color( 0.1f, 0.07f, 0.07f ), new Color( 0.06f, 0.02f, 0.02f ), 500f, 4200f );
s.WaveSpeed = 1.1f;
float length = Length;
s.Box( "Deep Floor", P( length * 0.5f, 0f, -20f ), new Vector3( length + 120f, HalfWidth * 2f + 120f, 40f ), FloorTint );
s.Box( "Deep Roof", P( length * 0.5f, 0f, Height + 20f ), new Vector3( length + 120f, HalfWidth * 2f + 120f, 40f ), (Wall * 0.6f).WithAlpha( 1f ) );
foreach ( float side in new[] { 1f, -1f } )
s.Box( "Deep Wall", P( length * 0.5f, side * (HalfWidth + 20f), Height * 0.5f ), new Vector3( length + 120f, 40f, Height + 80f ), Wall );
s.Box( "Deep Back", P( -20f, 0f, Height * 0.5f ), new Vector3( 40f, HalfWidth * 2f + 120f, Height + 80f ), Wall );
s.Box( "Deep End", P( length + 20f, 0f, Height * 0.5f ), new Vector3( 40f, HalfWidth * 2f + 120f, Height + 80f ), new Color( 0.03f, 0.01f, 0.01f ) );
// Ribs up both walls, swelling in a slow wave that runs the way you are going.
for ( float x = 130f; x < length; x += 260f )
foreach ( float side in new[] { 1f, -1f } )
s.Pulse( s.Box( "Deep Rib", P( x, side * (HalfWidth - 10f), Height * 0.5f ), new Vector3( 34f, 34f, Height ), (Wall * 1.15f).WithAlpha( 1f ), false ), WallHot, x / 330f, 0.14f );
for ( int k = 0; k < Chambers; k++ ) BuildChamber( k );
// The way on: a short dark passage with the heart's light at the end of it.
float exit = X1( Chambers - 1 ) + WallThick;
s.Light( "Heart Glow", P( exit + ExitLength - 60f, 0f, 140f ), new Color( 1f, 0.3f, 0.12f ) * 1.6f, 1000f, 1f );
var gullet = s.Ball( "Way Down", P( exit + ExitLength - 110f, 0f, 4f ), new Vector3( 170f, 170f, 5f ), new Color( 0.02f, 0f, 0f ) );
s.Pulse( gullet, new Color( 0.6f, 0.1f, 0.05f ), 0f, 0.12f, 1f );
_host = s.Root.Components.Create<FinaleCellHost>();
_host.Centre = P( length * 0.5f, 0f );
_host.HalfX = length * 0.5f;
_host.HalfY = HalfWidth;
_host.FloorZ = s.Origin.z;
_host.Adopt( Player );
_host.Live = false;
BuildClot();
// Far enough in that the whole column has room to stand behind the player.
Start = P( 160f + Procession.Count * Procession.Spacing, 0f, 4f );
StartYaw = 0f;
PulseFrom = P( length, 0f, 100f );
}
void BuildChamber( int k )
{
var s = Space;
float x0 = X0( k ), x1 = X1( k );
// Columns to walk round (and to lose sight of each other behind).
for ( int i = 0; i < 6; i++ )
{
float x = x0 + 250f + i * (ChamberLength - 500f) / 5f + s.R( -90f, 90f );
float y = (i % 2 == 0 ? 1f : -1f) * s.R( 240f, HalfWidth - 220f );
float girth = s.R( 70f, 120f );
var column = s.Box( "Column", P( x, y, Height * 0.5f ), new Vector3( girth, girth, Height ), (Wall * s.R( 0.85f, 1.1f )).WithAlpha( 1f ), true, Rotation.FromYaw( s.R( 0f, 90f ) ) );
s.Pulse( s.Ball( "Column Swelling", column.WorldPosition.WithZ( s.Origin.z + s.R( 150f, 330f ) ), girth * 0.78f, Wall * 1.1f ), WallHot, x / 330f, 0.12f );
}
foreach ( float t in new[] { 0.25f, 0.75f } )
s.Light( "Deep Light", P( x0 + ChamberLength * t, 0f, Height * 0.75f ), PaleLight * 0.75f, 1500f, 0.45f );
// A nest by one wall: where a taken misfit is put. Pale lumps in a ring, like eggs.
float side = k % 2 == 0 ? 1f : -1f;
var nest = P( x0 + ChamberLength * 0.5f + s.R( -300f, 300f ), side * (HalfWidth - 120f) );
_nests.Add( nest );
for ( int i = 0; i < 7; i++ )
{
var at = nest + Rotation.FromYaw( i * 360f / 7f ).Forward * 78f;
s.Pulse( s.Ball( "Nest Lump", at + Vector3.Up * 10f, new Vector3( 22f, 22f, 16f ), Pale * 0.8f ), Pale, i * 0.9f, 0.2f, 0.5f );
}
s.Light( "Nest Light", nest + Vector3.Up * 90f, Pale * 0.5f, 380f, 0.3f );
// The wall at the far end, with one doorway and a membrane across it.
float mid = x1 + WallThick * 0.5f;
float wing = HalfWidth - DoorHalf;
foreach ( float way in new[] { 1f, -1f } )
s.Box( "Partition", P( mid, way * (DoorHalf + wing * 0.5f), Height * 0.5f ), new Vector3( WallThick, wing, Height ), Wall );
s.Box( "Lintel", P( mid, 0f, DoorHeight + (Height - DoorHeight) * 0.5f ), new Vector3( WallThick, DoorHalf * 2f, Height - DoorHeight ), Wall );
var membrane = s.Box( "Membrane", P( mid, 0f, DoorHeight * 0.5f ), new Vector3( WallThick * 0.4f, DoorHalf * 2f, DoorHeight ), new Color( 0.8f, 0.45f, 0.42f ) );
s.Pulse( membrane, new Color( 1f, 0.7f, 0.62f ), x1 / 330f, 0.03f, 0.6f );
_gates.Add( new Gate { Membrane = membrane, Front = P( x1 - 70f, 0f ), X = x1 + WallThick, Clot = k == Chambers - 1 } );
}
/// <summary>The last doorway: cells standing shoulder to shoulder in front of it, facing whoever comes.</summary>
void BuildClot()
{
float x1 = X1( Chambers - 1 );
for ( int i = 0; i < ClotCells; i++ )
{
int row = i / 3, column = i % 3;
var spot = P( x1 - 55f - row * 44f, (column - 1) * 50f + (row % 2) * 12f );
var cell = SpawnCell( spot, 180f, Chambers - 1 );
cell.Standing = true;
cell.Spot = spot;
cell.GoTo( spot, 60f, P( x1 - 600f, 0f ) );
_clot.Add( cell );
}
}
FinaleCell SpawnCell( Vector3 at, float yaw, int chamber )
{
var s = Space;
var shirt = Color.Lerp( Pale, new Color( 0.75f, 0.8f, 0.85f ), s.R( 0f, 0.5f ) );
var trousers = Color.Lerp( Pale, new Color( 0.6f, 0.6f, 0.62f ), s.R( 0.2f, 0.7f ) );
var cell = FinaleCell.Spawn( s.Root, _host, at, yaw, shirt, trousers, ChaseSpeed );
cell.Chamber = chamber;
_cells.Add( cell );
return cell;
}
// ---- Where things are -------------------------------------------------------------------------
public override float Floor( Vector3 point ) => Space.Origin.z;
public override Vector3 Contain( Vector3 point )
{
var local = point - Space.Origin;
float x = Math.Clamp( local.x, 12f, Length - 12f );
float y = Math.Clamp( local.y, -HalfWidth + 12f, HalfWidth - 12f );
float z = Math.Clamp( local.z, -30f, Height );
if ( MathF.Abs( x - local.x ) < 40f && MathF.Abs( y - local.y ) < 40f && MathF.Abs( z - local.z ) < 80f ) return point;
return P( x, y, 4f );
}
int ChamberOf( float localX ) => Math.Clamp( (int)MathF.Floor( localX / (ChamberLength + WallThick) ), 0, Chambers - 1 );
public override (Vector3 Feet, float Yaw) Checkpoint => _chamber <= 0 ? (Start, StartYaw) : (P( X0( _chamber ) + 150f, 0f, 4f ), 0f);
public override void SentBack()
{
_sending = false;
_host.DropAll();
_host.Grace( 5f );
Finale.Foreign = 0.15f;
foreach ( var cell in _cells )
if ( cell.Alive && cell.Chasing ) Drift( cell, true );
}
public override void End()
{
if ( _host.IsValid() ) { _host.Live = false; _host.DropAll(); }
}
// ---- Playing ----------------------------------------------------------------------------------
public override void Begin()
{
_host.Live = true;
Procession.HurryDistance = 260f;
Procession.SayNearest( Player.WorldPosition, FinaleWriting.DeepEnter, 6f );
}
public override void Update( float dt )
{
var player = Player.WorldPosition;
var local = player - Space.Origin;
EnterChamber( ChamberOf( local.x ) );
UpdateWaves( dt, local );
UpdateBelonging( dt, player );
UpdateCells( dt, player );
UpdateHunt( dt, player );
UpdateGates( dt, player, local );
// Held: say what to do about it. (It is the opposite of what the stadium taught.)
if ( _host.Held )
{
Sump.Prompt = _host.Pinned ? "PINNED. Stop fighting. Be still, and they let go." : "They have hold of you. Be still. (Or shake them off, if you must.)";
if ( !_saidGrabbed ) { _saidGrabbed = true; Procession.SayNearest( player, FinaleWriting.DeepGrabbed, 7f ); Sump.Sfx( "cell_grab", player, 0.8f ); }
}
if ( !_sending && _host.PinnedSeconds >= PinSeconds )
{
_sending = true;
Finale.SendBack( "THEY PUT YOU OUT", "Not far. Not hard. The way you put a spider outside." );
}
if ( local.x > X1( Chambers - 1 ) + WallThick + ExitLength - 190f ) Done = true;
}
/// <summary>The player has walked into a chamber: the one behind empties, and this one starts to wake.</summary>
void EnterChamber( int chamber )
{
if ( chamber == _chamber ) return;
_chamber = chamber;
_wave = 0;
_sinceWave = 0f;
// The cells of the chambers behind sink back into the walls (the ones carrying somebody stay).
foreach ( var cell in _cells )
{
if ( cell.Chamber >= chamber || cell.Standing || cell.Prey is not null || !cell.Person.IsValid() ) continue;
if ( cell.Person.State == CrowdState.Holding ) continue;
cell.Person.GameObject.Destroy();
}
_cells.RemoveAll( c => !c.Person.IsValid() );
}
void UpdateWaves( float dt, Vector3 local )
{
if ( _chamber < 0 ) return;
_sinceWave += dt;
float into = local.x - X0( _chamber );
if ( _wave == 0 && into > 120f ) SendWave( FirstWave );
else if ( _wave == 1 && (_sinceWave > SecondWaveAfter || into > ChamberLength * 0.5f) ) SendWave( SecondWave );
}
/// <summary>A wave comes out of the walls: each cell steps out of the side of the chamber and drifts inward.</summary>
void SendWave( int[] sizes )
{
_wave++;
_sinceWave = 0f;
int count = sizes[Math.Min( _chamber, sizes.Length - 1 )];
float x0 = X0( _chamber ), x1 = X1( _chamber );
// Never right on top of the doorway the player has just come through.
float from = _wave == 1 ? x0 + 600f : x0 + 200f;
for ( int i = 0; i < count; i++ )
{
float side = i % 2 == 0 ? 1f : -1f;
float x = Space.R( from, x1 - 200f );
var at = P( x, side * (HalfWidth - 46f) );
var cell = SpawnCell( at, side > 0f ? -90f : 90f, _chamber );
cell.Home = P( Math.Clamp( x + Space.R( -300f, 300f ), x0 + 200f, x1 - 200f ), Space.R( -HalfWidth + 250f, HalfWidth - 250f ) );
cell.Roam = 520f;
cell.GoTo( cell.Home, DriftSpeed * 1.6f );
cell.NextWander = Time.Now + Space.R( 6f, 12f );
var rib = Space.NearestPulser( at + Vector3.Up * 200f );
if ( rib is not null ) rib.Flash = 1f;
}
Sump.Sfx( "cell_emerge", P( (x0 + x1) * 0.5f, 0f, 60f ), 0.9f );
}
/// <summary>How much the player does not belong, from how they are moving and who is with them.</summary>
void UpdateBelonging( float dt, Vector3 player )
{
float speed = Player.Velocity.Length;
bool keys = !Player.InputLocked && Input.AnalogMove.Length > 0.2f;
bool still = speed < StillSpeed && !keys;
bool running = speed > RunSpeed;
float target = still ? 0f : running ? 1f : WalkForeign;
if ( !still && !running )
target -= MathF.Min( Procession.CountNear( player, FollowerRadius ) * PerFollower, FollowerLimit );
if ( _host.Held && !still ) target = MathF.Max( target, 0.8f ); // struggling is not belonging
var patchwork = Patchwork.Of( Player );
if ( patchwork.IsValid() && patchwork.Unseen ) target *= UnseenFactor;
target = Math.Clamp( target, 0f, 1f );
float foreign = Finale.Foreign;
foreign = target > foreign ? MathF.Min( foreign + RiseRate * dt, target ) : MathF.Max( foreign - FallRate * dt, target );
Finale.Foreign = foreign;
if ( still ) Finale.StillSeconds += dt;
if ( running ) Finale.RanSeconds += dt;
// The lesson: stop, and they let go.
if ( foreign <= LetGoBelow && _host.Held )
{
_host.Calm();
Finale.Calmed++;
Sump.Sfx( "cell_calm", player, 0.8f );
if ( !_saidCalmed ) { _saidCalmed = true; Procession.SayNearest( player, FinaleWriting.DeepCalmed, 7f ); }
}
}
void UpdateCells( float dt, Vector3 player )
{
float foreign = Finale.Foreign;
float notice = Lerp( NoticeNear, NoticeFar, foreign );
var magnet = Patchwork.MagnetPoint;
float now = Time.Now;
foreach ( var cell in _cells )
{
if ( !cell.Alive ) continue;
cell.Pale( Pale, 0.82f );
if ( !cell.Awake || cell.Standing || cell.Prey is not null ) continue;
if ( cell.Person.State == CrowdState.Holding ) continue; // lets go through the host, when the player is calm
var at = cell.Position;
// A magnet point: something they are sure does not belong, and cannot find.
if ( magnet is { } pull && (pull - at).WithZ( 0f ).Length < Patchwork.MagnetRadius )
{
cell.GoTo( pull.WithZ( at.z ), FetchSpeed );
continue;
}
float distance = (player - at).WithZ( 0f ).Length;
bool hidden = Patchwork.HiddenFrom( cell.Eye, cell.Facing );
bool wants = cell.Chasing
? !hidden && foreign > LetGoBelow && distance < notice * 1.4f + 120f
: !hidden && foreign > NoticeAbove && distance < notice;
if ( wants )
{
if ( !cell.Chasing )
{
cell.Chase();
if ( !_saidNoticed ) { _saidNoticed = true; Procession.SayNearest( player, FinaleWriting.DeepNoticed, 7f ); Sump.Sfx( "cell_notice", at, 0.8f ); }
}
continue;
}
if ( cell.Chasing || now >= cell.NextWander || cell.Person.AtGoal ) Drift( cell, cell.Chasing );
}
}
/// <summary>Back to drifting about its patch of the chamber.</summary>
void Drift( FinaleCell cell, bool now )
{
if ( !now && Time.Now < cell.NextWander ) return;
var spot = cell.Home + new Vector3( Space.R( -cell.Roam, cell.Roam ), Space.R( -cell.Roam, cell.Roam ), 0f );
var local = spot - Space.Origin;
float x0 = X0( cell.Chamber ) + 120f, x1 = X1( cell.Chamber ) - 120f;
spot = P( Math.Clamp( local.x, x0, x1 ), Math.Clamp( local.y, -HalfWidth + 120f, HalfWidth - 120f ) );
cell.GoTo( spot, DriftSpeed );
cell.NextWander = Time.Now + Space.R( 5f, 11f );
}
// ---- Taking misfits ---------------------------------------------------------------------------
void UpdateHunt( float dt, Vector3 player )
{
if ( _hunt is null )
{
_nextHunt -= dt;
if ( _nextHunt <= 0f ) _nextHunt = StartHunt( player ) ? HuntEvery : 2.5f;
return;
}
var hunt = _hunt;
var victim = hunt.Victim;
hunt.Cells.RemoveAll( c => !c.Awake );
if ( hunt.Cells.Count == 0 || victim.Free )
{
// Nobody left to hold it (priced, gone, or it was freed some other way).
EndHunt( false );
return;
}
float toPlayer = (player - victim.Position).WithZ( 0f ).Length;
switch ( victim.State )
{
case FollowerState.Marked:
{
foreach ( var cell in hunt.Cells ) cell.GoTo( victim.Position, FetchSpeed );
// Company is what saves it: the player, or two of the others catching up.
if ( toPlayer < RescueRadius || Procession.CountNear( victim.Position, 150f, victim ) >= 2 || victim.StateTime > 25f )
{
Procession.Say( victim, "They changed their minds. I was standing next to somebody. That is all it took.", 6f );
EndHunt( false );
return;
}
int close = 0;
foreach ( var cell in hunt.Cells )
if ( (cell.Position - victim.Position).WithZ( 0f ).Length < TakeRadius ) close++;
bool friend = Procession.CountNear( victim.Position, 150f, victim ) >= 1;
hunt.Grip = close >= 2 ? hunt.Grip + dt * (friend ? 0.5f : 1f) : MathF.Max( hunt.Grip - dt, 0f );
if ( hunt.Grip >= TakeSeconds )
{
victim.Set( FollowerState.Carried );
Finale.Taken++;
Sump.Say( "", $"{victim.Name} is carried off. Not hurt. Put away for later.", 6f );
Sump.Sfx( "misfit_taken", victim.Position, 1f );
}
break;
}
case FollowerState.Carried:
{
foreach ( var cell in hunt.Cells ) cell.GoTo( hunt.Nest, FetchSpeed * 0.9f );
var carrier = hunt.Cells[0].Position;
victim.Position = Vector3.Lerp( victim.Position, carrier + Vector3.Up * 46f, Math.Clamp( dt * 6f, 0f, 1f ) );
if ( toPlayer < FreeRadius ) { Free(); return; }
if ( (carrier - hunt.Nest).WithZ( 0f ).Length < 95f || victim.StateTime > 16f )
{
victim.Position = hunt.Nest;
victim.Set( FollowerState.Held );
}
break;
}
case FollowerState.Held:
{
// They stand round it, looking at it, with nothing further in mind.
for ( int i = 0; i < hunt.Cells.Count; i++ )
{
var spot = hunt.Nest + Rotation.FromYaw( 200f + i * 80f ).Forward * 105f;
hunt.Cells[i].GoTo( spot, DriftSpeed * 1.5f, hunt.Nest );
}
if ( toPlayer < FreeRadius ) { Free(); return; }
if ( toPlayer < 600f ) Sump.Prompt = $"Fetch {victim.Name}. Walk in. Do not run.";
break;
}
}
}
/// <summary>Picks whoever has fallen furthest behind and sends the nearest cells for it. False if nobody has.</summary>
bool StartHunt( Vector3 player )
{
if ( Procession.Count == 0 || _chamber < 0 ) return false;
float tidy = Procession.Gap + (Procession.Count - 1) * Procession.Spacing + LonelySlack;
FinaleFollower victim = null;
float furthest = tidy;
foreach ( var follower in Procession.Followers )
{
if ( follower.State != FollowerState.Following ) continue;
float d = (follower.Position - player).WithZ( 0f ).Length;
if ( d > furthest ) { furthest = d; victim = follower; }
}
if ( victim is null ) return false;
var hunt = new Hunt { Victim = victim };
var free = new List<FinaleCell>();
foreach ( var cell in _cells )
if ( cell.Awake && !cell.Standing && cell.Prey is null && !cell.Chasing && cell.Person.State == CrowdState.Standing
&& (cell.Position - victim.Position).WithZ( 0f ).Length < 1200f ) free.Add( cell );
if ( free.Count < 2 ) return false;
free.Sort( ( a, b ) => (a.Position - victim.Position).LengthSquared.CompareTo( (b.Position - victim.Position).LengthSquared ) );
for ( int i = 0; i < Math.Min( 3, free.Count ); i++ )
{
free[i].Prey = victim;
hunt.Cells.Add( free[i] );
}
// The nest of whichever chamber it is standing in.
int chamber = ChamberOf( (victim.Position - Space.Origin).x );
hunt.Nest = _nests[Math.Clamp( chamber, 0, _nests.Count - 1 )];
victim.Set( FollowerState.Marked );
Procession.Say( victim, FinaleWriting.DeepMarked, 6f );
Sump.Sfx( "cell_notice", victim.Position, 0.9f );
_hunt = hunt;
return true;
}
void Free()
{
var victim = _hunt.Victim;
Finale.Freed++;
Sump.Sfx( "misfit_freed", victim.Position, 1f );
EndHunt( true );
Procession.Say( victim, FinaleWriting.DeepFreed, 6.5f );
}
/// <summary>The cells lose interest in the misfit and it walks again.</summary>
void EndHunt( bool freed )
{
var hunt = _hunt;
_hunt = null;
_nextHunt = HuntEvery;
if ( hunt is null ) return;
foreach ( var cell in hunt.Cells )
{
cell.Prey = null;
if ( cell.Alive ) Drift( cell, true );
}
if ( !hunt.Victim.Free )
{
hunt.Victim.Position = hunt.Victim.Position.WithZ( Space.Origin.z );
hunt.Victim.Set( FollowerState.Following );
}
if ( freed ) _host.Grace( 2.5f );
}
// ---- The doors --------------------------------------------------------------------------------
void UpdateGates( float dt, Vector3 player, Vector3 local )
{
bool still = Player.Velocity.Length < StillSpeed && (Player.InputLocked || Input.AnalogMove.Length < 0.2f);
foreach ( var gate in _gates )
{
if ( gate.Open ) continue;
float radius = gate.Clot ? ClotRadius + 60f : MembraneRadius;
if ( local.x > gate.X || (player - gate.Front).WithZ( 0f ).Length > radius ) { gate.Held = MathF.Max( gate.Held - dt, 0f ); continue; }
if ( !gate.Clot && !_saidMembrane ) { _saidMembrane = true; Procession.SayNearest( player, FinaleWriting.DeepMembrane, 6f ); }
if ( gate.Clot && !_saidClot ) { _saidClot = true; Procession.SayNearest( player, FinaleWriting.DeepClot, 7f ); }
var missing = Procession.FirstTaken();
if ( missing is not null )
{
gate.Held = 0f;
if ( !_host.Held ) Sump.Prompt = $"Not without {missing.Name}.";
continue;
}
bool calm = still && !_host.Held && (!gate.Clot || Finale.Foreign <= ClotCalm);
float needed = gate.Clot ? ClotSeconds : MembraneSeconds;
gate.Held = calm ? gate.Held + dt : MathF.Max( gate.Held - dt * 1.5f, 0f );
if ( gate.Held >= needed ) { OpenGate( gate, player ); continue; }
if ( _host.Held ) continue;
if ( gate.Clot )
Sump.Prompt = calm ? "Among them. Still. They are getting used to you..." : "They do not move for anything that pushes. Stand among them. Be still.";
else
Sump.Prompt = calm ? "It is opening..." : "It gives a little, then pushes back. Stand still.";
}
}
void OpenGate( Gate gate, Vector3 player )
{
gate.Open = true;
if ( gate.Membrane.IsValid() ) gate.Membrane.Enabled = false;
if ( !gate.Clot )
{
Sump.Sfx( "membrane_open", gate.Front, 1f );
return;
}
// The wall of cells parts down the middle and stands aside.
for ( int i = 0; i < _clot.Count; i++ )
{
var cell = _clot[i];
if ( !cell.Alive ) continue;
int row = i / 3, column = i % 3;
float side = column == 0 ? -1f : column == 2 ? 1f : (row % 2 == 0 ? 1f : -1f);
var aside = cell.Spot + new Vector3( -60f - row * 20f, side * (DoorHalf + 90f + row * 46f), 0f );
cell.Spot = aside;
cell.GoTo( aside, 70f, gate.Front );
}
Finale.Calmed++;
Sump.Sfx( "clot_part", gate.Front, 1f );
Procession.SayNearest( player, FinaleWriting.DeepClotParted, 7f );
}
}