This commit is contained in:
PC-20230316NUNE\Administrator
2024-02-04 16:17:39 +08:00
parent f04299219c
commit 04043cc6fc
51 changed files with 20366 additions and 2051 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9606b3aa4a2d4390890ae975ea7567d3
timeCreated: 1707030980

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Plugins.JNGame.Util;
namespace Plugins.JNGame.Sync.Frame.game.Time
{
public class JNFrameTimeHandler
{
public int ID;
public Action Handler;
public int Timeout;
}
//帧同步定时器
public class JNFrameTime : Singleton<JNFrameTime>
{
private int _id = 0;
private List<JNFrameTimeHandler> _handlers = new();
//设置定时器
public int SetTimeout(Action handler,int timeout = 0)
{
var funHandler = new JNFrameTimeHandler()
{
ID = this._id++,
Handler = handler,
Timeout = timeout
};
this._handlers.Add(funHandler);
return funHandler.ID;
}
public void Update(int dt)
{
this._handlers.ToList().ForEach(action =>
{
action.Timeout -= dt;
if (action.Timeout <= 0)
{
action.Handler();
this._handlers.Remove(action);
}
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d96b5b6d30384c7482ce18479c06ebfb
timeCreated: 1707030984

View File

@@ -7,6 +7,7 @@ using Cysharp.Threading.Tasks;
using Google.Protobuf;
using Plugins.JNGame.Sync.Frame.Entity;
using Plugins.JNGame.Sync.Frame.game;
using Plugins.JNGame.Sync.Frame.game.Time;
using Plugins.JNGame.System;
using Plugins.JNGame.Util;
using UnityEngine;
@@ -33,7 +34,7 @@ namespace Plugins.JNGame.Sync.Frame
//大于多少帧进行快速追帧
private int _nMaxFrameLoopBan = 18;
//将服务器帧数进行平分
private int _nDivideFrame = 3;
private int _nDivideFrame = 2;
public int NSyncTime => _nSyncTime;
public int NMaxFrameBan => _nMaxFrameBan;
@@ -85,6 +86,10 @@ namespace Plugins.JNGame.Sync.Frame
int dtTotal = 0;
//输入更新
int dtInputTotal = 0;
//是否在追帧
private Boolean _isLoop = false;
public bool IsLoop => _isLoop;
public JNTime Time => (new JNTime(this));
@@ -122,7 +127,10 @@ namespace Plugins.JNGame.Sync.Frame
this.dtInputTotal = 0;
this._isRequestServerData = false;
Physics.SyncTransforms();
//清除定时器
SingletonUtil<JNFrameTime>.Clean();
// Physics.SyncTransforms();
EventDispatcher.Event.Dispatch(JNSyncFrameEvent.CREATE);
}
@@ -140,6 +148,7 @@ namespace Plugins.JNGame.Sync.Frame
if(nSyncTime > 0){
while(nSyncTime != 0 && this.dtTotal > nSyncTime){
this._isLoop = false;
this.OnUpdate();
this.dtTotal -= nSyncTime;
nSyncTime = this.DyTime();
@@ -147,7 +156,9 @@ namespace Plugins.JNGame.Sync.Frame
}else{
//追帧运行 保持前端 15 帧 刷新
long endTime = (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) + 66;
while(this.DyTime() == 0 && (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) < endTime){
while(this.DyTime() == 0 && (new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()) < endTime)
{
this._isLoop = true;
this.OnUpdate();
}
dtTotal = 0;
@@ -211,7 +222,7 @@ namespace Plugins.JNGame.Sync.Frame
inputs[message.NId] = message;
}
Debug.Log(inputs.Count);
// Debug.Log(inputs.Count);
//运行之前帧
this._nSyncActors.ToList().ForEach(child =>
@@ -243,9 +254,11 @@ namespace Plugins.JNGame.Sync.Frame
}
});
//执行下一帧物理
Physics.Simulate((float)dt / 1000);
Physics.SyncTransforms();
//执行定时器
SingletonUtil<JNFrameTime>.Instance.Update(dt);
// //执行下一帧物理
// Physics.Simulate((float)dt / 1000);
// Physics.SyncTransforms();
}