Tuning Knobs
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
#if UNITY_EDITOR
|
||||
using ProjectM.Simulation;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ProjectM.Client
|
||||
{
|
||||
/// <summary>
|
||||
/// MC-0 — EDITOR-ONLY client owner of the local <see cref="TuningConfig"/> singleton (the value the PREDICTED
|
||||
/// DashSystem reads on this client). Each tick it (1) drains any authoritative <see cref="DebugTuningReport"/>
|
||||
/// into the <see cref="TuningReadout"/> static, then (2) pushes <see cref="TuningReadout.Current"/> into the
|
||||
/// client TuningConfig singleton. The overlay mutates the readout OPTIMISTICALLY via
|
||||
/// <see cref="TuningReadout.SetLocal"/> (so the tuner's own dash uses a nudged value instantly — instant, then
|
||||
/// EVENTUALLY CONSISTENT with the server within ~1 RTT once the SetTuning RPC lands and the next report
|
||||
/// reconciles). Runs EVERY tick (no RequireForUpdate) so an overlay-only change with no report still reaches the
|
||||
/// singleton. Plain client <see cref="SimulationSystemGroup"/> (mutated at most once/tick → the predicted
|
||||
/// re-sim reads a per-tick-constant value); non-Burst (touches a managed static). This DELIBERATELY extends the
|
||||
/// DevTelemetry pattern (which writes only a static) with a per-world singleton; the release path has no such
|
||||
/// system, so DashSystem's <c>TryGetSingleton ? : Defaults()</c> fallback is the permanent release behaviour.
|
||||
/// </summary>
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
|
||||
[UpdateInGroup(typeof(SimulationSystemGroup))]
|
||||
public partial struct DevTuningReceiveSystem : ISystem
|
||||
{
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
if (state.GetEntityQuery(ComponentType.ReadWrite<TuningConfig>()).IsEmpty)
|
||||
{
|
||||
var e = state.EntityManager.CreateEntity(typeof(TuningConfig));
|
||||
state.EntityManager.SetComponentData(e, TuningConfig.Defaults());
|
||||
}
|
||||
}
|
||||
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
// (1) Authoritative server snapshot wins — overwrite the readout (FULL state).
|
||||
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
||||
foreach (var (report, reqEntity) in
|
||||
SystemAPI.Query<RefRO<DebugTuningReport>>()
|
||||
.WithAll<ReceiveRpcCommandRequest>().WithEntityAccess())
|
||||
{
|
||||
TuningReadout.Current = TuningConfig.FromReport(report.ValueRO);
|
||||
TuningReadout.Initialized = true;
|
||||
ecb.DestroyEntity(reqEntity);
|
||||
}
|
||||
ecb.Playback(state.EntityManager);
|
||||
ecb.Dispose();
|
||||
|
||||
// (2) Push the readout (authoritative, or optimistic SetLocal, or Defaults()) into the client singleton.
|
||||
if (SystemAPI.TryGetSingletonEntity<TuningConfig>(out var cfgEntity))
|
||||
state.EntityManager.SetComponentData(cfgEntity, TuningReadout.Current);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MC-0 — static bridge from the ECS tuning receiver to the IMGUI <c>DebugOverlay</c> (so the overlay reads/sets
|
||||
/// a plain struct, never ECS state). <see cref="Current"/> seeds to <see cref="TuningConfig.Defaults"/> on
|
||||
/// play-enter (so it matches the server's seeded Defaults before the first report). <see cref="SetLocal"/> is the
|
||||
/// overlay's OPTIMISTIC apply — it runs the same <see cref="TuningConfig.Apply"/> clamp so an overlay nudge can
|
||||
/// never feed a 0/negative into the predicted DashSystem.
|
||||
/// </summary>
|
||||
public static class TuningReadout
|
||||
{
|
||||
public static TuningConfig Current;
|
||||
|
||||
/// <summary>True once an authoritative server report has been received (else <see cref="Current"/> is Defaults()).</summary>
|
||||
public static bool Initialized;
|
||||
|
||||
/// <summary>Optimistic local apply (overlay button) — clamped via <see cref="TuningConfig.Apply"/>.</summary>
|
||||
public static void SetLocal(byte knob, float value) => TuningConfig.Apply(ref Current, knob, value);
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
static void Reset()
|
||||
{
|
||||
Current = TuningConfig.Defaults();
|
||||
Initialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user