mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using BestHTTP.WebSocket;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Google.Protobuf;
|
||||
using Plugins.JNGame.Network.Action;
|
||||
using Plugins.JNGame.Network.Entity;
|
||||
using Plugins.JNGame.Network.Util;
|
||||
using Plugins.JNGame.System;
|
||||
|
@@ -6,40 +6,45 @@ namespace Plugins.JNGame.Sync.Frame.Entity
|
||||
|
||||
private JNSyncFrame _sync;
|
||||
|
||||
public static JNTime Time;
|
||||
public static JNTime Time = new JNTime();
|
||||
|
||||
/// <summary>
|
||||
/// 在Unity中,Time.time是一个非常有用的属性,它表示从游戏开始运行到现在的时间,单位是秒。它常常被用来作为游戏内时间或者帧时间的参考。
|
||||
/// </summary>
|
||||
public float time => (this.deltaTime * _sync.NLocalRunFrame);
|
||||
public float time => _sync == null ? UnityEngine.Time.time : (this.deltaTime * _sync.NLocalRunFrame);
|
||||
|
||||
/// <summary>
|
||||
/// 在Unity中,Time.deltaTime是一个表示上一帧所花费时间的属性,单位是秒。它用于在游戏运行时提供稳定的时间测量,不受帧率变化的影响
|
||||
/// </summary>
|
||||
public float deltaTime => ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
public float deltaTime => _sync == null ? UnityEngine.Time.deltaTime : ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
|
||||
/// <summary>
|
||||
/// Time.fixedDeltaTime是一个在Unity中表示以秒为单位的固定时间间隔的属性,通常用于物理计算和其他需要固定帧率更新的场景。它与Time.deltaTime不同,因为Time.fixedDeltaTime可以自行赋值,并且用于固定帧率更新(如FixedUpdate()函数)。
|
||||
/// </summary>
|
||||
public float fixedDeltaTime => ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
public float fixedDeltaTime => _sync == null ? UnityEngine.Time.fixedDeltaTime : ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
|
||||
/// <summary>
|
||||
/// Time.frameCount 表示已经渲染的总帧数。这个属性通常用于性能测试,例如,你可以使用它来检查游戏是否在每一帧都进行了必要的渲染工作,或者是否在某些帧上出现了渲染瓶颈。
|
||||
/// </summary>
|
||||
public int frameCount => _sync.NLocalRunFrame;
|
||||
public int frameCount => _sync == null ? UnityEngine.Time.frameCount : _sync.NLocalRunFrame;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Time.realtimeSinceStartup是Unity中表示自游戏启动以来经过的实时时间(以秒为单位)的属性。这个时间不受Time.timeScale的影响,即使游戏暂停或慢动作,它的值也会持续增加,因为它是独立于游戏时间的。
|
||||
/// </summary>
|
||||
public float realtimeSinceStartup => (this.time);
|
||||
public float realtimeSinceStartup => _sync == null ? UnityEngine.Time.realtimeSinceStartup : (this.time);
|
||||
|
||||
/// <summary>
|
||||
/// Time.unscaledDeltaTime 是 Unity 中的一个属性,表示上一帧所花费的时间,单位是秒。与 Time.deltaTime 不同,Time.unscaledDeltaTime 是未经过时间缩放的,也就是说它不受 Time.timeScale 的影响。
|
||||
/// </summary>
|
||||
public float unscaledDeltaTime => ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
public float unscaledDeltaTime => _sync == null ? UnityEngine.Time.unscaledDeltaTime : ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
|
||||
public float smoothDeltaTime => ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
public float smoothDeltaTime => _sync == null ? UnityEngine.Time.smoothDeltaTime : ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
|
||||
/// <summary>
|
||||
/// Time.timeSinceLevelLoad是一个表示从当前场景开始到现在所经过的时间的属性,单位是秒。这个属性通常用于计算从加载关卡开始到当前帧的时间,并且会随着游戏的暂停而停止计算
|
||||
/// </summary>
|
||||
public float timeSinceLevelLoad => _sync == null ? UnityEngine.Time.timeSinceLevelLoad : ((float)_sync.NSyncTime / _sync.NDivideFrame) / 1000;
|
||||
|
||||
|
||||
public JNTime(JNSyncFrame sync)
|
||||
@@ -47,6 +52,10 @@ namespace Plugins.JNGame.Sync.Frame.Entity
|
||||
Time = this;
|
||||
this._sync = sync;
|
||||
}
|
||||
public JNTime()
|
||||
{
|
||||
Time = this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Plugins.JNGame.Sync.Frame.game
|
||||
{
|
||||
@@ -9,9 +10,16 @@ namespace Plugins.JNGame.Sync.Frame.game
|
||||
public int _nId;
|
||||
[HideInInspector]
|
||||
public int NID => _nId;
|
||||
|
||||
public abstract Boolean IsInput();
|
||||
|
||||
//清空输入
|
||||
public abstract void ClearInput();
|
||||
|
||||
//获取同步类
|
||||
protected abstract JNSyncFrame GetSync();
|
||||
|
||||
|
||||
public abstract byte[] Encoder();
|
||||
|
||||
}
|
||||
}
|
@@ -12,16 +12,28 @@ namespace Plugins.JNGame.Sync.Frame.game
|
||||
|
||||
//当前输入
|
||||
private T _input;
|
||||
public T NInput
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._input == null)
|
||||
this._input = Activator.CreateInstance<T>();
|
||||
return this._input;
|
||||
}
|
||||
}
|
||||
|
||||
//是否有输入
|
||||
public Boolean isInput => this._input == null;
|
||||
|
||||
public override bool IsInput()
|
||||
{
|
||||
return this._input != null;
|
||||
}
|
||||
|
||||
//是否初始化完成
|
||||
[HideInInspector]
|
||||
public Boolean isSyncInitSuccess = false;
|
||||
|
||||
//清空输入
|
||||
public void ClearInput(){
|
||||
public override void ClearInput(){
|
||||
this._input = default(T);
|
||||
}
|
||||
|
||||
@@ -49,8 +61,14 @@ namespace Plugins.JNGame.Sync.Frame.game
|
||||
public byte[] Encoder(T input){
|
||||
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(input));
|
||||
}
|
||||
|
||||
|
||||
public override byte[] Encoder()
|
||||
{
|
||||
return this.Encoder(this.NInput);
|
||||
}
|
||||
|
||||
//帧同步
|
||||
public abstract void OnLateSyncUpdate(int dt,JNFrameInfo frame,T input);
|
||||
public abstract void OnSyncUpdate(int dt,JNFrameInfo frame,T input);
|
||||
|
||||
|
||||
|
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Google.Protobuf;
|
||||
using Plugins.JNGame.Sync.Frame.Entity;
|
||||
using Plugins.JNGame.Sync.Frame.game;
|
||||
using Plugins.JNGame.System;
|
||||
@@ -138,7 +140,7 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
|
||||
if(nSyncTime > 0){
|
||||
while(nSyncTime != 0 && this.dtTotal > nSyncTime){
|
||||
this.onUpdate();
|
||||
this.OnUpdate();
|
||||
this.dtTotal -= nSyncTime;
|
||||
nSyncTime = this.DyTime();
|
||||
}
|
||||
@@ -146,15 +148,53 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
//追帧运行 保持前端 15 帧 刷新
|
||||
long endTime = (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) + 66;
|
||||
while(this.DyTime() == 0 && (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) < endTime){
|
||||
this.onUpdate();
|
||||
this.OnUpdate();
|
||||
}
|
||||
dtTotal = 0;
|
||||
}
|
||||
|
||||
|
||||
//更新输入
|
||||
if(this.dtInputTotal > (this._nSyncTime / this._nDivideFrame)){
|
||||
this.dtInputTotal = 0;
|
||||
this.OnInput();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//运行操作
|
||||
private void OnInput()
|
||||
{
|
||||
|
||||
//输入数据
|
||||
JNFrameInputs inputs = new JNFrameInputs();
|
||||
|
||||
|
||||
//收集帧数据
|
||||
this._nSyncActors.ForEach(action =>
|
||||
{
|
||||
if (action.IsInput())
|
||||
{
|
||||
//如果输入了 则添加到输入数据
|
||||
var input = new JNFrameInput();
|
||||
input.NId = action.NID;
|
||||
var data = action.Encoder();
|
||||
input.Input = ByteString.CopyFrom(data);
|
||||
inputs.Inputs.Add(input);
|
||||
}
|
||||
action.ClearInput();
|
||||
});
|
||||
|
||||
//发送帧操作
|
||||
if (inputs.Inputs.Count > 0)
|
||||
{
|
||||
this.OnSendInput(inputs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//运行帧
|
||||
public void onUpdate()
|
||||
private void OnUpdate()
|
||||
{
|
||||
if(!(_nFrameQueue.TryDequeue(out var frame))) return;
|
||||
|
||||
@@ -163,15 +203,33 @@ namespace Plugins.JNGame.Sync.Frame
|
||||
|
||||
int dt = this._nSyncTime / this._nDivideFrame;
|
||||
|
||||
|
||||
//拆出输入
|
||||
Dictionary<int, JNFrameInput> inputs = new();
|
||||
foreach (var message in frame.Messages)
|
||||
{
|
||||
inputs.Add(message.NId,message);
|
||||
inputs[message.NId] = message;
|
||||
}
|
||||
|
||||
Debug.Log(inputs.Count);
|
||||
|
||||
//运行之前帧
|
||||
this._nSyncActors.ToList().ForEach(child =>
|
||||
{
|
||||
MethodInfo OnSyncUpdate = child.GetType().GetMethod("OnLateSyncUpdate");
|
||||
MethodInfo Decoder = child.GetType().GetMethod("Decoder");
|
||||
if (inputs.ContainsKey(child.NID))
|
||||
{
|
||||
OnSyncUpdate.Invoke(child,new []{dt,frame,Decoder.Invoke(child,new object[]{ inputs[child.NID].Input.ToByteArray() })});
|
||||
}
|
||||
else
|
||||
{
|
||||
OnSyncUpdate.Invoke(child,new []{ dt,frame,(object)null });
|
||||
}
|
||||
});
|
||||
|
||||
//更新帧
|
||||
this._nSyncActors.ForEach(child =>
|
||||
this._nSyncActors.ToList().ForEach(child =>
|
||||
{
|
||||
MethodInfo OnSyncUpdate = child.GetType().GetMethod("OnSyncUpdate");
|
||||
MethodInfo Decoder = child.GetType().GetMethod("Decoder");
|
||||
|
Reference in New Issue
Block a user