using System.Collections.Generic; using System.Threading.Tasks; using AppGame.Sync; using JNGame.Runtime.Sync; using JNGame.Runtime.System; using JNGame.Sync.Frame; using JNGame.Sync.State; using JNGame.Sync.System.Data; using JNGame.Sync.System.View; namespace AppGame.Systems { public class JNGGame : SystemBase { private JNSyncDefaultService client; public JNSyncDefaultService Client => client; private JNSStateServerService server; public JNSStateServerService Server => server; public bool IsStartClient => client is not null && client.IsStartGame; public bool IsStartServer => server is not null && server.IsStartGame; public override async Task OnInit() { } public override void OnClose() { base.OnClose(); client?.Dispose(); server?.Dispose(); } /// /// 运行同步类 /// public T StartClient() where T : JNSyncDefaultService,new() { client = new T(); client.Initialize(); client.TStartExecute(); return client as T; } public T StartServer() where T : JNSStateServerService,new() { server = new T(); server.Initialize(); server.TStartExecute(); return server as T; } /// /// 获取第一个客户端的输入类 /// /// /// public T GetInput() where T : JNInputBase, new() { return Client.GetSystem().Input(); } public T GetClient() where T : JNSyncDefaultService { if (!IsStartClient) return null; return client as T; } /// /// 接收输入数据 /// /// public void AddInput(JNFrameInfo frame) { (client as JNGFrameSystem)?.AddFrame(frame); (server as JNGStateServerSystem)?.AddInput(frame); } public void AddTileInput(JNStateTileInputs frame) { if (server is JNGTileServerSystem system1) { if (system1.TID == frame.TId || frame.TId == 0) { system1.AddInput(frame); } } if (server is JNGTileSlaveServerSystem system2) { if (system2.TID == frame.TId || frame.TId == 0) { system2.AddInput(frame); } } } /// /// 清除指定区域的状态 /// public void ClearTileState(int index) { client?.GetSystems().ForEach(child => { child.ClearTileData(index); }); server?.GetSystems().ForEach(child => { child.ClearTileData(index); }); } /// /// 接收状态数据 /// public void SyncState(JNStateItemData frame,bool isSyncClient,bool isSyncServer) { var message = new Dictionary(); foreach (var data in frame.Messages) { message.Add(data.Key,data.Value.Data.ToByteArray()); } if (isSyncClient) { client?.GetSystems().ForEach(child => { if (child.NetID != frame.NetID) return; child.OnInsertUBytes(message); }); } if (isSyncServer) { server?.GetSystems().ForEach(child => { if (child.NetID != frame.NetID) return; child.OnInsertUBytes(message); }); } } } }