提交Unity 联机Pro

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-17 14:27:18 +08:00
parent f00193b000
commit 894100ae37
7448 changed files with 854473 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
using AppGame.Systems;
using Plugins.JNGame.System;
using Plugins.JNGame.Util;
using Service;
namespace AppGame
{
/// <summary>
/// App 环境
/// </summary>
public enum AppEnv
{
Client,
Server,
ServerClient,
}
public class App
{
public static AppEnv Env = AppEnv.ServerClient;
/// <summary>
/// 业务服务器
/// </summary>
public static readonly JNGSocket Business = new JNGSocket();
/// <summary>
/// Tile服务器
/// </summary>
public static readonly JNGServer Server = new JNGServer();
/// <summary>
/// Tile客户端
/// </summary>
public static readonly JNGClientGroup Client = new JNGClientGroup();
public static readonly JNGResService Resource = new JNGResService();
public static readonly JNGGame Game = new JNGGame();
public static readonly JNGSocket Socket = new JNGSocket();
public static readonly JAPI API = new(new JAPIConfig(){BaseURL = "http://127.0.0.1:8080"});
public static readonly GAPI GAPI = new GAPI();
public static int ClientID => Client.ClientID;
public static SystemBase[] AllSystem()
{
return new SystemBase[]
{
Business,
Client,
IsServer() ? Server : null,
Resource,
Game,
// Socket,
};
}
public static bool IsClient()
{
return Env is AppEnv.Client or AppEnv.ServerClient;
}
public static bool IsServer()
{
return Env is AppEnv.Server or AppEnv.ServerClient;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2195ef86653b46b598afc902afa35d36
timeCreated: 1721384370

View File

@@ -0,0 +1,57 @@
using AppGame;
using AppGame.Sync;
using Cinemachine;
using Game.Input;
using JNGame;
using JNGame.Sync.Debuger;
using UnityEngine;
public class DApplication : MonoBehaviour
{
public GameObject Player1;
public GameObject VWorld;
public CinemachineFreeLook FreeLook;
private async void Awake()
{
await JNetGame.Instance.Init(App.AllSystem());
//绑定资源
App.Resource.Register(VWorld,Player1,FreeLook);
//开始运行同步
if (App.IsServer())
{
var tileServer = App.Game.Start<JNGTileServerSystem>();
JNTileServerDebuger.Instance.Add(tileServer);
}
if (App.IsClient()) App.Game.Start<JNGTileClientSystem>();
}
private void Update()
{
App.Game.Update();
}
public void OnClickPlayerCreate()
{
var input = App.Game.GetInput<IDWorld>();
input.IsPlayerCreate = true;
}
public void OnClickButton()
{
var input = App.Game.GetInput<IDWorld>();
input.IsAdd = true;
}
public void OnClickServerCreate()
{
App.Game.Start<JNGTileServerSystem>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f213ebe3a8895ee42b56cb52bb2cb44b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,31 @@
using System;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Util;
namespace AppGame
{
public class TileServerInfo
{
public String id;
public String ip;
public int port;
}
/// <summary>
/// 游戏API
/// </summary>
public class GAPI
{
//获取ID
public UniTask<NewsContext<int>> NSyncTileId = App.API.GetNews<int>($"/sync/tile/id");
//获取端口
public UniTask<NewsContext<int>> NSyncTilePort = App.API.GetNews<int>($"/sync/tile/port");
//获取默认连接
public UniTask<NewsContext<TileServerInfo>> NSyncTileServer(int index) => App.API.GetNews<TileServerInfo>($"/sync/tile/server?index={index}");
//获取玩家Id
public UniTask<NewsContext<int>> NSyncTileClientId = App.API.GetNews<int>($"/sync/tile/client/id");
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 617d97cfd4364314aa47cbd96de41f77
timeCreated: 1723433766

View File

@@ -0,0 +1,12 @@
using Plugins.JNGame.Network.Action;
namespace AppGame
{
public enum GActionEnum : int
{
//绑定客户端Id
BindClientID
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 75e48cb9a73a47ca8f5be63285d40309
timeCreated: 1723002055

View File

@@ -0,0 +1,28 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace AppGame
{
public class Main : MonoBehaviour
{
public void SetClientEnv()
{
App.Env = AppEnv.Client;
SceneManager.LoadScene(1);
}
public void SetServerEnv()
{
App.Env = AppEnv.Server;
SceneManager.LoadScene(1);
}
public void SetServerClientEnv()
{
App.Env = AppEnv.ServerClient;
SceneManager.LoadScene(1);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: eab418b967e6433a8cb655e981dec48b
timeCreated: 1723112402

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6b8e69199e8f4057b15e5190d31d5e96
timeCreated: 1720606718

View File

@@ -0,0 +1,122 @@
using System;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using Game.Input;
using Game.JNGFrame.Logic;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.Logic.System;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
/// <summary>
/// 帧同步游戏
/// </summary>
public class JNGFrameSystem : JNSyncFrameService
{
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//帧同步逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.ServerClient), //游戏数据
};
}
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
public override bool IsStartGame => true;
public override JNContexts CreateContexts()
{
return new EDContexts();
}
protected override void OnRunSimulate()
{
if (!(NFrameQueue.TryDequeue(out var frame))) return;
//插入当前输入
frame.Messages.ForEach(child =>
{
GetSystem<DInputSystem>().Enqueue(child);
});
Simulate();
}
/// <summary>
/// 获取输入
/// </summary>
/// <returns></returns>
protected override JNFrameInputs GetInputs()
{
return GetSystem<DInputSystem>().Dequeue();
}
/// <summary>
/// 发送输入
/// </summary>
/// <param name="inputs"></param>
protected override void OnSendInput(JNFrameInputs inputs)
{
//发送帧数据给服务端
App.Socket.Send((int)NActionEnum.NSyncFrameInput,inputs);
}
/// <summary>
/// 追帧
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
protected override async UniTask<JNFrameInfos> OnServerData(int start, int end)
{
Debug.Log($"OnServerData - {start}");
try
{
var data = (await App.API.GetByte($"/sync/frame?start={start}"));
if (data is { Length: > 0 })
{
JNFrameInfos info = JNFrameInfos.Parser.ParseFrom(data);
Debug.Log($"OnServerData - {start} {end} 结束");
return info;
}
}
catch(Exception e)
{
// ignored
Debug.LogError(e.Message);
}
return new JNFrameInfos();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e596ac8b6f4144ba89fdd7ca984dd7f1
timeCreated: 1712663552

View File

@@ -0,0 +1,106 @@
using System.Collections.Generic;
using Game.Input;
using Game.JNGFrame.Logic;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.Logic.System;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using Game.Logic.System;
using JNGame.Sync.Entity;
using JNGame.Sync.State;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Plugins.JNGame.Network.Action;
namespace AppGame.Sync
{
/// <summary>
/// 状态同步[服务器]
/// </summary>
public class JNGStateServerSystem : JNSStateServerService
{
protected List<JNFrameInput> Inputs = new();
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
new DPlayerSystem(), //玩家逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.ServerClient), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.ServerClient), //游戏数据
};
}
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
public override JNContexts CreateContexts()
{
return new EDContexts();
}
protected override void OnRunSimulate()
{
//插入未处理输入
foreach (var input in Inputs)
{
GetSystem<DInputSystem>().Enqueue(input);
}
Inputs.Clear();
base.OnRunSimulate();
//发送输入
OnSendInput();
}
/// <summary>
/// 发送输入 (正常服务器是不需要发送输入的 这里用于测试)
/// </summary>
protected void OnSendInput()
{
var inputs = GetSystem<DInputSystem>().Dequeue();
if (inputs.Inputs.Count > 0)
{
//发送帧数据给服务端
App.Socket.Send((int)NActionEnum.NSyncFrameInput,inputs);
}
}
/// <summary>
/// 添加输入
/// </summary>
public void AddInput(JNFrameInfo info)
{
foreach (var input in info.Messages)
{
Inputs.Add(input);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0dbc5b160b164d41bb2138f2ed76ebb3
timeCreated: 1721811852

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 75baa1cf11cc44c2bb1e10fb46f74e50
timeCreated: 1722493271

View File

@@ -0,0 +1,199 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AppGame.Systems;
using Game.Input;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using JNGame.Math;
using JNGame.Sync.State.Tile;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using JNGame.Util;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
public class JNGTileClientSystem : JNSSTileClientService
{
//区块Socket
public Dictionary<int, JNGClient> Sockets = new ();
//玩家位置
public LVector3? PlayerPos;
public override void Initialize()
{
base.Initialize();
//默认玩家位置
SetPlayerPosition(LVector3.Zero);
//定时更新Socket
Timers.Instance.SetInterval(1f, UpdateTileSocket);
}
protected override int[][] Tiles => new[]
{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
};
protected override int TileSize => 100;
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.Client), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.Client), //游戏数据
};
}
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
protected override void OnRunSimulate()
{
//更新玩家位置
UpdatePlayerPosition();
base.OnRunSimulate();
//发送输入
OnSendInput();
}
/// <summary>
/// 发送输入 (正常服务器是不需要发送输入的 这里用于测试)
/// </summary>
private void OnSendInput()
{
var inputs = GetSystem<DInputSystem>().Dequeue();
if (inputs.Inputs.Count > 0)
{
//发送帧数据给服务端
JNStateTileInputs tileInputs = new JNStateTileInputs()
{
TId = 0,
Message = inputs
};
App.Client.Send((int)NActionEnum.NSyncTileInput,tileInputs);
}
}
/// <summary>
/// 设置玩家位置
/// </summary>
public void SetPlayerPosition(LVector3 pos)
{
PlayerPos = pos;
}
/// <summary>
/// 更新玩家位置
/// </summary>
public void UpdatePlayerPosition()
{
if (PlayerPos is null) return;
List<int> ids = GetTileGridIndex(PlayerPos.Value);
ids.ForEach(AddTileShow);
TileShow.ForEach(id =>
{
if (!(ids.Contains(id)))
{
RemoveTileShow(id);
}
});
}
/// <summary>
/// 更新区块Socket
/// </summary>
public void UpdateTileSocket()
{
TileShow.ForEach(index =>
{
if (!IsTileConnect(index))
{
AddSocket(index);
}
});
var keysToRemove = Sockets.Keys.Where(key => !TileShow.Contains(key)).ToList();
foreach (var key in keysToRemove)
{
RemoveSocket(key);
}
}
/// <summary>
/// 判断是否当前区块是否连接
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool IsTileConnect(int index)
{
return Sockets.ContainsKey(index);
}
protected async Task AddSocket(int index)
{
if (IsTileConnect(index)) return;
var client = new JNGClient();
Sockets.Add(index,client);
//获取连接
var message = (await App.GAPI.NSyncTileServer(index));
TileServerInfo info = message.data;
if (info is not null)
{
client.SetPoint(new IPEndPoint(IPAddress.Parse(info.ip),info.port));
if (IsTileConnect(index))
{
App.Client.AddClient(client);
}
}
else
{
Sockets.Remove(index);
Debug.Log("获取连接失败");
}
}
protected void RemoveSocket(int index)
{
if (Sockets.TryGetValue(index,out var client))
{
App.Client.RemoveClient(client);
}
Sockets.Remove(index);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a158f1d590a642988611d229ddaa6a1c
timeCreated: 1722493289

View File

@@ -0,0 +1,225 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AppGame.Systems;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using Game.Input;
using Game.JNGFrame.Logic;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.Logic.System;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using Game.Logic.System;
using JNGame.Sync.State.Tile;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using JNGame.Util;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
/// <summary>
/// 瓦片状态同步[服务器]
/// </summary>
public class JNGTileServerSystem : JNSSTileServerService
{
/// <summary>
/// 测试标识
/// </summary>
private static int TileId = 1;
protected List<JNFrameInput> Inputs = new();
//区块Socket
public Dictionary<int, JNGTileClient> Sockets = new ();
/// <summary>
/// 初始化服务器
/// </summary>
/// <returns></returns>
protected override async Task OnInit()
{
await base.OnInit();
//添加Tile服务器
App.Business.Send((int)NActionEnum.NAddTileServer,new JNAddTileServer()
{
Tile = TID,
Ip = "127.0.0.1",
Port = App.Server.Port
});
//定时更新Socket
Timers.Instance.SetInterval(1f, UpdateTileSocket);
}
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
new DPlayerSystem(), //玩家逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.Server), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.Server), //游戏数据
};
}
#if UNITY_EDITOR
/// <summary>
/// 编辑器显示视图层
/// </summary>
/// <returns></returns>
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
#endif
protected override JNTileContexts CreateTileContexts()
{
return new EDContexts();
}
protected override int[][] Tiles => new[]
{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
};
protected override int TileSize => 100;
protected override async UniTask<int> FetchTileId()
{
// await UniTask.NextFrame();
// return TileId++;
var message = await App.GAPI.NSyncTileId;
return message.data;
}
protected override void OnRunSimulate()
{
//插入未处理输入
foreach (var input in Inputs)
{
GetSystem<DInputSystem>().Enqueue(input);
}
Inputs.Clear();
base.OnRunSimulate();
}
/// <summary>
/// 添加输入
/// </summary>
public void AddInput(JNStateTileInputs info)
{
lock (Inputs)
{
info.Message.Inputs.ForEach(child =>
{
Inputs.Add(child);
});
}
}
/// <summary>
/// 更新区块Socket
/// </summary>
public void UpdateTileSocket()
{
//获取周围TileId
List<int> grid = GetTileGridIndex(TID);
grid.Remove(TID);
grid.ForEach(index =>
{
if (!IsTileConnect(index))
{
AddSocket(index);
}
});
}
/// <summary>
/// 判断是否当前区块是否连接
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool IsTileConnect(int index)
{
return Sockets.ContainsKey(index);
}
/// <summary>
/// 获取当前连接的区块列表
/// </summary>
/// <returns></returns>
public override int[] GetLinkTiles()
{
return Sockets.Keys.Where(key => Sockets[key].IsOpen).ToArray();
}
protected async Task AddSocket(int index)
{
if (IsTileConnect(index)) return;
var client = new JNGTileClient();
Sockets.Add(index,client);
//获取连接
var message = (await App.GAPI.NSyncTileServer(index));
TileServerInfo info = message.data;
if (info is not null)
{
client.SetPoint(new IPEndPoint(IPAddress.Parse(info.ip),info.port));
await client.OnInit();
}
else
{
Sockets.Remove(index);
Debug.Log("获取连接失败");
}
}
protected void RemoveSocket(int index)
{
if (Sockets.TryGetValue(index,out var client))
{
client.OnClose();
}
Sockets.Remove(index);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 34430b78df32409ea2142a49f0e36683
timeCreated: 1722241862

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: adb805196eb84a23aa21c4c66c48a2ee
timeCreated: 1721384419

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2a1ee7e98b494ccfadfd9ee103715117
timeCreated: 1723691610

View File

@@ -0,0 +1,42 @@
using System.Net;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGClient : JNTCPClient
{
private IPEndPoint _point;
public void SetPoint(IPEndPoint point)
{
_point = point;
}
protected override async UniTask<IPEndPoint> GetEndPoint()
{
await UniTask.NextFrame();
return _point;
}
public override async Task OnInit()
{
//监听服务端事件
AddListener((int)NActionEnum.NSyncStateDataUpdate,OnNSyncStateDataUpdate);
//连接
await base.OnInit();
}
private void OnNSyncStateDataUpdate(byte[] data)
{
var info = JNStateItemData.Parser.ParseFrom(data);
App.Game.AddState(info);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de07e6b56137456fb6464195f9e1941d
timeCreated: 1722426114

View File

@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using Plugins.JNGame.Network.Group;
namespace AppGame.Systems
{
public class JNGClientGroup : JNClientGroup<JNGClient>
{
//玩家Id
private int clientId;
public int ClientID => clientId;
public override async Task OnInit()
{
clientId = (await App.GAPI.NSyncTileClientId).data;
await base.OnInit();
}
public override void AddClient(JNGClient client)
{
base.AddClient(client);
//向服务器发送玩家Id
client.Send((int)GActionEnum.BindClientID,new RClientIDMessage()
{
ClientId = clientId
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b74afb48ac594bbd8db4d8323f8c2b7f
timeCreated: 1723451509

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using JNGame.Network;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGServer : JNTCPServer
{
private int _index = 1;
private bool isInit = false;
//客户端绑定的Id
private Dictionary<int, int> ids = new();
public override async Task OnInit()
{
if (isInit) return;
isInit = true;
//监听服务端事件
AddListener((int)NActionEnum.NSyncFrameInput,OnNSyncFrameInput);
AddListener((int)NActionEnum.NSyncTileInput,OnNSyncTileInput);
AddListener((int)GActionEnum.BindClientID,OnBindClientID);
//连接
await base.OnInit();
}
public override void OnClose()
{
isInit = false;
base.OnClose();
}
protected override async UniTask<int> GetPort()
{
return (await App.GAPI.NSyncTilePort).data;
}
/// <summary>
/// 绑定客户端Id
/// </summary>
/// <param name="obj"></param>
/// <exception cref="NotImplementedException"></exception>
private void OnBindClientID(JNServerParam args)
{
var message = RClientIDMessage.Parser.ParseFrom(args.Message);
ids[args.Client] = message.ClientId;
}
/// <summary>
/// 接收帧数入
/// </summary>
/// <param name="args"></param>
private void OnNSyncFrameInput(JNServerParam args)
{
var inputs = JNFrameInputs.Parser.ParseFrom(args.Message);
var frame = new JNFrameInfo();
frame.Index = 0;
foreach (var input in inputs.Inputs)
{
frame.Messages.Add(input);
}
App.Game.AddInput(frame);
}
/// <summary>
/// 接收瓦片输入
/// </summary>
/// <param name="args"></param>
private void OnNSyncTileInput(JNServerParam args)
{
var inputs = JNStateTileInputs.Parser.ParseFrom(args.Message);
//只有绑定过ID 的客户端才可以执行操作
inputs.Message.Inputs.ForEach(child =>
{
child.ClientId = args.Client;
});
App.Game.AddTileInput(inputs);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4d29d51922a6446490e06c87636d2590
timeCreated: 1722426103

View File

@@ -0,0 +1,43 @@
using System.Net;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGTileClient : JNTCPClient
{
private IPEndPoint _point;
public void SetPoint(IPEndPoint point)
{
_point = point;
}
protected override async UniTask<IPEndPoint> GetEndPoint()
{
await UniTask.NextFrame();
return _point;
}
public override async Task OnInit()
{
//监听服务端事件
AddListener((int)NActionEnum.NSyncStateDataUpdate,OnNSyncStateDataUpdate);
//连接
await base.OnInit();
}
private void OnNSyncStateDataUpdate(byte[] data)
{
var info = JNStateItemData.Parser.ParseFrom(data);
App.Game.AddState(info);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 892255e4a0a4489784ba78fef9492936
timeCreated: 1723691588

View File

@@ -0,0 +1,135 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AppGame.Sync;
using JNGame.Sync.Frame;
using JNGame.Sync.State;
using JNGame.Sync.System.Data;
using JNGame.Sync.System.View;
using Plugins.JNGame.System;
namespace AppGame.Systems
{
public class JNGGame : SystemBase
{
/// <summary>
/// 同步组
/// </summary>
private List<JNSyncDefaultService> syncs = new();
private JNSyncDefaultService client;
private List<JNSStateServerService> servers = new();
public bool IsStartGame => syncs.Count > 0 && syncs[0].IsStartGame;
public override async Task OnInit()
{
}
/// <summary>
/// 运行一个同步类
/// </summary>
public T Start<T>() where T : JNSyncDefaultService,new()
{
syncs.Add(new T());
syncs[^1].Initialize();
if (syncs[^1] is JNSStateServerService server)
{
servers.Add(server);
}
else
{
client = syncs[^1];
}
return syncs[^1] as T;
}
/// <summary>
/// 更新周期
/// </summary>
public void Update()
{
foreach (var sync in syncs)
{
sync.Execute();
}
}
/// <summary>
/// 获取第一个客户端的输入类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetInput<T>() where T : JNInputBase, new()
{
return GetClient().GetSystem<JNInputSystem>().Input<T>();
}
/// <summary>
/// 获取第一个客户端
/// </summary>
/// <returns></returns>
public JNSyncDefaultService GetClient()
{
return client;
}
public T GetClient<T>() where T : JNSyncDefaultService
{
return (T)client;
}
/// <summary>
/// 接收输入数据
/// </summary>
/// <param name="frame"></param>
public void AddInput(JNFrameInfo frame)
{
foreach (var sync in syncs)
{
(sync as JNGFrameSystem)?.AddFrame(frame);
(sync as JNGStateServerSystem)?.AddInput(frame);
}
}
public void AddTileInput(JNStateTileInputs frame)
{
foreach (var sync in syncs)
{
if (sync is JNGTileServerSystem system)
{
if (system.TID == frame.TId || frame.TId == 0)
{
system.AddInput(frame);
}
}
}
}
/// <summary>
/// 接收状态数据
/// </summary>
/// <param name="frame"></param>
public void AddState(JNStateItemData frame)
{
var message = new Dictionary<long, byte[]>();
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
foreach (var sync in syncs)
{
sync.GetSystems<ISStateDataSystem>().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bf6b9c1fec19438fa9b636bc2e7e4df1
timeCreated: 1722495304

View File

@@ -0,0 +1,12 @@

using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
public class JNGSocket : JNSocket
{
protected override async UniTask<string> GetUrl()
{
await UniTask.NextFrame();
return "ws://127.0.0.1:8080/websocket";
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa09b61b047f4c98bd0b5e3dc2fad6aa
timeCreated: 1721384427