mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -26,7 +26,7 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
//同步时间 (和服务器保持一致)
|
||||
private int _nSyncTime = 67;
|
||||
//大于多少帧进行追帧
|
||||
private int _nMaxFrameBan = 3;
|
||||
private int _nMaxFrameBan = 4;
|
||||
//大于多少帧进行快速追帧
|
||||
private int _nMaxFrameLoopBan = 18;
|
||||
//将服务器帧数进行平分
|
||||
@@ -36,11 +36,17 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
//帧队列
|
||||
private Queue<JNFrameInfo> _nFrameQueue = new();
|
||||
|
||||
//本地已执行帧数
|
||||
private int _nLocalRunFrame = 0;
|
||||
//本地帧数
|
||||
private int _nLocalFrame = 0;
|
||||
//运行的帧
|
||||
private int _nFrameRun = 0;
|
||||
|
||||
//服务器的帧
|
||||
private int _nServerFrame = 0;
|
||||
|
||||
public int NLocalRunFrame => _nLocalRunFrame;
|
||||
public int NLocalFrame => _nLocalFrame;
|
||||
public int NServerFrame => _nServerFrame;
|
||||
|
||||
//暂存帧列表
|
||||
private Dictionary<int,JNFrameInfo> _nFrameTempQueue = new();
|
||||
|
||||
@@ -51,17 +57,22 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
public Func<int> nSyncID = RandomUtil.Next(0);
|
||||
|
||||
//随机数
|
||||
public Func<double> nRandom = RandomUtil.SyncRandom(100);
|
||||
public Func<float> nRandom = RandomUtil.SyncRandom();
|
||||
|
||||
//随机数double
|
||||
public Func<float,float,float> nRandomFloat = RandomUtil.SyncRandomFloat();
|
||||
|
||||
//随机数整数
|
||||
public Func<int, int, int> nRandomInt = RandomUtil.SyncRandomInt(100);
|
||||
public Func<int, int, int> nRandomInt = RandomUtil.SyncRandomInt();
|
||||
|
||||
//是否开始同步
|
||||
Boolean _isStart = false;
|
||||
|
||||
//是否请求后台数据
|
||||
private Boolean _isRequestServerData = false;
|
||||
|
||||
|
||||
public bool IsRequestServerData => _isRequestServerData;
|
||||
|
||||
//帧更新
|
||||
int dtTotal = 0;
|
||||
//输入更新
|
||||
@@ -88,13 +99,15 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
|
||||
this._isStart = false;
|
||||
this.nSyncID = RandomUtil.Next(0);
|
||||
this.nRandom = RandomUtil.SyncRandom(100);
|
||||
this.nRandomInt = RandomUtil.SyncRandomInt(100);
|
||||
this.nRandom = RandomUtil.SyncRandom();
|
||||
this.nRandomFloat = RandomUtil.SyncRandomFloat();
|
||||
this.nRandomInt = RandomUtil.SyncRandomInt();
|
||||
this._nSyncActors = new();
|
||||
this._nFrameQueue = new();
|
||||
this._nFrameTempQueue = new();
|
||||
this._nLocalFrame = 0;
|
||||
this._nFrameRun = 0;
|
||||
this._nServerFrame = 0;
|
||||
this._nLocalRunFrame = 0;
|
||||
|
||||
Physics.SyncTransforms();
|
||||
EventDispatcher.Event.Dispatch(JNSyncFrameEvent.CREATE);
|
||||
@@ -120,10 +133,11 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
}
|
||||
}else{
|
||||
//追帧运行 保持前端 15 帧 刷新
|
||||
int endTime = DateTime.Now.Millisecond + 66;
|
||||
while(this.DyTime() == 0 && DateTime.Now.Millisecond < endTime){
|
||||
long endTime = (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) + 66;
|
||||
while(this.DyTime() == 0 && (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) < endTime){
|
||||
this.onUpdate();
|
||||
}
|
||||
dtTotal = 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -132,6 +146,9 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
public void onUpdate()
|
||||
{
|
||||
if(!(_nFrameQueue.TryDequeue(out var frame))) return;
|
||||
|
||||
if(frame.Index != 0)
|
||||
this._nLocalRunFrame = frame.Index;
|
||||
|
||||
int dt = this._nSyncTime / this._nDivideFrame;
|
||||
|
||||
@@ -165,7 +182,7 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
}
|
||||
|
||||
//自适应间隔时间
|
||||
private int DyTime(){
|
||||
public int DyTime(){
|
||||
int dt = this._nSyncTime / this._nDivideFrame;
|
||||
int loop = dt;
|
||||
|
||||
@@ -186,6 +203,7 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
}else{
|
||||
loop = dt;
|
||||
}
|
||||
|
||||
return loop;
|
||||
}
|
||||
|
||||
@@ -199,13 +217,15 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
|
||||
if (!_isStart) return;
|
||||
|
||||
if(isLatestData){
|
||||
//如果推的帧小于本地帧 则 重开
|
||||
if(frame.Index < _nLocalFrame){
|
||||
OnReset();
|
||||
OnStart();
|
||||
return;
|
||||
}
|
||||
if(isLatestData)
|
||||
{
|
||||
this._nServerFrame = frame.Index;
|
||||
// //如果推的帧小于本地帧 则 重开
|
||||
// if(frame.Index < _nLocalFrame){
|
||||
// OnReset();
|
||||
// OnStart();
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
|
||||
//我需要的下一帧
|
||||
@@ -219,23 +239,28 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
//在未列入中拿到需要的帧
|
||||
JNFrameInfo tamp = null;
|
||||
|
||||
if ((tamp = _nFrameTempQueue.GetValueOrDefault(index,null)) == null){
|
||||
if ((tamp = _nFrameTempQueue.GetValueOrDefault(index,null)) == null)
|
||||
{
|
||||
|
||||
var that = this;
|
||||
|
||||
//如果没有则向服务器请求我需要的帧数
|
||||
if (!this._isRequestServerData)
|
||||
if (!that._isRequestServerData)
|
||||
{
|
||||
this._isRequestServerData = true;
|
||||
|
||||
that._isRequestServerData = true;
|
||||
|
||||
//请求
|
||||
this.OnServerData(this._nLocalFrame, 0).ContinueWith(infos =>
|
||||
that.OnServerData(this._nLocalFrame, 0).ContinueWith(infos =>
|
||||
{
|
||||
foreach (var frameInfo in infos.Frames)
|
||||
{
|
||||
this.AddInput(frameInfo,false);
|
||||
if(frameInfo.Index >= this._nLocalFrame)
|
||||
that.AddInput(frameInfo,false);
|
||||
}
|
||||
|
||||
this._isRequestServerData = false;
|
||||
that._isRequestServerData = false;
|
||||
}).Forget();
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
|
Reference in New Issue
Block a user