mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交Unity 联机Pro
This commit is contained in:
3
JNFrame2/Assets/Scripts/AppGame/Systems/CServer.meta
Normal file
3
JNFrame2/Assets/Scripts/AppGame/Systems/CServer.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a1ee7e98b494ccfadfd9ee103715117
|
||||
timeCreated: 1723691610
|
42
JNFrame2/Assets/Scripts/AppGame/Systems/CServer/JNGClient.cs
Normal file
42
JNFrame2/Assets/Scripts/AppGame/Systems/CServer/JNGClient.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de07e6b56137456fb6464195f9e1941d
|
||||
timeCreated: 1722426114
|
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b74afb48ac594bbd8db4d8323f8c2b7f
|
||||
timeCreated: 1723451509
|
91
JNFrame2/Assets/Scripts/AppGame/Systems/CServer/JNGServer.cs
Normal file
91
JNFrame2/Assets/Scripts/AppGame/Systems/CServer/JNGServer.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d29d51922a6446490e06c87636d2590
|
||||
timeCreated: 1722426103
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 892255e4a0a4489784ba78fef9492936
|
||||
timeCreated: 1723691588
|
135
JNFrame2/Assets/Scripts/AppGame/Systems/JNGGame.cs
Normal file
135
JNFrame2/Assets/Scripts/AppGame/Systems/JNGGame.cs
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
3
JNFrame2/Assets/Scripts/AppGame/Systems/JNGGame.cs.meta
Normal file
3
JNFrame2/Assets/Scripts/AppGame/Systems/JNGGame.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf6b9c1fec19438fa9b636bc2e7e4df1
|
||||
timeCreated: 1722495304
|
12
JNFrame2/Assets/Scripts/AppGame/Systems/JNGSocket.cs
Normal file
12
JNFrame2/Assets/Scripts/AppGame/Systems/JNGSocket.cs
Normal 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";
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa09b61b047f4c98bd0b5e3dc2fad6aa
|
||||
timeCreated: 1721384427
|
Reference in New Issue
Block a user