Sump/Flashbacks/BodyOverride.cs

Defines BodyHandling presets for each BodyPlan (speed, jump, gravity, grip, visibility, camera, hint text) and the static BodyOverride helper that swaps the player's lizard for a MisfitBody during flashbacks. Apply retunes the ReptileController, records a Snapshot, disables the attack and any IGraft components; Remove restores every value and destroys the stand-in.

using System;
using System.Collections.Generic;

/// <summary>
/// How one body plan handles when the player wears it in a flashback. Plain numbers, written
/// straight onto the lizard's controller for as long as the memory lasts.
/// </summary>
public sealed class BodyHandling
{
	public float Speed = 300f, Sprint = 620f;
	public float Acceleration = 24f, Braking = 30f, Turn = 18f;
	public float Jump = 480f, Gravity = 1350f;
	/// <summary>Can it hold on to walls and ceilings.</summary>
	public bool Cling = true;
	/// <summary>Radius of the collision sphere: small ones get under things.</summary>
	public float Radius = 9f;
	public float CameraDistance = 210f, CameraHeight = 46f;
	/// <summary>Size of the stand-in body against the misfit's hub size.</summary>
	public float Scale = 0.5f;
	/// <summary>How easily humans see it: multiplies how far their eyes reach.</summary>
	public float Visibility = 1f;
	/// <summary>The same, while it is not moving. Near zero: standing still, it is part of the scenery.</summary>
	public float StillVisibility = 1f;
	/// <summary>How hard a grip on it is to shake off, against the lizard's (below 1 is slippery).</summary>
	public float Grip = 1f;
	/// <summary>Time limits are multiplied by this, so the slow bodies are not asked the impossible.</summary>
	public float Patience = 1f;
	/// <summary>One line told to the player when the memory starts: what this body is like to be in.</summary>
	public string Hint = "";
}

/// <summary>
/// The body the player relives a memory in: the lizard is hidden, the misfit's own body stands in
/// for it, and the controller is retuned to suit. Everything is put back by <see cref="Remove"/>.
/// </summary>
public static class BodyOverride
{
	public const string StandInTag = "sump_standin";

