Base types for the flashback minigames: a checkpoint record, a rules object holding objective, win/loss delegates, clock and meter, and an abstract Flashback genre class with a static registry mapping FlashbackGenre to factory functions. Handles setup, RNG helpers and dialogue text filling.
using System;
using System.Collections.Generic;
/// <summary>A place in a memory the player is put back to when it goes wrong.</summary>
public sealed class FlashbackCheckpoint
{
public string Name;
/// <summary>Where the player stands (feet, world).</summary>
public Vector3 Feet;
public float Yaw;
/// <summary>How far through the memory's clock it was reached.</summary>
public float Elapsed;
/// <summary>Anything the flashback wants to remember about how far it had got.</summary>
public int Stage;
}
/// <summary>
/// The rules of one flashback, as the runner needs them: what to tell the player, how it is won
/// and lost, where it rewinds to, how long there is, and what to do every frame. A flashback
/// fills one of these in; <see cref="FlashbackRunner"/> does the rest.
/// </summary>
public sealed class FlashbackRules
{
/// <summary>The current goal. Keep it up to date; the runner adds the clock and the meter.</summary>
public string Objective = "";
/// <summary>True once the memory has been lived through to its end.</summary>
public Func<bool> Won;
/// <summary>A reason (shown to the player) once it has gone wrong, else null. The memory then rewinds.</summary>
public Func<string> Lost;
/// <summary>Called every frame while the memory is being played, with the frame's seconds.</summary>
public Action<float> Update;
/// <summary>Seconds the memory lasts, or 0 for no limit. Running out is a loss.</summary>
public float TimeLimit;
/// <summary>What the clock is counting down to ("THE PROCEDURE").</summary>
public string TimeLabel = "";
/// <summary>What is said when the clock runs out.</summary>
public string TimeUpLine = "Too late.";
/// <summary>Seconds of the memory's clock used so far.</summary>
public float Elapsed;
public float TimeLeft => TimeLimit > 0f ? MathF.Max( TimeLimit - Elapsed, 0f ) : -1f;
/// <summary>After a rewind there is always at least this long left on the clock.</summary>
public float RewindGrace = 45f;
/// <summary>Pinned by the crowd for this long without a break is a loss. 0: the flashback deals with it itself.</summary>
public float PinLoseSeconds = 5f;
public string PinLoseLine = "They have you.";
/// <summary>A meter for the HUD, 0 to 1, or below zero for none.</summary>
public float Meter = -1f;
public string MeterLabel = "";
/// <summary>Further than this from the arena's middle the player is put back (they fell out of the memory).</summary>
public float BoundsRadius = 4000f;
public readonly List<FlashbackCheckpoint> Checkpoints = new();
/// <summary>The last checkpoint reached, or null before the first.</summary>
public FlashbackCheckpoint Checkpoint => Checkpoints.Count > 0 ? Checkpoints[^1] : null;
/// <summary>Records a checkpoint (once per name). The memory rewinds to the latest one.</summary>
public bool Reach( string name, Vector3 feet, float yaw, int stage = 0 )
{
if ( Reached( name ) ) return false;
Checkpoints.Add( new FlashbackCheckpoint { Name = name, Feet = feet, Yaw = yaw, Elapsed = Elapsed, Stage = stage } );
return true;
}
public bool Reached( string name )
{
foreach ( var checkpoint in Checkpoints )
if ( checkpoint.Name == name ) return true;
return false;
}
}
/// <summary>
/// One genre of flashback: a misfit's first contact with humans as a small game of its own. A
/// genre builds its arena, says where the player starts, and fills in <see cref="Rules"/>.
/// Everything else (the fades, the body, the title card, rewinding, winning, going home) is the
/// runner's. A new genre is one new class and one line in <see cref="Genres"/>.
/// </summary>
public abstract class Flashback
{
/// <summary>Every genre there is, by the value rolled into a misfit's profile.</summary>
public static readonly Dictionary<FlashbackGenre, Func<Flashback>> Genres = new()
{
[FlashbackGenre.FarmHorror] = () => new FarmFlashback(),
[FlashbackGenre.LabEscape] = () => new LabFlashback(),
[FlashbackGenre.TalkShow] = () => new TalkShowFlashback(),
};
public static Flashback Create( FlashbackGenre genre ) => Genres.TryGetValue( genre, out var make ) ? make() : null;
/// <summary>The kind of game this is, for the HUD ("FARM HORROR").</summary>
public abstract string GenreTitle { get; }
public FlashbackArena Arena { get; private set; }
public MisfitProfile Profile => Arena.Profile;
public FlashbackRules Rules { get; } = new();
/// <summary>Where and when, for the title card ("Harlan County, Kansas. 1987").</summary>
public string Place { get; protected set; } = "";
/// <summary>Where the player is put when the memory starts (feet, world) and which way they face.</summary>
public Vector3 Start { get; protected set; }
public float StartYaw { get; protected set; }
protected Random Rng => Arena.Rng;
protected string Who => MisfitVoice.ShortName( Profile );
/// <summary>The thing the misfit clings to, in lower case mid-sentence ("the Walkman").</summary>
protected string Thing => string.IsNullOrEmpty( Profile.ClungObject ) ? "the thing" : "the " + Profile.ClungObject;
public void Setup( FlashbackArena arena )
{
Arena = arena;
Rules.Update = Tick;
}
/// <summary>Builds the arena under <see cref="FlashbackArena.Root"/> and sets <see cref="Start"/>, <see cref="Place"/> and the rules.</summary>
public abstract void Build();
/// <summary>The title card has gone and the player has the controls.</summary>
public virtual void Begin() { }
/// <summary>Every frame while the memory is played.</summary>
protected virtual void Tick( float dt ) { }
/// <summary>The memory went wrong and has been wound back to this checkpoint: put the humans back where they belong.</summary>
public virtual void Rewind( FlashbackCheckpoint to ) { }
/// <summary>The memory has been won and is frozen: a last human line, if the genre has one.</summary>
public virtual void Won() { }
/// <summary>A float from the memory's own seed.</summary>
protected float R( float min, float max ) => min + (float)Rng.NextDouble() * (max - min);
protected T Pick<T>( IReadOnlyList<T> from ) => from[Rng.Next( from.Count )];
/// <summary>Fills in the misfit's particulars in a line: {name}, {thing}, {fear}.</summary>
protected string Fill( string line ) => line
.Replace( "{name}", Who )
.Replace( "{thing}", Thing )
.Replace( "{object}", string.IsNullOrEmpty( Profile.ClungObject ) ? "thing" : Profile.ClungObject )
.Replace( "{fear}", string.IsNullOrEmpty( Profile.Fear ) ? "the dark" : Profile.Fear );
protected void Say( string speaker, string line, float seconds = 4.5f ) => Sump.Say( speaker, Fill( line ), seconds );
}