为本地抖动情况制定修补计划
This commit is contained in:
@@ -196,6 +196,7 @@ public class MovementComponent : MonoBehaviour
|
||||
}
|
||||
|
||||
Simulate(_cachedMoveInput);
|
||||
_predictionBuffer.AccumulateLatest(Time.fixedDeltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -300,11 +301,11 @@ public class MovementComponent : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void ReplayPendingInputs(IReadOnlyList<MoveInput> replayInputs)
|
||||
private void ReplayPendingInputs(IReadOnlyList<PredictedMoveStep> replayInputs)
|
||||
{
|
||||
foreach (var replayInput in replayInputs)
|
||||
{
|
||||
ApplyTankMovement(replayInput.TurnInput, replayInput.ThrottleInput, _sendInterval);
|
||||
ApplyTankMovement(replayInput.Input.TurnInput, replayInput.Input.ThrottleInput, replayInput.SimulatedDurationSeconds);
|
||||
}
|
||||
|
||||
if (_isControlled)
|
||||
|
||||
@@ -5,13 +5,26 @@ using Network.Defines;
|
||||
|
||||
namespace Network.NetworkApplication
|
||||
{
|
||||
public readonly struct PredictedMoveStep
|
||||
{
|
||||
public PredictedMoveStep(MoveInput input, float simulatedDurationSeconds)
|
||||
{
|
||||
Input = input ?? throw new ArgumentNullException(nameof(input));
|
||||
SimulatedDurationSeconds = simulatedDurationSeconds < 0f ? 0f : simulatedDurationSeconds;
|
||||
}
|
||||
|
||||
public MoveInput Input { get; }
|
||||
|
||||
public float SimulatedDurationSeconds { get; }
|
||||
}
|
||||
|
||||
public sealed class ClientPredictionBuffer
|
||||
{
|
||||
private readonly List<MoveInput> pendingInputs = new();
|
||||
private readonly List<PredictedMoveStep> pendingInputs = new();
|
||||
|
||||
public long? LastAuthoritativeTick { get; private set; }
|
||||
|
||||
public IReadOnlyList<MoveInput> PendingInputs => pendingInputs;
|
||||
public IReadOnlyList<PredictedMoveStep> PendingInputs => pendingInputs;
|
||||
|
||||
public void Record(MoveInput input)
|
||||
{
|
||||
@@ -20,15 +33,26 @@ namespace Network.NetworkApplication
|
||||
throw new ArgumentNullException(nameof(input));
|
||||
}
|
||||
|
||||
if (pendingInputs.Count > 0 && pendingInputs[^1].Tick >= input.Tick)
|
||||
if (pendingInputs.Count > 0 && pendingInputs[^1].Input.Tick >= input.Tick)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pendingInputs.Add(input);
|
||||
pendingInputs.Add(new PredictedMoveStep(input, 0f));
|
||||
}
|
||||
|
||||
public bool TryApplyAuthoritativeState(PlayerState state, out IReadOnlyList<MoveInput> replayInputs)
|
||||
public void AccumulateLatest(float simulatedDurationSeconds)
|
||||
{
|
||||
if (pendingInputs.Count == 0 || simulatedDurationSeconds <= 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var latest = pendingInputs[^1];
|
||||
pendingInputs[^1] = new PredictedMoveStep(latest.Input, latest.SimulatedDurationSeconds + simulatedDurationSeconds);
|
||||
}
|
||||
|
||||
public bool TryApplyAuthoritativeState(PlayerState state, out IReadOnlyList<PredictedMoveStep> replayInputs)
|
||||
{
|
||||
if (state == null)
|
||||
{
|
||||
@@ -37,12 +61,12 @@ namespace Network.NetworkApplication
|
||||
|
||||
if (LastAuthoritativeTick.HasValue && state.Tick <= LastAuthoritativeTick.Value)
|
||||
{
|
||||
replayInputs = Array.Empty<MoveInput>();
|
||||
replayInputs = Array.Empty<PredictedMoveStep>();
|
||||
return false;
|
||||
}
|
||||
|
||||
LastAuthoritativeTick = state.Tick;
|
||||
pendingInputs.RemoveAll(input => input.Tick <= state.Tick);
|
||||
pendingInputs.RemoveAll(input => input.Input.Tick <= state.Tick);
|
||||
replayInputs = pendingInputs.ToArray();
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user