AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Plugins/AxiReplay/NetReplay.cs

61 lines
1.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
namespace AxiReplay
{
public class NetReplay
{
2024-11-11 13:58:27 +08:00
/// <summary>
/// 客户端当前帧
/// </summary>
public int mCurrClientFrameIdx { get; private set; } = -1;
2024-09-13 13:52:37 +08:00
/// <summary>
/// 服务器远端当前帧
/// </summary>
2024-11-11 13:58:27 +08:00
public int mRemoteFrameIdx { get; private set; } = -1;
/// <summary>
/// 网络数据队列
/// </summary>
Queue<ReplayStep> mNetReplayQueue = new Queue<ReplayStep>();
2024-09-13 13:52:37 +08:00
/// <summary>
2024-11-11 13:58:27 +08:00
/// 当前数据
2024-09-13 13:52:37 +08:00
/// </summary>
2024-11-11 13:58:27 +08:00
ReplayStep mCurrReplay;
public NetReplay()
{
2024-11-11 13:58:27 +08:00
ResetData();
}
2024-09-14 17:43:08 +08:00
public void ResetData()
{
2024-11-11 13:58:27 +08:00
mNetReplayQueue.Clear();
2024-09-14 17:43:08 +08:00
mRemoteFrameIdx = 0;
mCurrReplay = default(ReplayStep);
}
2024-11-11 11:39:38 +08:00
public void InData(ReplayStep inputData, int ServerFrameIdx)
{
2024-11-11 13:58:27 +08:00
mNetReplayQueue.Enqueue(inputData);
2024-09-14 17:43:08 +08:00
mRemoteFrameIdx = inputData.FrameStartID;
}
2024-11-11 13:58:27 +08:00
public bool TryGetNextFrame(out ReplayStep data, out int frameDiff, out bool inputDiff)
{
2024-11-11 13:58:27 +08:00
TakeFrame(1, out data, out frameDiff, out inputDiff);
return frameDiff > 0;
}
2024-11-11 13:58:27 +08:00
void TakeFrame(int addFrame, out ReplayStep data, out int bFrameDiff, out bool inputDiff)
{
2024-11-11 13:58:27 +08:00
inputDiff = false;
int targetFrame = mCurrClientFrameIdx += addFrame;
if (targetFrame <= mRemoteFrameIdx && mNetReplayQueue.Count > 0)
{
2024-11-11 13:58:27 +08:00
//当前帧追加
mCurrClientFrameIdx = targetFrame;
ulong oldInput = mCurrReplay.InPut;
mCurrReplay = mNetReplayQueue.Dequeue();
if (oldInput != mCurrReplay.InPut)
inputDiff = true;
}
2024-11-11 13:58:27 +08:00
bFrameDiff = mRemoteFrameIdx - mCurrClientFrameIdx;
data = mCurrReplay;
}
}
}