Helper kit for the finale sequence. FinalePulser holds per-object pulse state; FinaleSpace builds a layer's geometry (boxes, balls, point lights, billboard text, ambient light and fog) under a non-saved root and animates swelling and beating each frame; FinaleLayer is the abstract base each descent layer implements to build, update, report floor height and contain the player.
using System;
using System.Collections.Generic;
/// <summary>Something that swells and warms in waves: a rib of the throat, a lobe of the heart.</summary>
public sealed class FinalePulser
{
public GameObject Go;
public ModelRenderer Renderer;
public Vector3 Scale;
public Color Tint, Hot;
/// <summary>Where it sits in the travelling wave (radians); neighbours a little apart make the wave move.</summary>
public float Phase;
/// <summary>How much bigger it gets at the top of the wave (0.1 = a tenth).</summary>
public float Swell;
/// <summary>0 follows the travelling wave, 1 follows the heartbeat.</summary>
public float Beat;
/// <summary>Set to 1 to make it flare once; it dies away by itself.</summary>
public float Flash;
}
/// <summary>
/// One layer's worth of built things: a root object, the boxes, balls, lights and words under it,
/// its air (ambient light and fog), and everything in it that pulses. Each layer of the descent
/// has one, and destroying the root takes the whole layer away.
/// All positions are world positions.
/// </summary>
public sealed class FinaleSpace
{
public GameObject Root { get; }
public Vector3 Origin { get; }
public Random Rng { get; }
public AmbientLight Ambient { get; private set; }
/// <summary>How fast the travelling wave runs, in radians per second.</summary>
public float WaveSpeed { get; set; } = 1.6f;
sealed class Lamp
{
public PointLight Light;
public Color Color;
public float Beat;
}
readonly List<FinalePulser> _pulsers = new();
readonly List<Lamp> _lamps = new();
readonly Model _box = Model.Load( "models/dev/box.vmdl" ); // a 50 unit cube
readonly Model _sphere = Model.Load( "models/dev/sphere.vmdl" ); // radius 32
public FinaleSpace( GameObject parent, string name, Vector3 origin, int seed )
{
Origin = origin;
Rng = new Random( seed );
Root = new GameObject( parent, true, name );
Root.Flags |= GameObjectFlags.NotSaved;
Root.WorldPosition = origin;
}
public bool IsValid => Root.IsValid();
public void Destroy()
{
_pulsers.Clear();
_lamps.Clear();
if ( Root.IsValid() ) Root.Destroy();
}
public float R( float min, float max ) => min + (float)Rng.NextDouble() * (max - min);
// ---- Building blocks ------------------------------------------------------------------------
public GameObject Group( string name, Vector3 at, GameObject parent = null )
{
var go = new GameObject( parent ?? Root, true, name );
go.Flags |= GameObjectFlags.NotSaved;
go.WorldPosition = at;
return go;
}
/// <summary>A tinted box by its middle and size, turned if a rotation is given. Solid unless said otherwise.</summary>
public GameObject Box( string name, Vector3 centre, Vector3 size, Color tint, bool solid = true, Rotation? turn = null, GameObject parent = null )
{
var go = Group( name, centre, parent );
if ( turn is { } rotation ) go.WorldRotation = rotation;
go.WorldScale = size / 50f;
var renderer = go.Components.Create<ModelRenderer>();
renderer.Model = _box;
renderer.Tint = tint.WithAlpha( 1f );
renderer.RenderType = ModelRenderer.ShadowRenderType.Off;
if ( solid )
{
var collider = go.Components.Create<BoxCollider>();
collider.Scale = new Vector3( 50f, 50f, 50f );
collider.Static = true;
}
return go;
}
/// <summary>A tinted ball (or egg) by its middle and radii. Never solid.</summary>
public GameObject Ball( string name, Vector3 centre, Vector3 radii, Color tint, GameObject parent = null )
{
var go = Group( name, centre, parent );
go.WorldScale = radii / 32f;
var renderer = go.Components.Create<ModelRenderer>();
renderer.Model = _sphere;
renderer.Tint = tint.WithAlpha( 1f );
renderer.RenderType = ModelRenderer.ShadowRenderType.Off;
return go;
}
public GameObject Ball( string name, Vector3 centre, float radius, Color tint, GameObject parent = null ) =>
Ball( name, centre, new Vector3( radius ), tint, parent );
/// <summary>A point light with no shadows. Beat above 0 makes it brighten with the heartbeat.</summary>
public PointLight Light( string name, Vector3 at, Color color, float radius, float beat = 0f, GameObject parent = null )
{
var go = Group( name, at, parent );
var light = go.Components.Create<PointLight>();
light.LightColor = color.WithAlpha( 1f );
light.Radius = radius;
light.Shadows = false;
if ( beat > 0f ) _lamps.Add( new Lamp { Light = light, Color = color, Beat = beat } );
return light;
}
/// <summary>Changes the colour a beating light pulses around.</summary>
public void Recolour( PointLight light, Color color )
{
foreach ( var lamp in _lamps )
if ( lamp.Light == light ) { lamp.Color = color; return; }
if ( light.IsValid() ) light.LightColor = color.WithAlpha( 1f );
}
/// <summary>Words hanging in the air, always turned to the camera. Height is the size of a letter, in inches.</summary>
public TextRenderer Words( string name, Vector3 at, string text, float height, Color color, GameObject parent = null )
{
var go = Group( name, at, parent );
var words = go.Components.Create<TextRenderer>();
words.Text = text;
words.FontSize = 64f;
words.Scale = height / 64f;
words.Color = color;
words.Billboard = TextRenderer.BillboardMode.Always;
return words;
}
/// <summary>The air of the layer: ambient light and distance fog. Call again to change it.</summary>
public void Air( Color ambient, Color fog, float fogStart, float fogEnd )
{
// The cavern and the flashbacks use the same fog (SumpFog); keep them alike.
if ( !Ambient.IsValid() || !_fog.IsValid() )
{
var air = Group( "Air", Origin );
Ambient = air.Components.Create<AmbientLight>();
_fog = SumpFog.Create( air, fog, fogStart, fogEnd );
}
Ambient.Color = ambient * 2.6f; // the layers were authored too dark to see the procession
SumpFog.Set( _fog, fog, fogStart, fogEnd );
}
Fog _fog;
// ---- Things that pulse ------------------------------------------------------------------------
/// <summary>Makes a built thing swell and warm in waves. See <see cref="FinalePulser"/> for the numbers.</summary>
public FinalePulser Pulse( GameObject go, Color hot, float phase, float swell, float beat = 0f )
{
var renderer = go.Components.Get<ModelRenderer>();
var pulser = new FinalePulser
{
Go = go,
Renderer = renderer,
Scale = go.LocalScale,
Tint = renderer.IsValid() ? renderer.Tint : Color.White,
Hot = hot,
Phase = phase,
Swell = swell,
Beat = beat,
};
_pulsers.Add( pulser );
return pulser;
}
/// <summary>The pulser nearest a point, to flare when something is absorbed there.</summary>
public FinalePulser NearestPulser( Vector3 at )
{
FinalePulser best = null;
float nearest = float.MaxValue;
foreach ( var pulser in _pulsers )
{
if ( !pulser.Go.IsValid() ) continue;
float d = (pulser.Go.WorldPosition - at).LengthSquared;
if ( d < nearest ) { nearest = d; best = pulser; }
}
return best;
}
/// <summary>Moves every pulse and beating light on. Called once a frame with the heartbeat (0 to 1).</summary>
public void Animate( float time, float heartbeat, float dt )
{
foreach ( var pulser in _pulsers )
{
if ( !pulser.Go.IsValid() ) continue;
float wave = 0.5f + 0.5f * MathF.Sin( time * WaveSpeed - pulser.Phase );
float level = wave + (heartbeat - wave) * pulser.Beat;
if ( pulser.Flash > 0f )
{
level = MathF.Max( level, pulser.Flash );
pulser.Flash = MathF.Max( pulser.Flash - dt * 1.2f, 0f );
}
pulser.Go.LocalScale = pulser.Scale * (1f + pulser.Swell * level);
if ( pulser.Renderer.IsValid() ) pulser.Renderer.Tint = Color.Lerp( pulser.Tint, pulser.Hot, Math.Clamp( level, 0f, 1f ) ).WithAlpha( 1f );
}
foreach ( var lamp in _lamps )
{
if ( !lamp.Light.IsValid() ) continue;
float level = 1f - lamp.Beat * 0.5f + lamp.Beat * heartbeat;
lamp.Light.LightColor = (lamp.Color * level).WithAlpha( 1f );
}
}
}
/// <summary>
/// One layer of the descent: a place, a way of playing it, and the few questions the finale asks
/// of every layer (where the floor is, where the edges are, whether it has been got through).
/// </summary>
public abstract class FinaleLayer
{
public SumpFinale Finale { get; internal set; }
public FinaleSpace Space { get; internal set; }
public abstract string Title { get; }
public abstract string CardLine { get; }
public abstract string Objective { get; }
/// <summary>Odd things the procession says here.</summary>
public abstract IReadOnlyList<string> Chatter { get; }
/// <summary>Where the player arrives (feet) and which way they face.</summary>
public Vector3 Start { get; protected set; }
public float StartYaw { get; protected set; }
/// <summary>Where the heartbeat seems to come from.</summary>
public Vector3 PulseFrom { get; protected set; }
/// <summary>Set by the layer when the player has got through it.</summary>
public bool Done { get; protected set; }
/// <summary>
/// How far back the player's camera sits here, as a multiple of its usual distance. Below 1
/// for somewhere close (a tunnel), above 1 for somewhere vast. Put back when the layer ends.
/// </summary>
public virtual float CameraZoom => 1f;
protected ReptileController Player => Finale.Player;
protected FinaleProcession Procession => Finale.Procession;
/// <summary>Builds the place round <see cref="FinaleSpace.Origin"/>. The screen is black.</summary>
public abstract void Build();
/// <summary>The card has gone and the player has the controls.</summary>
public virtual void Begin() { }
/// <summary>Once a frame while the layer is being played.</summary>
public abstract void Update( float dt );
/// <summary>Height of the solid ground under a point.</summary>
public abstract float Floor( Vector3 point );
/// <summary>The nearest point that is inside the place (the point itself if it already is).</summary>
public abstract Vector3 Contain( Vector3 point );
/// <summary>Where the player goes back to when they are put out (feet and yaw). The start, unless the layer says otherwise.</summary>
public virtual (Vector3 Feet, float Yaw) Checkpoint => (Start, StartYaw);
/// <summary>The player has just been put back at the checkpoint.</summary>
public virtual void SentBack() { }
/// <summary>The layer is about to be destroyed (finished, skipped or left).</summary>
public virtual void End() { }
protected static float Lerp( float a, float b, float t ) => a + (b - a) * t;
}