136 lines
3.9 KiB
C#
Raw Normal View History

2024-08-17 14:27:18 +08:00
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
{
private JNSyncDefaultService client;
2024-08-22 20:37:39 +08:00
public JNSyncDefaultService Client => client;
private JNSStateServerService server;
public JNSStateServerService Server => server;
2024-08-17 14:27:18 +08:00
2024-08-22 20:37:39 +08:00
public bool IsStartClient => client is not null && client.IsStartGame;
public bool IsStartServer => server is not null && server.IsStartGame;
2024-08-17 14:27:18 +08:00
public override async Task OnInit()
{
}
/// <summary>
2024-08-22 20:37:39 +08:00
/// 运行同步类
2024-08-17 14:27:18 +08:00
/// </summary>
2024-08-22 20:37:39 +08:00
public T StartClient<T>() where T : JNSyncDefaultService,new()
2024-08-17 14:27:18 +08:00
{
2024-08-22 20:37:39 +08:00
client = new T();
client.Initialize();
return client as T;
}
public T StartServer<T>() where T : JNSStateServerService,new()
{
server = new T();
server.Initialize();
return server as T;
2024-08-17 14:27:18 +08:00
}
/// <summary>
/// 更新周期
/// </summary>
public void Update()
{
2024-08-22 20:37:39 +08:00
client?.Execute();
server?.Execute();
2024-08-17 14:27:18 +08:00
}
/// <summary>
/// 获取第一个客户端的输入类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetInput<T>() where T : JNInputBase, new()
{
2024-08-31 15:35:12 +08:00
return Client.GetSystem<JNInputSystem>().Input<T>();
2024-08-17 14:27:18 +08:00
}
public T GetClient<T>() where T : JNSyncDefaultService
{
2024-08-31 15:35:12 +08:00
if (!IsStartClient) return null;
return client as T;
2024-08-17 14:27:18 +08:00
}
/// <summary>
/// 接收输入数据
/// </summary>
/// <param name="frame"></param>
public void AddInput(JNFrameInfo frame)
{
2024-08-22 20:37:39 +08:00
(client as JNGFrameSystem)?.AddFrame(frame);
(server as JNGStateServerSystem)?.AddInput(frame);
2024-08-17 14:27:18 +08:00
}
public void AddTileInput(JNStateTileInputs frame)
{
2024-08-31 15:35:12 +08:00
if (server is JNGTileServerSystem system1)
{
if (system1.TID == frame.TId || frame.TId == 0)
{
system1.AddInput(frame);
}
}
if (server is JNGTileSlaveServerSystem system2)
2024-08-17 14:27:18 +08:00
{
2024-08-31 15:35:12 +08:00
if (system2.TID == frame.TId || frame.TId == 0)
2024-08-17 14:27:18 +08:00
{
2024-08-31 15:35:12 +08:00
system2.AddInput(frame);
2024-08-17 14:27:18 +08:00
}
}
}
2024-08-22 20:37:39 +08:00
/// <summary>
/// 清除指定区域的状态
/// </summary>
public void ClearTileState(int index)
{
client?.GetSystems<ISTileDataSystem>().ForEach(child =>
{
child.ClearTileData(index);
});
server?.GetSystems<ISTileDataSystem>().ForEach(child =>
{
child.ClearTileData(index);
});
}
2024-08-17 14:27:18 +08:00
/// <summary>
/// 接收状态数据
/// </summary>
public void AddState(JNStateItemData frame)
{
2024-08-23 10:48:19 +08:00
var message = new Dictionary<ulong, byte[]>();
2024-08-17 14:27:18 +08:00
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
2024-08-22 20:37:39 +08:00
client?.GetSystems<ISStateDataSystem>().ForEach(child =>
2024-08-17 14:27:18 +08:00
{
2024-08-22 20:37:39 +08:00
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
server?.GetSystems<ISStateDataSystem>().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
2024-08-17 14:27:18 +08:00
}
}
}