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;
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 T StartClient() where T : JNSyncDefaultService,new()
{
client = new T();
client.Initialize();
return client as T;
}
public T StartServer() where T : JNSStateServerService,new()
{
server = new T();
server.Initialize();
return server as T;
}
///
/// 更新周期
///
public void Update()
{
client?.Execute();
server?.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)
{
(client as JNGFrameSystem)?.AddFrame(frame);
(server as JNGStateServerSystem)?.AddInput(frame);
}
public void AddTileInput(JNStateTileInputs frame)
{
if (server is JNGTileServerSystem system)
{
if (system.TID == frame.TId || frame.TId == 0)
{
system.AddInput(frame);
}
}
}
///
/// 清除指定区域的状态
///
public void ClearTileState(int index)
{
client?.GetSystems().ForEach(child =>
{
child.ClearTileData(index);
});
server?.GetSystems().ForEach(child =>
{
child.ClearTileData(index);
});
}
///
/// 接收状态数据
///
public void AddState(JNStateItemData frame)
{
var message = new Dictionary();
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
client?.GetSystems().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
server?.GetSystems().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
}
}
}