Part of the SumpHub partial class that builds the hub's radio room: a lean-to of scrap, a desk of dead radios, a working set with a dial and needle, a chair and a junk satellite dish. Also runs the listening interaction, holding the player at the set, stepping the tuning between unlocked frequencies, firing static and radio sounds, and redrawing the dial marks from SumpHub.Radio state.
using System;
using System.Collections.Generic;
// The radio room: a lean-to of scrap with a dish made of junk, a desk of dead radios and one set
// that still works. They still listen here for orders that never came. Every healed misfit opens
// one more frequency on the dial. What the set says comes from SumpRadio (SumpHub.Radio), so the
// lore module can supply it; this file only builds the room and works the dial.
public sealed partial class SumpHub
{
/// <summary>Volume of the static while the player listens.</summary>
[Property, Group( "Radio" ), Range( 0f, 1f )] public float StaticVolume { get; set; } = 0.45f;
/// <summary>Petals in the junk dish.</summary>
[Property, Group( "Radio" ), Range( 6, 14 )] public int DishPetals { get; set; } = 10;
// The working set's dial, in the room's own space: the face is DialWidth across at DialFace.
static readonly Vector3 DialFace = new( -19.5f, 0f, 47f );
const float DialWidth = 22f;
GameObject _radioRoot;
HubBuilder _radioBuilder;
GameObject _dialMarks;
GameObject _needle;
int _dialMarkCount = -1;
HubFlame _dialFlame;
float _radioYaw;
SoundHandle _static;
TimeSince _sinceStatic;
TimeSince _sinceHeard;
void ForgetRadio()
{
_radioRoot = null;
_radioBuilder = null;
_dialMarks = null;
_needle = null;
_dialMarkCount = -1;
_dialFlame = null;
Radio.Built = false;
}
void EnsureRadio( bool animate )
{
if ( _radioRoot.IsValid() ) return;
_radioYaw = FaceLanding( _radioAt );
var b = HubBuilder.At( _root, "Radio Room", _radioAt, _radioYaw );
_radioRoot = b.Root;
_radioBuilder = b;
var rng = new HubRng( MisfitGenerator.Mix( SumpState.Seed, 0x4AD10, 7 ) );
BuildLeanTo( b, rng );
BuildDesk( b, rng );
BuildWorkingSet( b );
BuildChair( b );
BuildDish( b, rng );
// Upturned crates out front, for whoever comes to listen.
for ( int i = 0; i < 3; i++ )
{
var at = new Vector3( 105f + (i % 2) * 22f, -50f + i * 50f, 0f );
b.Box( "Listening Crate", at + Vector3.Up * 7f, new Vector3( 22f, 22f, 20f ), HubKit.Shade( rng.Pick( HubKit.Junk ), 0.8f ), false, Rotation.FromYaw( rng.R( 0f, 90f ) ) );
}
Radio.Built = true;
Radio.Position = b.World( new Vector3( -28f, 0f, 42f ) );
Radio.ListeningSpot = HubKit.OnFloor( b.World( new Vector3( 115f, 0f, 0f ) ) );
// The dial is drawn before the room rises, so its marks rise with the rest.
Radio.Sync( _order );
RefreshDial();
AddSpot( b.Root, b.World( new Vector3( 8f, 0f, 0f ) ), 95f, () => Radio.PlayerListening ? null : "E sit and listen for orders", StartListening );
Finish( b, animate, RiseSeconds * 1.3f );
}
/// <summary>A back wall of scrap sheets, a sloping roof, two posts, and a floor of boards.</summary>
void BuildLeanTo( HubBuilder b, HubRng rng )
{
for ( int i = 0; i < 3; i++ )
{
var tint = HubKit.Shade( i == 1 ? HubKit.Tin : HubKit.Rust, rng.R( 0.7f, 1f ) );
b.Box( "Scrap Wall", new Vector3( -62f, -62f + i * 62f, 52f + rng.R( -4f, 6f ) ), new Vector3( 4f, 64f, 120f ), tint, true, Rotation.FromRoll( rng.R( -4f, 4f ) ) );
}
b.Box( "Side Sheet", new Vector3( -30f, -94f, 44f ), new Vector3( 62f, 3f, 96f ), HubKit.Shade( HubKit.Junk[4], 0.7f ), false, Rotation.FromYaw( 4f ) );
b.Box( "Post", new Vector3( 46f, -90f, 42f ), new Vector3( 5f, 5f, 100f ), HubKit.DarkWood );
b.Box( "Post", new Vector3( 46f, 90f, 42f ), new Vector3( 5f, 5f, 100f ), HubKit.DarkWood );
var roof = b.Group( "Roof", new Vector3( -8f, 0f, 104f ), Rotation.FromPitch( 11f ) );
for ( int i = 0; i < 3; i++ )
roof.Box( "Roof Sheet", new Vector3( rng.R( -4f, 4f ), -64f + i * 64f, i * 1.5f ), new Vector3( 124f, 68f, 3f ), HubKit.Shade( i % 2 == 0 ? HubKit.Rust : HubKit.Tin, rng.R( 0.6f, 0.9f ) ) );
roof.Box( "Roof Weight", new Vector3( 20f, 30f, 6f ), new Vector3( 18f, 24f, 9f ), HubKit.Rock );
for ( int i = 0; i < 5; i++ )
b.Box( "Floor Board", new Vector3( -6f, -72f + i * 36f, 0.5f ), new Vector3( 108f, 33f, 2f ), HubKit.Shade( HubKit.Wood, rng.R( 0.6f, 0.9f ) ), false, Rotation.FromYaw( rng.R( -2f, 2f ) ) );
// How long they have been waiting, scratched into the wall in fives.
for ( int group = 0; group < 3; group++ )
{
float y = 36f + group * 14f;
for ( int i = 0; i < 4; i++ )
b.Box( "Tally", new Vector3( -59.6f, y + i * 2.4f, 78f ), new Vector3( 0.6f, 0.8f, 8f ), HubKit.Paper );
b.Box( "Tally", new Vector3( -59.5f, y + 3.6f, 78f ), new Vector3( 0.6f, 11f, 0.8f ), HubKit.Paper, false, Rotation.FromRoll( 30f ) );
}
// A bare bulb under the roof.
b.Rod( "Bulb Flex", new Vector3( 10f, 0f, 100f ), new Vector3( 10f, 0f, 84f ), 0.8f, HubKit.Ink );
b.Ball( "Bare Bulb", new Vector3( 10f, 0f, 81f ), 3.5f, HubKit.Bulbs[0] );
AddFlame( b.Light( "Bulb Light", new Vector3( 10f, 0f, 76f ), HubKit.Bulbs[0], 300f ), HubKit.Shade( HubKit.Bulbs[0], 0.55f ), 0.1f, 2f );
}
/// <summary>A desk on two crates, covered in radios that will never work again.</summary>
void BuildDesk( HubBuilder b, HubRng rng )
{
b.Box( "Desk Top", new Vector3( -30f, 0f, 30f ), new Vector3( 34f, 128f, 4f ), HubKit.Wood, true );
b.Box( "Desk Crate", new Vector3( -30f, -50f, 12f ), new Vector3( 28f, 20f, 32f ), HubKit.Shade( HubKit.Junk[0], 0.8f ), true );
b.Box( "Desk Crate", new Vector3( -30f, 50f, 12f ), new Vector3( 28f, 20f, 32f ), HubKit.Shade( HubKit.Junk[4], 0.8f ), true );
foreach ( float y in new[] { -50f, -27f, 27f, 50f } )
{
var size = new Vector3( rng.R( 12f, 18f ), rng.R( 15f, 20f ), rng.R( 10f, 18f ) );
var at = new Vector3( -30f + rng.R( -3f, 3f ), y, 32f + size.z * 0.5f );
var tint = HubKit.Shade( rng.Pick( HubKit.Junk ), rng.R( 0.45f, 0.7f ) );
var radio = b.Group( "Dead Radio", at, Rotation.FromYaw( rng.R( -16f, 16f ) ) );
radio.Box( "Case", Vector3.Zero, size, tint );
radio.Box( "Dead Dial", new Vector3( size.x * 0.5f + 0.3f, 0f, size.z * 0.15f ), new Vector3( 0.6f, size.y * 0.7f, size.z * 0.3f ), HubKit.Ink );
radio.Disc( "Knob", new Vector3( size.x * 0.5f + 0.6f, size.y * 0.25f, -size.z * 0.25f ), 1.6f, 0.8f, HubKit.Tin );
if ( rng.Chance( 0.7f ) )
radio.Rod( "Bent Aerial", new Vector3( 0f, size.y * 0.3f, size.z * 0.5f ), new Vector3( rng.R( -8f, 8f ), size.y * 0.3f + rng.R( -10f, 10f ), size.z * 0.5f + rng.R( 12f, 26f ) ), 0.9f, HubKit.Tin );
}
// Somebody's notes: the same call sign, written out again and again.
b.Box( "Notes", new Vector3( -18f, -12f, 32.4f ), new Vector3( 9f, 12f, 0.6f ), HubKit.Paper, false, Rotation.FromYaw( 14f ) );
b.Box( "Notes", new Vector3( -19f, -14f, 32.8f ), new Vector3( 9f, 12f, 0.6f ), HubKit.Shade( HubKit.Paper, 0.9f ), false, Rotation.FromYaw( -8f ) );
b.Rod( "Pencil", new Vector3( -17f, -6f, 33.4f ), new Vector3( -21f, -1f, 33.4f ), 0.9f, HubKit.Bulbs[0] );
}
/// <summary>The one set that works: a warm dial, a needle, a pair of headphones.</summary>
void BuildWorkingSet( HubBuilder b )
{
var body = new Color( 0.42f, 0.26f, 0.14f );
b.Box( "Working Set", new Vector3( -28f, 0f, 43f ), new Vector3( 16f, 30f, 22f ), body );
b.Box( "Dial Face", DialFace, new Vector3( 0.8f, DialWidth + 3f, 8f ), new Color( 1f, 0.85f, 0.5f ) );
b.Box( "Speaker", new Vector3( -19.6f, 0f, 37f ), new Vector3( 0.8f, 20f, 7f ), HubKit.Ink );
b.Disc( "Tuning Knob", new Vector3( -19.2f, 11f, 37f ), 2.6f, 1.2f, HubKit.Tin );
b.Rod( "Aerial", new Vector3( -32f, 12f, 54f ), new Vector3( -36f, 16f, 96f ), 1f, HubKit.Tin );
_needle = b.Box( "Needle", DialFace + new Vector3( 0.6f, 0f, 0f ), new Vector3( 0.6f, 0.9f, 9f ), new Color( 0.9f, 0.1f, 0.08f ) );
// Headphones, hung on the corner of the set.
b.Ring( "Headphones", new Vector3( -22f, -17f, 44f ), 7f, 1.4f, HubKit.Ink, 0f, 180f, 6 );
b.Ball( "Ear Cup", new Vector3( -22f, -24f, 43f ), new Vector3( 3f, 2f, 3.5f ), HubKit.Ink );
b.Ball( "Ear Cup", new Vector3( -22f, -10f, 43f ), new Vector3( 3f, 2f, 3.5f ), HubKit.Ink );
var light = b.Light( "Dial Light", new Vector3( -10f, 0f, 48f ), new Color( 1f, 0.7f, 0.3f ), 130f );
_dialFlame = AddFlame( light, new Color( 1f, 0.7f, 0.3f ), 0.35f, 11f );
}
void BuildChair( HubBuilder b )
{
var tint = HubKit.Shade( HubKit.Junk[5], 0.8f );
b.Box( "Chair Seat", new Vector3( 6f, 0f, 16f ), new Vector3( 18f, 18f, 3f ), tint );
b.Box( "Chair Back", new Vector3( 14f, 0f, 29f ), new Vector3( 3f, 18f, 22f ), tint, false, Rotation.FromPitch( 6f ) );
foreach ( var (x, y) in new[] { (-1f, -7f), (-1f, 7f), (13f, -7f), (13f, 7f) } )
b.Box( "Chair Leg", new Vector3( x, y, 7f ), new Vector3( 2f, 2f, 16f ), HubKit.Tin );
}
/// <summary>
/// The big dish: a mast in a block of junk, and at its top a bowl of mismatched panels (trays,
/// car doors, a satellite dish that was somebody's television) tilted at the roof, toward the sky.
/// </summary>
void BuildDish( HubBuilder b, HubRng rng )
{
var foot = new Vector3( -40f, 140f, 0f );
b.Box( "Mast Foot", foot + Vector3.Up * 8f, new Vector3( 40f, 40f, 30f ), HubKit.Rust, true, Rotation.FromYaw( 20f ) );
b.Box( "Mast", foot + Vector3.Up * 85f, new Vector3( 6f, 6f, 150f ), HubKit.Tin );
b.Rod( "Mast Stay", foot + new Vector3( 0f, 0f, 120f ), foot + new Vector3( 40f, 30f, 0f ), 1f, HubKit.Ink );
b.Rod( "Mast Stay", foot + new Vector3( 0f, 0f, 120f ), foot + new Vector3( -30f, 40f, 0f ), 1f, HubKit.Ink );
b.Rod( "Cable", foot + new Vector3( 0f, 0f, 60f ), new Vector3( -36f, 16f, 60f ), 1.2f, HubKit.Ink );
// The dish's own +X is where it points: up, and a little toward the landing.
var dish = b.Group( "Dish", foot + Vector3.Up * 166f, Rotation.FromYaw( -25f ) * Rotation.FromPitch( -58f ) );
dish.Disc( "Dish Hub", Vector3.Zero, 12f, 3f, HubKit.Tin );
int petals = Math.Clamp( DishPetals, 6, 14 );
for ( int i = 0; i < petals; i++ )
{
var round = Rotation.FromRoll( i * 360f / petals );
var tint = HubKit.Shade( rng.Pick( HubKit.Junk ), rng.R( 0.7f, 1.1f ) );
float length = rng.R( 34f, 44f );
dish.Box( "Dish Panel", round * new Vector3( 9f, 0f, 9f + length * 0.5f ), new Vector3( 2f, 2f * (9f + length * 0.6f) * MathF.Tan( MathF.PI / petals ) + 2f, length ),
tint, false, round * Rotation.FromPitch( 24f ) );
}
var focus = new Vector3( 42f, 0f, 0f );
for ( int i = 0; i < 3; i++ )
dish.Rod( "Feed Arm", Rotation.FromRoll( i * 120f + 30f ) * new Vector3( 14f, 0f, 44f ), focus, 1.2f, HubKit.Tin );
dish.Box( "Feed Horn", focus, new Vector3( 8f, 6f, 6f ), HubKit.Junk[5] );
}
// ---- The dial -----------------------------------------------------------------------------------
/// <summary>Where a frequency sits across the dial face, from -half to +half its width.</summary>
float DialOffset( int position ) => ((Radio.Tenths( position ) - 881) / 198f - 0.5f) * DialWidth;
/// <summary>Redraws the marks on the dial (one per open frequency, in its misfit's colour) and sets the needle.</summary>
void RefreshDial()
{
if ( _radioBuilder is null || !_radioRoot.IsValid() ) return;
MoveNeedle();
if ( _dialMarks.IsValid() && _dialMarkCount == Radio.Frequencies.Count ) return;
_dialMarkCount = Radio.Frequencies.Count;
if ( _dialMarks.IsValid() ) _dialMarks.Destroy();
var marks = _radioBuilder.Group( "Dial Marks", DialFace );
_dialMarks = marks.Root;
for ( int i = 0; i < Radio.Frequencies.Count; i++ )
{
var profile = Radio.MisfitAt( i );
if ( profile is null ) continue;
marks.Box( "Frequency Mark", new Vector3( 0.5f, DialOffset( i ), -2.6f ), new Vector3( 0.6f, 1.4f, 2.6f ), HubKit.Shade( profile.Primary, 0.8f ) );
marks.Ball( "Frequency Dot", new Vector3( 0.6f, DialOffset( i ), 5.6f ), 0.9f, profile.Secondary );
}
}
void MoveNeedle()
{
if ( !_needle.IsValid() ) return;
float offset = Radio.Tuned >= 0 ? DialOffset( Radio.Tuned ) : -DialWidth * 0.5f;
_needle.LocalPosition = DialFace + new Vector3( 0.6f, offset, 0f );
}
void OnTuned( int position )
{
MoveNeedle();
if ( _dialFlame is not null ) _dialFlame.Boost = 0.6f;
if ( !Radio.Built ) return;
Sump.Sfx( "radio_tune", Radio.Position, 0.6f );
if ( Radio.PlayerListening ) SayHeard();
}
// ---- Listening ----------------------------------------------------------------------------------
void StartListening()
{
var player = Sump.Player;
if ( Radio.PlayerListening || !Radio.Built || !player.IsValid() ) return;
Radio.PlayerListening = true;
player.InputLocked = true;
Sump.Sfx( "radio_click", Radio.Position, 0.7f );
_sinceStatic = 100f;
if ( Radio.Tuned < 0 ) Radio.Tune( 0 );
SayHeard();
}
void StopListening()
{
if ( !Radio.PlayerListening ) return;
Radio.PlayerListening = false;
if ( Sump.Player.IsValid() ) Sump.Player.InputLocked = false;
if ( _static.IsValid() ) _static.Stop( 0.4f );
_static = null;
if ( Radio.Built ) Sump.Sfx( "radio_click", Radio.Position, 0.5f );
DropPrompt();
}
void SayHeard()
{
_sinceHeard = 0;
Sump.Say( "RADIO", Radio.Heard(), MathF.Max( RadioRepeat - 1f, 3f ) );
if ( _dialFlame is not null ) _dialFlame.Boost = 0.4f;
}
/// <summary>
/// While the player sits at the set: holds them there, turns the dial on Left and Right, repeats
/// what is heard, and lets them up on Use. Returns true while listening (it owns the prompt then).
/// </summary>
bool UpdateListening()
{
if ( !Radio.PlayerListening ) return false;
var player = Sump.Player;
if ( Sump.Mode != SumpMode.Hub || !player.IsValid() || !_radioRoot.IsValid() )
{
StopListening();
return false;
}
player.InputLocked = true;
string label = Radio.Tuned >= 0 ? Radio.Label( Radio.Tuned ) : "static";
ShowPrompt( Radio.Frequencies.Count > 1
? $"A / D turn the dial ({label}, {Radio.Tuned + 1} of {Radio.Frequencies.Count}) E stand up"
: $"Listening on {label} E stand up" );
if ( Input.Pressed( "Left" ) ) Radio.Step( -1 );
else if ( Input.Pressed( "Right" ) ) Radio.Step( 1 );
if ( _sinceHeard > RadioRepeat ) SayHeard();
// The static is one long loop; if it has run out (or was never there), try again now and then.
if ( _sinceStatic > 6f && (!_static.IsValid() || _static.Finished) )
{
_sinceStatic = 0;
_static = Sump.Sfx( "radio_static_loop", Radio.Position, StaticVolume );
}
if ( _sinceUse > 0.4f && Input.Pressed( "Use" ) )
{
_sinceUse = 0;
StopListening();
}
return true;
}
}