	/// <summary>The one table of how each body plan handles.</summary>
	public static readonly Dictionary<BodyPlan, BodyHandling> Table = new()
	{
		[BodyPlan.GasSac] = new BodyHandling
		{
			Speed = 150f, Sprint = 230f, Acceleration = 3f, Braking = 2.5f, Turn = 6f, Jump = 260f, Gravity = 240f,
			Cling = false, Radius = 13f, CameraDistance = 240f, CameraHeight = 60f, Scale = 0.5f,
			Visibility = 1.2f, Grip = 1.1f, Patience = 1.5f,
			Hint = "You drift. Jumps are long and soft, stopping takes a while, and nothing holds you to a wall. You are a worried balloon.",
		},
		[BodyPlan.CrystalLattice] = new BodyHandling
		{
			Speed = 140f, Sprint = 215f, Acceleration = 8f, Braking = 30f, Turn = 7f, Jump = 260f, Gravity = 1900f,
			Cling = false, Radius = 13f, CameraDistance = 230f, CameraHeight = 54f, Scale = 0.5f,
			Visibility = 1.3f, Grip = 0.7f, Patience = 1.5f,
			Hint = "You are heavy. Slow, barely able to jump, and you catch the light. Hands slide off your edges. You are a chandelier with opinions.",
		},
		[BodyPlan.Swarm] = new BodyHandling
		{
			Speed = 330f, Sprint = 560f, Acceleration = 20f, Braking = 14f, Turn = 20f, Jump = 430f, Gravity = 800f,
			Cling = false, Radius = 8f, CameraDistance = 200f, CameraHeight = 44f, Scale = 0.45f,
			Visibility = 0.8f, Grip = 0.5f,
			Hint = "You are many and light. Fast, hard to keep hold of, and you cannot climb. Most of you agree on the direction.",
		},
		[BodyPlan.Jelly] = new BodyHandling
		{
			Speed = 130f, Sprint = 200f, Acceleration = 10f, Braking = 12f, Turn = 9f, Jump = 240f, Gravity = 1100f,
			Cling = true, Radius = 6f, CameraDistance = 190f, CameraHeight = 36f, Scale = 0.45f,
			Visibility = 0.9f, StillVisibility = 0.5f, Grip = 0.55f, Patience = 1.5f,
			Hint = "You are slow and soft. You stick to anything, squeeze under things, and slip out of hands. You also make a noise. Sorry.",
		},
		[BodyPlan.Insectoid] = new BodyHandling
		{
			Speed = 320f, Sprint = 600f, Acceleration = 26f, Braking = 32f, Turn = 22f, Jump = 460f, Gravity = 1300f,
			Cling = true, Radius = 9f, CameraDistance = 200f, CameraHeight = 42f, Scale = 0.45f,
			Grip = 0.9f,
			Hint = "You are quick, and every wall and ceiling is a floor to you.",
		},
		[BodyPlan.Fungus] = new BodyHandling
		{
			Speed = 110f, Sprint = 170f, Acceleration = 8f, Braking = 30f, Turn = 6f, Jump = 220f, Gravity = 1300f,
			Cling = false, Radius = 10f, CameraDistance = 210f, CameraHeight = 48f, Scale = 0.5f,
			Visibility = 0.9f, StillVisibility = 0.05f, Grip = 1.2f, Patience = 1.7f,
			Hint = "You are slow. But stand quite still and you are only a mushroom: nobody sees a mushroom.",
		},
		[BodyPlan.Machine] = new BodyHandling
		{
			Speed = 260f, Sprint = 700f, Acceleration = 2.2f, Braking = 2.5f, Turn = 4f, Jump = 200f, Gravity = 1500f,
			Cling = false, Radius = 12f, CameraDistance = 230f, CameraHeight = 50f, Scale = 0.5f,
			Visibility = 1.1f,
			Hint = "You roll. Very fast in a straight line, and you turn and stop like a shopping trolley.",
		},
	};

	public static BodyHandling For( BodyPlan plan ) => Table.TryGetValue( plan, out var handling ) ? handling : new BodyHandling();

	/// <summary>Everything on the player this module changes, as it was before.</summary>
	public sealed class Snapshot
	{
		public float Speed, Sprint, Acceleration, Braking, Turn, Jump, Gravity, Radius, SpeedScale, LeashRadius;
		public bool Cling, ClearView, PassCrowd, AttackEnabled;
		public string StepSound;
		public Func<Vector3, float> FloorHeight;
		// The camera is changed by ratio, not by value, so it combines with whoever else scales it (the phobias).
		public float CameraDistanceRatio = 1f, CameraHeightRatio = 1f;
		public LizardAttack Attack;
		public List<(IGraft Graft, bool Active)> Grafts = new();
	}

	/// <summary>Remembers the player as it is now. Call before anything is changed.</summary>
	public static Snapshot Take( ReptileController player )
	{
		var snapshot = new Snapshot
		{
			Speed = player.Speed, Sprint = player.SprintSpeed, Acceleration = player.Acceleration, Braking = player.Braking,
			Turn = player.TurnSpeed, Jump = player.JumpSpeed, Gravity = player.Gravity, Radius = player.Radius,
			SpeedScale = player.SpeedScale, LeashRadius = player.LeashRadius,
			Cling = player.ClingEnabled, ClearView = player.ClearView, PassCrowd = player.PassCrowd,
			StepSound = player.StepSound, FloorHeight = player.FloorHeight,
		};
		snapshot.Attack = player.Components.Get<LizardAttack>();
		snapshot.AttackEnabled = snapshot.Attack.IsValid() && snapshot.Attack.Enabled;
		return snapshot;
	}

