mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-26 18:26:23 +00:00
完美
This commit is contained in:
@@ -50,8 +50,8 @@ namespace Plugins.JNGame.Network
|
||||
|
||||
public override void OnClose()
|
||||
{
|
||||
client.Close();
|
||||
client.Dispose();
|
||||
client?.Close();
|
||||
client?.Dispose();
|
||||
Debug.Log($"[JNTCPClient] 关闭对象");
|
||||
base.OnClose();
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ namespace JNGame.Sync.Frame
|
||||
|
||||
public JNSyncDefaultService() : base()
|
||||
{
|
||||
|
||||
var main = UnityMainThreadDispatcher.Instance;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
@@ -128,6 +128,7 @@ namespace JNGame.Sync.Frame
|
||||
/// </summary>
|
||||
public void TStartExecute(int ms = -1)
|
||||
{
|
||||
|
||||
if (ms <= -1) ms = DeltaTime;
|
||||
|
||||
thread = new Thread(() =>
|
||||
|
@@ -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)
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -46,17 +46,21 @@ namespace JNGame.Sync.System.View
|
||||
|
||||
protected abstract KeyValue<Type, int> TClass { get; }
|
||||
protected Dictionary<Type, JNInputBase> TNewClass = new ();
|
||||
protected readonly Dictionary<int, JNInputBase> Inputs = new ();
|
||||
protected readonly Dictionary<int, JNInputBase> UIInputs = new ();
|
||||
protected readonly Dictionary<int, Dictionary<int,JNInputBase>> SInputs = new ();
|
||||
|
||||
protected Dictionary<int,JNFrameInput> frame = new();
|
||||
protected Dictionary<int,List<JNFrameInput>> frame = new();
|
||||
|
||||
/// <summary>
|
||||
/// 移入数据
|
||||
/// </summary>
|
||||
public void Enqueue(JNFrameInput input)
|
||||
{
|
||||
frame[input.NId] = input;
|
||||
if (!frame.ContainsKey(input.NId))
|
||||
{
|
||||
frame[input.NId] = new List<JNFrameInput>();
|
||||
}
|
||||
frame[input.NId].Add(input);
|
||||
}
|
||||
|
||||
protected JNInputSystem()
|
||||
@@ -130,11 +134,14 @@ namespace JNGame.Sync.System.View
|
||||
/// <returns></returns>
|
||||
public T Input<T>() where T : JNInputBase,new()
|
||||
{
|
||||
if (!(Inputs.TryGetValue(TClass.Key2Value(typeof(T)),out var input)))
|
||||
lock (UIInputs)
|
||||
{
|
||||
Inputs[TClass.Key2Value(typeof(T))] = input = new T();
|
||||
if (!(UIInputs.TryGetValue(TClass.Key2Value(typeof(T)),out var input)))
|
||||
{
|
||||
UIInputs[TClass.Key2Value(typeof(T))] = input = new T();
|
||||
}
|
||||
return (T)input;
|
||||
}
|
||||
return (T)input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -143,17 +150,20 @@ namespace JNGame.Sync.System.View
|
||||
/// <returns></returns>
|
||||
public JNFrameInputs Dequeue()
|
||||
{
|
||||
JNFrameInputs inputs = new JNFrameInputs();
|
||||
foreach (var key in Inputs.Keys)
|
||||
lock (UIInputs)
|
||||
{
|
||||
var input = new JNFrameInput();
|
||||
var info = Inputs[key];
|
||||
input.NId = key;
|
||||
input.Input = ByteString.CopyFrom(info.Encoder());
|
||||
inputs.Inputs.Add(input);
|
||||
JNFrameInputs inputs = new JNFrameInputs();
|
||||
foreach (var key in UIInputs.Keys)
|
||||
{
|
||||
var input = new JNFrameInput();
|
||||
var info = UIInputs[key];
|
||||
input.NId = key;
|
||||
input.Input = ByteString.CopyFrom(info.Encoder());
|
||||
inputs.Inputs.Add(input);
|
||||
}
|
||||
UIInputs.Clear();
|
||||
return inputs;
|
||||
}
|
||||
Inputs.Clear();
|
||||
return inputs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -166,19 +176,19 @@ namespace JNGame.Sync.System.View
|
||||
//解析输入
|
||||
foreach (var kInput in frame)
|
||||
{
|
||||
var input = kInput.Value;
|
||||
var tClass = TClass.Value2Key(input.NId);
|
||||
|
||||
if (!(SInputs.TryGetValue(input.NId,out var inputs)))
|
||||
foreach (var input in kInput.Value)
|
||||
{
|
||||
SInputs.Add(input.NId, inputs = new Dictionary<int, JNInputBase>());
|
||||
var tClass = TClass.Value2Key(input.NId);
|
||||
|
||||
if (!(SInputs.TryGetValue(input.NId,out var inputs)))
|
||||
{
|
||||
SInputs.Add(input.NId, inputs = new Dictionary<int, JNInputBase>());
|
||||
}
|
||||
|
||||
inputs[input.ClientId] = TNewClass[tClass].Decoder(input.Input.ToByteArray()) as JNInputBase;
|
||||
}
|
||||
|
||||
inputs.Add(input.ClientId,TNewClass[tClass].Decoder(input.Input.ToByteArray()) as JNInputBase);
|
||||
|
||||
kInput.Value.Clear();
|
||||
}
|
||||
frame.Clear();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae13387f055c48969fe5a2229582c832
|
||||
timeCreated: 1721635230
|
@@ -6,11 +6,9 @@ using AppGame.Systems.CServer;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using DotRecast.Core.Collections;
|
||||
using Game.Input;
|
||||
using Game.JNGFrame.Logic;
|
||||
using Game.JNGFrame.Logic.Entity;
|
||||
using Game.JNGFrame.View;
|
||||
using Game.JNGState.Logic.Data;
|
||||
using Game.Logic.System;
|
||||
using Game.Logic.System.Logic;
|
||||
using Game.Logic.System.Usual;
|
||||
using JNGame.Sync.State.Tile;
|
||||
@@ -28,7 +26,7 @@ namespace AppGame.Sync
|
||||
/// </summary>
|
||||
public class JNGTileServerSystem : JNSSTileServerService
|
||||
{
|
||||
|
||||
|
||||
protected List<JNFrameInput> Inputs = new();
|
||||
|
||||
//区块Socket
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using Game.Logic.Entity.Nodes.Component;
|
||||
using Game.Logic.Entity.Nodes.Component.Components;
|
||||
using JNGame.Math;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Game.JNGFrame.Logic.Entity.Controller
|
||||
{
|
||||
@@ -50,6 +51,7 @@ namespace Game.JNGFrame.Logic.Entity.Controller
|
||||
{
|
||||
|
||||
if (Input.SInput<IDPlayer>(Auth) is not IDPlayer input || input.MoveVector is null) return;
|
||||
Debug.Log($"收到移动数据: ${input.MoveVector.ToLVector2()}");
|
||||
Move.Vector(input.MoveVector.ToLVector2());
|
||||
|
||||
}
|
||||
|
@@ -44,6 +44,7 @@ namespace Game.JNGFrame.View.Entity
|
||||
|
||||
public override void ViewRemove(GameObject view)
|
||||
{
|
||||
view.transform.DOKill();
|
||||
Object.Destroy(view);
|
||||
}
|
||||
}
|
||||
|
@@ -41,6 +41,7 @@ namespace Game.JNGFrame.View.Entity
|
||||
|
||||
public override void ViewRemove(GameObject view)
|
||||
{
|
||||
view.transform.DOKill();
|
||||
Object.Destroy(view);
|
||||
}
|
||||
}
|
||||
|
@@ -26,7 +26,6 @@ namespace Game.JNGFrame.View.Entity
|
||||
//更新位置
|
||||
if (data.Value.Position != null)
|
||||
{
|
||||
view.transform.DOKill();
|
||||
view.transform.DOMove(data.Value.Position.ToVector3(), 0.5f);
|
||||
}
|
||||
|
||||
@@ -59,6 +58,7 @@ namespace Game.JNGFrame.View.Entity
|
||||
|
||||
public override void ViewRemove(GameObject view)
|
||||
{
|
||||
view.transform.DOKill();
|
||||
Object.Destroy(view);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user