mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交Unity 联机Pro
This commit is contained in:
199
JNFrame2/Assets/Scripts/AppGame/Sync/Tile/JNGTileClientSystem.cs
Normal file
199
JNFrame2/Assets/Scripts/AppGame/Sync/Tile/JNGTileClientSystem.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a158f1d590a642988611d229ddaa6a1c
|
||||
timeCreated: 1722493289
|
225
JNFrame2/Assets/Scripts/AppGame/Sync/Tile/JNGTileServerSystem.cs
Normal file
225
JNFrame2/Assets/Scripts/AppGame/Sync/Tile/JNGTileServerSystem.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34430b78df32409ea2142a49f0e36683
|
||||
timeCreated: 1722241862
|
Reference in New Issue
Block a user