mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 19:34:47 +00:00
135 lines
3.5 KiB
C#
135 lines
3.5 KiB
C#
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);
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
} |