2024-09-11 18:10:47 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace AxiReplay
|
|
|
|
|
{
|
|
|
|
|
public class NetReplay
|
|
|
|
|
{
|
2024-09-14 17:22:01 +08:00
|
|
|
|
public int mCurrPlayFrame = -1;
|
2024-09-11 18:10:47 +08:00
|
|
|
|
Queue<ReplayStep> mQueueReplay;
|
|
|
|
|
ReplayStep mNextReplay;
|
|
|
|
|
ReplayStep mCurrReplay;
|
|
|
|
|
int byFrameIdx = 0;
|
2024-09-13 13:52:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 服务器远端当前帧
|
|
|
|
|
/// </summary>
|
2024-09-14 17:43:08 +08:00
|
|
|
|
public int mRemoteFrameIdx { get; private set; }
|
2024-09-13 13:52:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前帧和服务器帧相差数量
|
|
|
|
|
/// </summary>
|
2024-09-14 17:43:08 +08:00
|
|
|
|
public int remoteFrameDiff => mRemoteFrameIdx - mCurrPlayFrame;
|
2024-09-11 18:10:47 +08:00
|
|
|
|
public NetReplay()
|
|
|
|
|
{
|
|
|
|
|
mQueueReplay = new Queue<ReplayStep>();
|
|
|
|
|
}
|
2024-09-14 17:43:08 +08:00
|
|
|
|
|
|
|
|
|
public void ResetData()
|
|
|
|
|
{
|
|
|
|
|
mQueueReplay.Clear();
|
|
|
|
|
mRemoteFrameIdx = 0;
|
|
|
|
|
byFrameIdx = 0;
|
|
|
|
|
mNextReplay = default(ReplayStep);
|
|
|
|
|
mCurrReplay = default(ReplayStep);
|
|
|
|
|
}
|
2024-11-11 11:39:38 +08:00
|
|
|
|
|
|
|
|
|
public void InData(ReplayStep inputData, int ServerFrameIdx)
|
2024-09-11 18:10:47 +08:00
|
|
|
|
{
|
|
|
|
|
mQueueReplay.Enqueue(inputData);
|
2024-09-14 17:43:08 +08:00
|
|
|
|
mRemoteFrameIdx = inputData.FrameStartID;
|
2024-09-11 18:10:47 +08:00
|
|
|
|
}
|
2024-11-11 11:39:38 +08:00
|
|
|
|
|
2024-09-11 18:10:47 +08:00
|
|
|
|
public bool NextFrame(out ReplayStep data, out int FrameDiff)
|
|
|
|
|
{
|
|
|
|
|
return TakeFrame(0, out data, out FrameDiff);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 往前推进帧的,指定帧下标
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool NextFramebyFrameIdx(int FrameID, out ReplayStep data, out int FrameDiff)
|
|
|
|
|
{
|
|
|
|
|
bool res = TakeFrame(FrameID - byFrameIdx, out data, out FrameDiff);
|
|
|
|
|
byFrameIdx = FrameID;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
public bool TakeFrame(int addFrame, out ReplayStep data, out int FrameDiff)
|
|
|
|
|
{
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
mCurrPlayFrame += addFrame;
|
|
|
|
|
if (mCurrPlayFrame >= mNextReplay.FrameStartID)
|
|
|
|
|
{
|
|
|
|
|
Changed = mCurrReplay.InPut != mNextReplay.InPut;
|
|
|
|
|
mCurrReplay = mNextReplay;
|
|
|
|
|
data = mCurrReplay;
|
|
|
|
|
UpdateNextFrame(mCurrPlayFrame, out FrameDiff);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data = mCurrReplay;
|
2024-09-14 17:43:08 +08:00
|
|
|
|
FrameDiff = mRemoteFrameIdx - mCurrPlayFrame;
|
2024-09-11 18:10:47 +08:00
|
|
|
|
}
|
|
|
|
|
return Changed;
|
|
|
|
|
}
|
2024-11-11 11:39:38 +08:00
|
|
|
|
void UpdateNextFrame(int targetFrame, out int FrameDiff)
|
2024-09-11 18:10:47 +08:00
|
|
|
|
{
|
2024-09-14 17:43:08 +08:00
|
|
|
|
FrameDiff = mRemoteFrameIdx - targetFrame;
|
2024-09-11 18:10:47 +08:00
|
|
|
|
//如果已经超过
|
|
|
|
|
while (targetFrame > mNextReplay.FrameStartID)
|
|
|
|
|
{
|
2024-09-14 17:43:08 +08:00
|
|
|
|
if (mNextReplay.FrameStartID >= mRemoteFrameIdx)
|
2024-09-11 18:10:47 +08:00
|
|
|
|
{
|
|
|
|
|
//TODO
|
|
|
|
|
//bEnd = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mQueueReplay.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
mNextReplay = mQueueReplay.Dequeue();
|
|
|
|
|
}
|
|
|
|
|
targetFrame++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|