mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -85,6 +85,7 @@ namespace Plugins.JNGame.Network
|
||||
{
|
||||
|
||||
//发送消息
|
||||
if(!_event.EventHandlers.ContainsKey($"{data.HId}")) return;
|
||||
List<Delegate> funs = _event.EventHandlers[$"{data.HId}"];
|
||||
funs.ForEach(fun =>
|
||||
{
|
||||
|
@@ -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;
|
||||
|
@@ -1,31 +1,55 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Plugins.JNGame.Util
|
||||
{
|
||||
public static class RandomUtil
|
||||
{
|
||||
|
||||
public static Func<double> SyncRandom(int seed,int start = 1)
|
||||
public static Func<float> SyncRandom(int seed = 1000000,int start = 10)
|
||||
{
|
||||
|
||||
for(var i = 0; i < start; i++){
|
||||
seed = (seed * 9301 + 49297) % 233280;
|
||||
}
|
||||
// 线性同余生成器的参数
|
||||
const int a = 1103515245;
|
||||
const int m = 2147483647; // 2^31 - 1
|
||||
const int increment = 16807;
|
||||
|
||||
for (var i = 0; i < start; i++)
|
||||
{
|
||||
seed = (a * seed + increment) % m;
|
||||
}
|
||||
|
||||
return () =>
|
||||
{
|
||||
seed = (a * seed + increment) % m;
|
||||
var rnd = Math.Abs((float)seed / m); // 转换为0.0到1.0之间的数
|
||||
return rnd; // 返回随机浮点数
|
||||
};
|
||||
|
||||
return () =>
|
||||
{
|
||||
seed = (seed * 9301 + 49297) % 233280;
|
||||
var rnd = seed / 233280.0;
|
||||
return 0 + rnd * (1 - 0);
|
||||
};
|
||||
}
|
||||
|
||||
public static Func<float,float,float> SyncRandomFloat(int seed = 1000000,int start = 10)
|
||||
{
|
||||
|
||||
var nRandom = SyncRandom(seed, start);
|
||||
|
||||
return (float min,float max) =>
|
||||
{
|
||||
var random = nRandom();
|
||||
return random * (max - min) + min;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public static Func<int,int,int> SyncRandomInt(int seed,int start = 1)
|
||||
public static Func<int,int,int> SyncRandomInt(int seed = 2000000,int start = 10)
|
||||
{
|
||||
|
||||
var nRandom = SyncRandom(seed, start);
|
||||
|
||||
return (min, max) => (int)(Math.Round(nRandom() * (max - min)) + min);
|
||||
return (min, max) =>
|
||||
{
|
||||
var random = nRandom();
|
||||
return (int)Math.Round(random * (max - min) + min);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user