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 List syncs = new();
private JNSyncDefaultService client;
private List servers = new();
public bool IsStartGame => syncs.Count > 0 && syncs[0].IsStartGame;
public override async Task OnInit()
{
}
///
/// 运行一个同步类
///
public T Start() 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;
}
///
/// 更新周期
///
public void Update()
{
foreach (var sync in syncs)
{
sync.Execute();
}
}
///
/// 获取第一个客户端的输入类
///
///
///
public T GetInput() where T : JNInputBase, new()
{
return GetClient().GetSystem().Input();
}
///
/// 获取第一个客户端
///
///
public JNSyncDefaultService GetClient()
{
return client;
}
public T GetClient() where T : JNSyncDefaultService
{
return (T)client;
}
///
/// 接收输入数据
///
///
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);
}
}
}
}
///
/// 接收状态数据
///
///
public void AddState(JNStateItemData frame)
{
var message = new Dictionary();
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
foreach (var sync in syncs)
{
sync.GetSystems().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
}
}
}
}