Scene bootstrap component for the Sump game. On start it loads or resets the Sump state, finds the ReptileController player, and creates the first wave of module components (cavern, spawner, phobia, sense). Later waves hook in through a partial SumpModules class.
using System;
using System.Linq;
/// <summary>
/// The one component placed in scenes/sump.scene. When the scene starts it loads the Sump's
/// state, finds the player, and switches on every Sump module by adding its component here (or
/// on the player). Modules never start each other; they are all started from this list.
/// </summary>
[Title( "Sump Bootstrap" ), Category( "Sump" ), Icon( "south" )]
public sealed class SumpBootstrap : Component
{
public static SumpBootstrap Current { get; private set; }
/// <summary>Start a fresh Sump every time the scene loads, instead of continuing the save.</summary>
[Property] public bool AlwaysNew { get; set; }
protected override void OnAwake()
{
if ( Scene.IsEditor ) return;
Current = this;
Sump.ResetScene();
if ( AlwaysNew ) SumpState.Reset(); else SumpState.EnsureStarted();
}
protected override void OnStart()
{
if ( Scene.IsEditor ) return;
Current = this;
Sump.Player = Scene.GetAllComponents<ReptileController>().FirstOrDefault();
if ( Scene.Camera.IsValid() ) Scene.Camera.ZFar = 60000f;
// Wave 1.
Components.GetOrCreate<SumpCavern>();
MisfitSpawner.SpotSource = () => SumpCavern.Ready ? SumpCavern.MisfitSpots : null;
Components.GetOrCreate<MisfitSpawner>();
if ( Sump.Player.IsValid() ) Sump.Player.Components.GetOrCreate<PlayerPhobia>();
// The sense pulse (Q) the Sump's controls card promises: the same faint glow as in the opening.
if ( Sump.Player.IsValid() ) Sump.Player.Components.GetOrCreate<LizardSense>();
SumpModules.StartLaterWaves( this );
}
protected override void OnDestroy()
{
if ( Current == this ) Current = null;
}
protected override void OnUpdate()
{
// Only in the hub does the cavern hold the lizard inside its rock.
if ( Sump.Player.IsValid() ) Sump.Player.StayInCaves = Sump.Mode == SumpMode.Hub || Sump.Mode == SumpMode.Arrival;
}
/// <summary>Moves the player at once.</summary>
public void Teleport( Vector3 feet, float yaw )
{
if ( Sump.Player.IsValid() ) Sump.Player.Teleport( feet, yaw );
}
}
/// <summary>Modules from waves 2 and 3 are switched on here as they are written.</summary>
public static partial class SumpModules
{
public static void StartLaterWaves( SumpBootstrap root )
{
StartWave2( root );
StartWave3( root );
}
static partial void StartWave2( SumpBootstrap root );
static partial void StartWave3( SumpBootstrap root );
}