Part of the SumpHub partial class that builds and drives the hub's "way down": a chained hatch over a sinkhole in front of the sealed finale door. Procedurally places kerb stones, a pit with a heart glow light, two sliding plates, chains with padlocks and their fallen counterparts, removes chains as misfits are healed, brightens the glow, and slides the plates apart when the finale unlocks. Adds an interact spot that calls Sump.RequestFinale.
using System;
using System.Collections.Generic;
// The way down: a chained hatch over a sinkhole, in front of the sealed gate the cavern puts at
// SumpCavern.FinaleDoor. Each healed misfit takes chains off it and lets more of the glow from
// underneath out. At SumpState.FinaleNeeds the last chain falls, the two plates slide apart, and
// the player can lead everybody down.
public sealed partial class SumpHub
{
/// <summary>How far in front of the sealed gate (toward the middle of the cavern) the hatch lies.</summary>
[Property, Group( "Way Down" )] public float HatchDistance { get; set; } = 170f;
/// <summary>Chains across the hatch for each misfit still needed.</summary>
[Property, Group( "Way Down" ), Range( 1, 3 )] public int ChainsPerMisfit { get; set; } = 2;
/// <summary>Seconds the plates take to slide open.</summary>
[Property, Group( "Way Down" )] public float HatchOpenSeconds { get; set; } = 3.5f;
[Property, Group( "Way Down" )] public Color HeartGlow { get; set; } = new( 1f, 0.32f, 0.12f );
const float HatchRadius = 88f;
GameObject _downRoot;
GameObject _plateLeft, _plateRight;
readonly List<GameObject> _chains = new();
readonly List<GameObject> _fallenChains = new();
HubFlame _downFlame;
Vector3 _downAt;
float _hatchOpen, _hatchTarget, _hatchShown = -1f;
int _chainsLeft = -1;
void ForgetWayDown()
{
_downRoot = null;
_plateLeft = _plateRight = null;
_chains.Clear();
_fallenChains.Clear();
_downFlame = null;
_hatchOpen = _hatchTarget = 0f;
_hatchShown = -1f;
_chainsLeft = -1;
}
void EnsureWayDown()
{
if ( _downRoot.IsValid() ) return;
var door = SumpCavern.FinaleDoor;
var inward = (SumpCavern.Centre - door).WithZ( 0f );
inward = inward.Length > 1f ? inward.Normal : Vector3.Forward;
_downAt = HubKit.OnFloor( door + inward * HatchDistance );
var b = HubBuilder.At( _root, "Way Down", _downAt, HubKit.YawToward( _downAt, _downAt + inward ) );
_downRoot = b.Root;
var rng = new HubRng( MisfitGenerator.Mix( SumpState.Seed, 0xD0E5 ) );
// A kerb of rock round the hole.
const int stones = 12;
for ( int i = 0; i < stones; i++ )
{
float yaw = i * 360f / stones + rng.R( -6f, 6f );
var at = Rotation.FromYaw( yaw ).Forward * (HatchRadius + 16f);
b.Box( "Kerb Stone", at + Vector3.Up * 5f, new Vector3( rng.R( 26f, 36f ), rng.R( 48f, 60f ), rng.R( 22f, 34f ) ), HubKit.Shade( HubKit.Rock, rng.R( 0.8f, 1.3f ) ), true,
new Angles( rng.R( -6f, 6f ), yaw, rng.R( -6f, 6f ) ).ToRotation() );
}
// The hole itself, and the light from whatever is at the bottom of it.
b.Disc( "Pit", new Vector3( 0f, 0f, 1.5f ), HatchRadius, 1.5f, new Color( 0.02f, 0.005f, 0.005f ), true );
b.Disc( "Pit Glow", new Vector3( 0f, 0f, 2.6f ), HatchRadius * 0.55f, 1f, HubKit.Shade( HeartGlow, 0.5f ), true );
_downFlame = AddFlame( b.Light( "Heart Light", new Vector3( 0f, 0f, 34f ), HeartGlow, 420f ), HeartGlow, 0.45f, 1.6f );
_plateLeft = BuildPlate( b, "Hatch Plate Left", 1f, rng );
_plateRight = BuildPlate( b, "Hatch Plate Right", -1f, rng );
// Chains over the top, each with its fallen self lying beside the kerb for when it comes off.
int count = Math.Clamp( SumpState.FinaleNeeds * ChainsPerMisfit, 1, 9 );
for ( int i = 0; i < count; i++ )
{
float yaw = 12f + i * 180f / count;
_chains.Add( BuildChain( b, yaw, i ) );
_fallenChains.Add( BuildFallenChain( b, yaw, rng ) );
}
AddSpot( b.Root, _downAt, HatchRadius + 90f, WayDownPrompt, () =>
{
if ( !SumpState.FinaleUnlocked ) return;
Sump.Sfx( "waydown_descend", _downAt, 1f );
Sump.RequestFinale();
} );
}
/// <summary>Half of the hatch: a rusted plate with rivets and a hazard stripe. Side is +1 for left, -1 for right.</summary>
GameObject BuildPlate( HubBuilder b, string name, float side, HubRng rng )
{
var plate = b.Group( name, new Vector3( 0f, side * HatchRadius * 0.5f, 12f ) );
plate.Box( "Plate", Vector3.Zero, new Vector3( HatchRadius * 2f, HatchRadius, 8f ), HubKit.Shade( HubKit.Rust, rng.R( 0.75f, 1f ) ), true );
plate.Box( "Hazard Stripe", new Vector3( 0f, -side * (HatchRadius * 0.5f - 7f), 4.3f ), new Vector3( HatchRadius * 2f - 12f, 8f, 0.6f ), new Color( 0.75f, 0.6f, 0.1f ) );
plate.Box( "Patch", new Vector3( rng.R( -50f, 50f ), side * 8f, 4.3f ), new Vector3( 40f, 30f, 0.8f ), HubKit.Shade( HubKit.Tin, 0.6f ), false, Rotation.FromYaw( rng.R( -20f, 20f ) ) );
for ( int i = 0; i < 5; i++ )
plate.Ball( "Rivet", new Vector3( -64f + i * 32f, side * (HatchRadius * 0.5f - 6f), 4.4f ), 1.8f, HubKit.Tin );
plate.Ring( "Pull Ring", new Vector3( 0f, side * 14f, 5f ), 8f, 1.6f, HubKit.Iron, 0f, 360f, 8, true );
return plate.Root;
}
/// <summary>One chain drawn tight across the hatch between two stakes, with a padlock on it.</summary>
GameObject BuildChain( HubBuilder b, float yaw, int index )
{
var chain = b.Group( "Chain", Vector3.Zero, Rotation.FromYaw( yaw ) );
float reach = HatchRadius + 34f;
const int links = 11;
for ( int i = 0; i < links; i++ )
{
float t = i / (float)(links - 1);
float x = -reach + t * reach * 2f;
float z = 17f + 7f * MathF.Sin( t * MathF.PI ) + index * 1.6f;
chain.Box( "Link", new Vector3( x, 0f, z ), new Vector3( reach * 2f / links + 3f, 6f, 3f ), HubKit.Iron, false, Rotation.FromRoll( i % 2 == 0 ? 0f : 90f ) );
}
chain.Box( "Stake", new Vector3( -reach - 4f, 0f, 8f ), new Vector3( 5f, 5f, 26f ), HubKit.Iron, false, Rotation.FromPitch( -14f ) );
chain.Box( "Stake", new Vector3( reach + 4f, 0f, 8f ), new Vector3( 5f, 5f, 26f ), HubKit.Iron, false, Rotation.FromPitch( 14f ) );
float lockAt = (index % 2 == 0 ? 1f : -1f) * (24f + index * 9f);
chain.Box( "Padlock", new Vector3( lockAt, 0f, 27f + index * 1.6f ), new Vector3( 9f, 5f, 9f ), new Color( 0.62f, 0.5f, 0.18f ) );
chain.Ring( "Shackle", new Vector3( lockAt, 0f, 31f + index * 1.6f ), 4f, 1.4f, HubKit.Tin, 0f, 180f, 4, false );
return chain.Root;
}
/// <summary>The same chain, off: a heap of links past the kerb and a padlock lying open.</summary>
GameObject BuildFallenChain( HubBuilder b, float yaw, HubRng rng )
{
var heap = b.Group( "Fallen Chain", Vector3.Zero, Rotation.FromYaw( yaw ) );
float start = HatchRadius + 44f;
for ( int i = 0; i < 5; i++ )
heap.Box( "Link", new Vector3( start + i * 9f + rng.R( -3f, 3f ), rng.R( -8f, 8f ), 2f + (i % 2) ), new Vector3( 14f, 6f, 3f ), HubKit.Iron, false,
new Angles( rng.R( -10f, 10f ), rng.R( 0f, 180f ), rng.R( -10f, 10f ) ).ToRotation() );
heap.Box( "Open Padlock", new Vector3( start + 20f, 14f, 2.5f ), new Vector3( 9f, 9f, 5f ), new Color( 0.62f, 0.5f, 0.18f ), false, Rotation.FromYaw( rng.R( 0f, 90f ) ) );
return heap.Root;
}
/// <summary>Sets how sealed the hatch is for a healed count: which chains are on, how bright the glow, open or shut.</summary>
void SetWayDown( int healed, bool animate )
{
if ( !_downRoot.IsValid() ) return;
int needs = Math.Max( SumpState.FinaleNeeds, 1 );
float loose = Math.Clamp( healed / (float)needs, 0f, 1f );
int left = healed >= needs ? 0 : Math.Max( (int)MathF.Ceiling( _chains.Count * (1f - loose) ), 1 );
for ( int i = 0; i < _chains.Count; i++ )
{
// The last chains to go are the first ones put on.
if ( _chains[i].IsValid() ) _chains[i].Enabled = i < left;
if ( _fallenChains[i].IsValid() ) _fallenChains[i].Enabled = i >= left;
}
_hatchTarget = healed >= needs ? 1f : 0f;
if ( !animate ) _hatchOpen = _hatchTarget;
if ( _downFlame is not null )
{
_downFlame.Level = 0.2f + 1.3f * loose;
if ( _downFlame.Light.IsValid() ) _downFlame.Light.Radius = 320f + 520f * loose;
}
if ( animate && _chainsLeft >= 0 && left < _chainsLeft )
{
Sump.Sfx( "chain_fall", _downAt, 1f );
if ( left == 0 ) Sump.Sfx( "waydown_open", _downAt, 1f );
if ( _downFlame is not null ) _downFlame.Boost = 1f;
}
_chainsLeft = left;
}
/// <summary>Slides the two plates apart (or together) toward where they should be.</summary>
void UpdateWayDown()
{
if ( !_downRoot.IsValid() ) return;
float step = Time.Delta / MathF.Max( HatchOpenSeconds, 0.1f );
_hatchOpen = _hatchOpen < _hatchTarget ? MathF.Min( _hatchOpen + step, _hatchTarget ) : MathF.Max( _hatchOpen - step, _hatchTarget );
if ( _hatchOpen == _hatchShown ) return;
_hatchShown = _hatchOpen;
// Ease in and out, slide outward over the kerb, and tip up a little as they go.
float e = _hatchOpen * _hatchOpen * (3f - 2f * _hatchOpen);
PlacePlate( _plateLeft, 1f, e );
PlacePlate( _plateRight, -1f, e );
}
static void PlacePlate( GameObject plate, float side, float open )
{
if ( !plate.IsValid() ) return;
plate.LocalPosition = new Vector3( 0f, side * (HatchRadius * 0.5f + open * (HatchRadius + 20f)), 12f + open * 22f );
plate.LocalRotation = Rotation.FromRoll( side * open * -16f );
}
string WayDownPrompt()
{
if ( SumpState.FinaleUnlocked ) return _hatchOpen > 0.9f ? "E lead them down" : null;
int more = SumpState.FinaleNeeds - SumpState.HealedCount;
return SumpState.HealedCount == 0
? "Chained shut. Something under it is breathing. Through the keyhole."
: $"Still chained. Help {more} more and it opens.";
}
}