	/// <summary>
	/// Hides the lizard, stands the misfit's body in for it and retunes the controller. Returns
	/// the stand-in. Call <see cref="Remove"/> with the same snapshot to undo all of it.
	/// </summary>
	public static MisfitBody Apply( ReptileController player, MisfitProfile profile, BodyHandling handling, Snapshot snapshot )
	{
		player.Speed = handling.Speed;
		player.SprintSpeed = handling.Sprint;
		player.Acceleration = handling.Acceleration;
		player.Braking = handling.Braking;
		player.TurnSpeed = handling.Turn;
		player.JumpSpeed = handling.Jump;
		player.Gravity = handling.Gravity;
		player.Radius = handling.Radius;
		player.ClingEnabled = handling.Cling;
		player.StepSound = "";   // no claws on these feet
		player.PassCrowd = false;

		snapshot.CameraDistanceRatio = handling.CameraDistance / MathF.Max( player.CameraDistance, 1f );
		snapshot.CameraHeightRatio = handling.CameraHeight / MathF.Max( player.CameraHeight, 1f );
		player.CameraDistance *= snapshot.CameraDistanceRatio;
		player.CameraHeight *= snapshot.CameraHeightRatio;

		// No biting in somebody else's mouth, and no grafts in somebody else's memory.
		if ( snapshot.Attack.IsValid() ) snapshot.Attack.Enabled = false;
		foreach ( var component in player.GameObject.Components.GetAll<Component>( FindMode.EverythingInSelfAndDescendants ) )
		{
			if ( component is not IGraft graft ) continue;
			snapshot.Grafts.Add( (graft, graft.Active) );
			graft.Active = false;
		}

		Sump.SetPlayerBodyVisible( false );

		var parent = player.Body.IsValid() ? player.Body : player.GameObject;
		var body = MisfitBody.Create( parent, profile, handling.Scale );
		body.GameObject.Tags.Add( StandInTag );
		// Whatever size the lizard's own body object is, the stand-in is its own size.
		body.GameObject.WorldScale = new Vector3( handling.Scale * MathF.Max( profile?.Size ?? 1f, 0.05f ) );
		body.GameObject.LocalPosition = Vector3.Zero;
		body.GameObject.LocalRotation = Rotation.Identity;
		return body;
	}

	/// <summary>Takes the stand-in away, shows the lizard again and puts every changed value back.</summary>
	public static void Remove( ReptileController player, MisfitBody standIn, Snapshot snapshot )
	{
		if ( standIn.IsValid() ) standIn.GameObject.Destroy();
		Sump.SetPlayerBodyVisible( true );
		if ( !player.IsValid() || snapshot is null ) return;

		player.Speed = snapshot.Speed;
		player.SprintSpeed = snapshot.Sprint;
		player.Acceleration = snapshot.Acceleration;
		player.Braking = snapshot.Braking;
		player.TurnSpeed = snapshot.Turn;
		player.JumpSpeed = snapshot.Jump;
		player.Gravity = snapshot.Gravity;
		player.Radius = snapshot.Radius;
		player.ClingEnabled = snapshot.Cling;
		player.StepSound = snapshot.StepSound;
		player.PassCrowd = snapshot.PassCrowd;
		player.FloorHeight = snapshot.FloorHeight;
		player.SpeedScale = snapshot.SpeedScale;
		player.LeashRadius = snapshot.LeashRadius;
		player.ClearView = snapshot.ClearView;
		player.Leash = null;
		player.Unpin();

		if ( snapshot.CameraDistanceRatio > 0.001f ) player.CameraDistance /= snapshot.CameraDistanceRatio;
		if ( snapshot.CameraHeightRatio > 0.001f ) player.CameraHeight /= snapshot.CameraHeightRatio;
		snapshot.CameraDistanceRatio = snapshot.CameraHeightRatio = 1f;

		if ( snapshot.Attack.IsValid() ) snapshot.Attack.Enabled = snapshot.AttackEnabled;
		foreach ( var (graft, active) in snapshot.Grafts )
			if ( graft is Component component && component.IsValid() ) graft.Active = active;
		snapshot.Grafts.Clear();

		// Back in the Sump the player always has the controls.
		player.SetSpeedFactor( FlashbackRunner.SpeedName, 1f );
		player.ForcedWish = null;
		player.InputLocked = false;
	}
}