diff --git a/client/Chamber.cs b/client/Chamber.cs index cdfc18c..c75c826 100644 --- a/client/Chamber.cs +++ b/client/Chamber.cs @@ -25,8 +25,17 @@ public partial class Chamber : Node3D /// The glow last drawn, 0 (dark) to 1. public float Glow { get; private set; } + /// The office has no chamber: hidden, and the glow light it casts off. + public void Hide() + { + Visible = false; + Glow = 0f; + Look.Set("ps1_glow_energy", 0f); + } + public void Sync(BodyPainBoxSession session) { + Visible = true; double intensity = session.BoxIntensity; // 0 once the box is off var box = session.Chamber; Position = new Vector3(box.CenterX, box.CenterY, box.CenterZ); diff --git a/client/Hud.cs b/client/Hud.cs index c29e2f3..11583d6 100644 --- a/client/Hud.cs +++ b/client/Hud.cs @@ -17,6 +17,9 @@ public partial class Hud : CanvasLayer public Hud(Main main) => _main = main; + private readonly VBoxContainer _chamberControls = new(); + + public OptionButton ScenarioPicker { get; } = new(); public SpinBox SeedBox { get; } = new() { MinValue = 0, MaxValue = 999999999, Step = 1, Value = 1 }; public Button ResetButton { get; } = new() { Text = "Reset" }; public HSlider IntensitySlider { get; } = new() { MinValue = 0, MaxValue = PainBox.MaxLevel, Step = 0.05, CustomMinimumSize = new Vector2(220, 24) }; @@ -53,13 +56,19 @@ public partial class Hud : CanvasLayer ResetButton.Pressed += () => _main.Reset((ulong)SeedBox.Value); controls.AddChild(ResetButton); - controls.AddChild(new Label { Text = "Intensity", Modulate = Ink }); + foreach (var scenario in System.Enum.GetValues()) ScenarioPicker.AddItem(scenario.ToString(), (int)scenario); + ScenarioPicker.Selected = (int)_main.Scenario; + ScenarioPicker.ItemSelected += i => _main.SetScenario((Scenario)ScenarioPicker.GetItemId((int)i)); + controls.AddChild(ScenarioPicker); + + controls.AddChild(_chamberControls); + _chamberControls.AddChild(new Label { Text = "Intensity", Modulate = Ink }); IntensitySlider.Value = _main.Intensity; IntensitySlider.ValueChanged += v => _main.SetIntensity(v); - controls.AddChild(IntensitySlider); + _chamberControls.AddChild(IntensitySlider); BoxOffButton.Pressed += () => _main.SwitchOff(); - controls.AddChild(BoxOffButton); + _chamberControls.AddChild(BoxOffButton); SlowToggle.Toggled += on => _main.SetSlowMotion(on); controls.AddChild(SlowToggle); } @@ -72,9 +81,17 @@ public partial class Hud : CanvasLayer return; } - var s = _main.Session; - var r = s.Result(); + _chamberControls.Visible = _main.Scenario == Scenario.Chamber; _text.Clear(); + if (_main.Scenario == Scenario.Office) + { + _text.Append($"office seed {_main.Seed} t {_main.Run.TickIndex * OfficeBody.TickSeconds:F2} s (tick {_main.Run.TickIndex})\n"); + _readout.Text = _text.ToString(); + return; + } + + var s = _main.PainBox; + var r = s.Result(); _text.Append($"seed {_main.Seed} t {s.TickIndex * BodyPainBox.TickSeconds:F2} s (tick {s.TickIndex})\n"); _text.Append($"box intensity {s.BoxIntensity:F2}{(s.BoxOn ? "" : " off")} (set {_main.Intensity:F2})\n"); _text.Append($"pain {s.Pain:F2} strain {s.Strain:F2} resolve {s.Resolve:F2}\n"); diff --git a/client/Main.cs b/client/Main.cs index d951126..cad3d64 100644 --- a/client/Main.cs +++ b/client/Main.cs @@ -6,10 +6,13 @@ using Godot; namespace BadDay.Client; +/// The runs the client hosts: the Synaptic Induction Chamber's pain box, or the office with its knife. +public enum Scenario { Chamber, Office } + /// /// The client's root. Godot's physics frame decides when a tick happens (physics interpolation on, Godot's own -/// physics engine Dummy); decides everything a tick does. This node only steps it, -/// draws it and forwards the player's commands to it. A session that cannot start (the native library is missing, say) +/// physics engine Dummy); the live (the pain box or the office, one at a time) decides everything a tick does. +/// This node only steps whichever is live, draws it and forwards the player's commands to it. A session that cannot start (the native library is missing, say) /// is reported once in and the log, and nothing ticks until a Reset succeeds. /// public partial class Main : Node3D @@ -22,10 +25,19 @@ public partial class Main : Node3D private Chamber _chamber = null!; private OrbitCamera _camera = null!; private Hud _hud = null!; - private BodyPainBoxSession? _session; + private IBodyRun? _run; private double _intensity = new PainBoxParameters().BoxIntensity; - public BodyPainBoxSession Session => _session ?? throw new InvalidOperationException($"Main has no session ({Failure ?? "before _Ready"})."); + public Scenario Scenario { get; private set; } = Scenario.Chamber; + + /// The live run, whichever scenario. + public IBodyRun Run => _run ?? throw new InvalidOperationException($"Main has no session ({Failure ?? "before _Ready"})."); + + /// The live run as the pain box; throws in the office. + public BodyPainBoxSession PainBox => Run as BodyPainBoxSession ?? throw new InvalidOperationException("The live run is not the pain box."); + + /// The live run as the office; throws in the chamber. + public OfficeBodySession Office => Run as OfficeBodySession ?? throw new InvalidOperationException("The live run is not the office."); public BodyRenderer Bodies => _bodies; public Chamber Chamber => _chamber; public Hud Hud => _hud; @@ -37,6 +49,9 @@ public partial class Main : Node3D /// The intensity the box is set to; the next Reset starts with it. public double Intensity => _intensity; + /// The parameters of an office run: every default except the 1/120 s tick the body needs. + public static StressParameters OfficeParameters { get; } = new() { TickSeconds = OfficeBody.TickSeconds }; + public bool SlowMotion { get; private set; } /// Every event the session has reported since the last Reset, oldest first. @@ -96,28 +111,37 @@ public partial class Main : Node3D { if (IsInstanceValid(GetWindow())) GetWindow().SizeChanged -= SetLines; Engine.PhysicsTicksPerSecond = StepsPerSecond(1.0); - _session?.Dispose(); - _session = null; + _run?.Dispose(); + _run = null; } public override void _PhysicsProcess(double delta) { - if (_session is null) return; - _session.Step(); // one step of 1/120 s per physics frame; never Godot's delta, whatever the frame rate + if (_run is null) return; + _run.Step(); // one step of 1/120 s per physics frame; never Godot's delta, whatever the frame rate Present(teleport: false); } - /// Starts a new run from at the current intensity, replacing the session. + /// Switches to and starts it at the current seed. + public void SetScenario(Scenario scenario) + { + Scenario = scenario; + Reset(Seed); + } + + /// Starts a new run of the current scenario from (the box at the current intensity), replacing the live run. public void Reset(ulong seed) { - _session?.Dispose(); - _session = null; + _run?.Dispose(); + _run = null; Failure = null; _events.Clear(); Seed = seed; try { - _session = new BodyPainBoxSession(ParametersFor(_intensity), seed); + _run = Scenario == Scenario.Office + ? new OfficeBodySession(OfficeParameters, seed) + : new BodyPainBoxSession(ParametersFor(_intensity), seed); } catch (Exception e) // the session's start can fail in native code (library missing, ABI mismatch): report once, tick nothing { @@ -131,11 +155,11 @@ public partial class Main : Node3D /// Sets the box intensity now and for the next Reset. public void SetIntensity(double level) { - _session?.SetIntensity(level); + (_run as BodyPainBoxSession)?.SetIntensity(level); _intensity = level; } - public void SwitchOff() => _session?.SwitchOff(); + public void SwitchOff() => (_run as BodyPainBoxSession)?.SwitchOff(); /// /// Slow motion is fewer session steps per real second: Godot runs its physics frames at a quarter of the rate and each @@ -150,9 +174,9 @@ public partial class Main : Node3D private void Present(bool teleport) { - var s = Session; + var s = Run; _bodies.Sync(s.View, teleport); - _chamber.Sync(s); + if (s is BodyPainBoxSession box) _chamber.Sync(box); else _chamber.Hide(); Span events = stackalloc BodyEvent[8]; int n; while ((n = s.DrainEvents(events)) > 0) diff --git a/client/tests/RenderCheck.cs b/client/tests/RenderCheck.cs index 66ea21d..f23f62c 100644 --- a/client/tests/RenderCheck.cs +++ b/client/tests/RenderCheck.cs @@ -30,7 +30,7 @@ public partial class RenderCheck : Node private string? _frames; private bool _done; - private BodyPainBoxSession Session => _main.Session; + private BodyPainBoxSession Session => _main.PainBox; private int Block => Look.BlockSize(Height); public override void _Ready() diff --git a/client/tests/Smoke.cs b/client/tests/Smoke.cs index b8cc7d4..d2c4a48 100644 --- a/client/tests/Smoke.cs +++ b/client/tests/Smoke.cs @@ -26,7 +26,8 @@ public partial class Smoke : Node private bool _done; private Hud Hud => _main.Hud; - private BodyPainBoxSession Session => _main.Session; + private BodyPainBoxSession Session => _main.PainBox; + private OfficeBodySession Office => _main.Office; public override void _Ready() => _ = RunAsync(); diff --git a/src/BadDay.Ymir/BodyPainBox.cs b/src/BadDay.Ymir/BodyPainBox.cs index 6fc9860..40f60d5 100644 --- a/src/BadDay.Ymir/BodyPainBox.cs +++ b/src/BadDay.Ymir/BodyPainBox.cs @@ -67,7 +67,7 @@ public static class BodyPainBox /// client drains them with , so a frame that spans several steps loses none. After /// every member throws . /// -public sealed class BodyPainBoxSession : IDisposable +public sealed class BodyPainBoxSession : IBodyRun { /// The box's half extent: the smallest 0.005 m step that keeps the hand in the box from the end of the ramp to the break on (on-body parameters, seed 1) at the hold pose below. Readings by half extent, ticks out from 2 s: 0.140 6, 0.145 3, 0.150 0, 0.155 0; excursions after the ramp: 0.150 1, 0.155 0. private const float BoxHalf = 0.155f; diff --git a/src/BadDay.Ymir/OfficeBodySession.cs b/src/BadDay.Ymir/OfficeBodySession.cs index 62f4d16..f4655ec 100644 --- a/src/BadDay.Ymir/OfficeBodySession.cs +++ b/src/BadDay.Ymir/OfficeBodySession.cs @@ -16,7 +16,7 @@ namespace BadDay.Ymir; /// no strength) and the run ends a release after the death. After only keeps /// the body moving, as the chamber does. /// -public sealed class OfficeBodySession : IDisposable +public sealed class OfficeBodySession : IBodyRun { private readonly SeatedBody _body; private readonly StressSession _session; diff --git a/src/BadDay.Ymir/SeatedBody.cs b/src/BadDay.Ymir/SeatedBody.cs index d42d049..f3bfd65 100644 --- a/src/BadDay.Ymir/SeatedBody.cs +++ b/src/BadDay.Ymir/SeatedBody.cs @@ -12,6 +12,20 @@ public readonly record struct PartHit(int Part, BodyPoint Point, BodyPoint Norma /// What the Actor reported on one tick of a body run; only ticks that reported something are queued. public readonly record struct BodyEvent(long Tick, TickEvents Events); +/// What a client needs of any body run to step it, draw it and drain its events: the pain box and the office. A client holds exactly one. +public interface IBodyRun : IDisposable +{ + /// The live lanes of the world, for rendering. + Box3DSoaView View { get; } + /// Ticks run so far; the tick the next runs. + long TickIndex { get; } + bool Finished { get; } + /// One step of 1/120 s. + void Step(); + /// Moves the queued events (oldest first) into ; returns how many. + int DrainEvents(Span buffer); +} + /// /// The one owner of a BDS body world: the sized by , the /// ragdoll puts in it, the body's blood (, the one writer of fluid particles and wet patches in the world), the sense-point, strength and target lanes, and the queue of