This commit is contained in:
DESKTOP-5RP3AKU\Jisol
2024-09-13 04:06:25 +08:00
parent bf7b5b1160
commit ced7fdce74
72 changed files with 25000 additions and 8157 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using DotRecast.Core.Collections;
using UnityEngine;
using NotImplementedException = System.NotImplementedException;
namespace JNGame.Sync.System.Data
@@ -72,7 +73,7 @@ namespace JNGame.Sync.System.Data
public bool isClient => Type is SStateDataEnum.ServerClient or SStateDataEnum.Client;
//待插入的数据
private Queue<Dictionary<ulong, byte[]>> WaitUBytes = new ();
protected Queue<Dictionary<ulong, byte[]>> WaitUBytes = new ();
protected SStateDataSystem(SStateDataEnum type)
{
@@ -111,7 +112,7 @@ namespace JNGame.Sync.System.Data
});
if (UBytes.Count > 0)
{
OnInsertUBytes(UBytes);
OnUByteUpdate(UBytes);
OnSendUBytes(UBytes);
UBytes.Clear();
}
@@ -132,7 +133,14 @@ namespace JNGame.Sync.System.Data
/// <returns></returns>
public void OnInsertUBytes(Dictionary<ulong, byte[]> bytes)
{
WaitUBytes.Enqueue(bytes);
if (bytes is not null)
{
WaitUBytes.Enqueue(bytes);
}
else
{
Debug.Log("有数据是空");
}
}
/// <summary>
@@ -161,6 +169,7 @@ namespace JNGame.Sync.System.Data
/// </summary>
public virtual void OnUByteUpdate(Dictionary<ulong, byte[]> bytes)
{
if (bytes is null) return;
lock (Data)
{
foreach (var info in bytes)

View File

@@ -43,6 +43,9 @@ namespace JNGame.Sync.System.Data
{
public JNTileEntity Entity;
//是否需要主动推送一次[一般用在从服务器获得主服务器消息后主动给客户端推送] 为什么不会自动推送因为这个实体自己没有权限
public bool IsActiveSyncOnce = false;
/// <summary>
/// 绑定实体到数据
@@ -107,8 +110,58 @@ namespace JNGame.Sync.System.Data
}
}
public override void OnSyncUpdate(int dt)
{
while (WaitUBytes.Count > 0)
{
OnUByteUpdate(WaitUBytes.Dequeue());
}
//服务器: 发送最近数据
if (isServer)
{
var latest = GetLatest();
latest.Keys.ForEach(key =>
{
if (Data.ContainsKey(key))
{
//更新数据
Update(latest[key]);
}
else
{
//如果之前没有则添加
Add(latest[key]);
}
});
Data.ForEach(child =>
{
//没有则删除
if (!(latest.ContainsKey(child.Key)))
{
Delete(child.Key);
return;
}
//主动更新
if (child.Value.IsActiveSyncOnce)
{
UBytes[child.Key] = child.Value.GetByte();
}
});
if (UBytes.Count > 0)
{
OnUByteUpdate(UBytes);
OnSendUBytes(UBytes);
UBytes.Clear();
}
}
}
public override void OnSendUBytes(Dictionary<ulong, byte[]> bytes)
{
Dictionary<ulong, byte[]> all = bytes;
Dictionary<ulong, byte[]> master = new Dictionary<ulong, byte[]>();
Dictionary<ulong, byte[]> slave = new Dictionary<ulong, byte[]>();
@@ -165,6 +218,8 @@ namespace JNGame.Sync.System.Data
{
//并且同步属性到实体中
child.TileSyncData(data);
//如果是从服务器则主动推送数据
if (IsSlave) data.IsActiveSyncOnce = true;
}
}
});
@@ -173,12 +228,16 @@ namespace JNGame.Sync.System.Data
foreach (var keyValue in lIsTileData)
{
var entity = NodeContext.TileSyncCreate(keyValue.Key);
if (entity is null) continue;
//如果当前是从服务器则同步的实体都是 从 主服务器 同步给 从服务器
if (IsSlave) entity.IsSyncSlave = true;
entity?.TileSyncData(keyValue.Value);
//将实体绑定到数据中
keyValue.Value.BindEntity(entity);
//如果是从服务器则主动推送数据
if (IsSlave) keyValue.Value.IsActiveSyncOnce = true;
}
}