mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-11-11 08:38:45 +00:00
提交Unity 联机Pro
This commit is contained in:
187
JNFrame2/Assets/JNGame/Sync/System/Logic/JNInputSystem.cs
Normal file
187
JNFrame2/Assets/JNGame/Sync/System/Logic/JNInputSystem.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Google.Protobuf;
|
||||
using JNGame.Util.Types;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace JNGame.Sync.System.View
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Json的输入类
|
||||
/// </summary>
|
||||
public class JNInputJson : JNInputBase
|
||||
{
|
||||
public byte[] Encoder()
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this));
|
||||
}
|
||||
|
||||
public virtual object Decoder(byte[] bytes)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bytes),this.GetType());
|
||||
}
|
||||
}
|
||||
|
||||
public interface JNInputBase
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 编码
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public byte[] Encoder();
|
||||
|
||||
/// <summary>
|
||||
/// 解码
|
||||
/// </summary>
|
||||
public object Decoder(byte[] bytes);
|
||||
|
||||
}
|
||||
|
||||
public abstract class JNInputSystem : SLogicSystem
|
||||
{
|
||||
|
||||
protected abstract KeyValue<Type, int> TClass { get; }
|
||||
protected Dictionary<Type, JNInputBase> TNewClass = new ();
|
||||
protected readonly Dictionary<int, JNInputBase> Inputs = new ();
|
||||
protected readonly Dictionary<int, Dictionary<int,JNInputBase>> SInputs = new ();
|
||||
|
||||
protected Dictionary<int,JNFrameInput> frame = new();
|
||||
|
||||
/// <summary>
|
||||
/// 移入数据
|
||||
/// </summary>
|
||||
public void Enqueue(JNFrameInput input)
|
||||
{
|
||||
frame[input.NId] = input;
|
||||
}
|
||||
|
||||
protected JNInputSystem()
|
||||
{
|
||||
OnInit();
|
||||
OnApply();
|
||||
}
|
||||
|
||||
protected virtual void OnInit(){}
|
||||
|
||||
/// <summary>
|
||||
/// 应用
|
||||
/// </summary>
|
||||
protected virtual void OnApply()
|
||||
{
|
||||
TNewClass.Clear();
|
||||
foreach (var key in TClass.Keys)
|
||||
{
|
||||
TNewClass.Add(key,Activator.CreateInstance(key) as JNInputBase);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnSyncUpdate()
|
||||
{
|
||||
base.OnSyncUpdate();
|
||||
UpdateSInputs();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取同步输入(逻辑中获取 不可修改)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public JNInputBase SInput<T>(int clientId) where T : JNInputBase, new()
|
||||
{
|
||||
var key = TClass.Key2Value(typeof(T));
|
||||
if (SInputs.TryGetValue(key, out var inputs))
|
||||
{
|
||||
inputs.TryGetValue(clientId,out var input);
|
||||
return input;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Dictionary<int,JNInputBase> SInput<T>() where T : JNInputBase, new()
|
||||
{
|
||||
var key = TClass.Key2Value(typeof(T));
|
||||
if (SInputs.TryGetValue(key, out var inputs))
|
||||
{
|
||||
return inputs;
|
||||
}
|
||||
return new();
|
||||
}
|
||||
public JNInputBase SInputOne<T>() where T : JNInputBase, new()
|
||||
{
|
||||
var key = TClass.Key2Value(typeof(T));
|
||||
if (SInputs.TryGetValue(key, out var inputs))
|
||||
{
|
||||
if (inputs.Count > 0)
|
||||
{
|
||||
return inputs.Values.First();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取输入 (禁止在逻辑中获取 只允许在UI层调用)
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Input<T>() where T : JNInputBase,new()
|
||||
{
|
||||
if (!(Inputs.TryGetValue(TClass.Key2Value(typeof(T)),out var input)))
|
||||
{
|
||||
Inputs[TClass.Key2Value(typeof(T))] = input = new T();
|
||||
}
|
||||
return (T)input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移出输入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public JNFrameInputs Dequeue()
|
||||
{
|
||||
JNFrameInputs inputs = new JNFrameInputs();
|
||||
foreach (var key in Inputs.Keys)
|
||||
{
|
||||
var input = new JNFrameInput();
|
||||
var info = Inputs[key];
|
||||
input.NId = key;
|
||||
input.Input = ByteString.CopyFrom(info.Encoder());
|
||||
inputs.Inputs.Add(input);
|
||||
}
|
||||
Inputs.Clear();
|
||||
return inputs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移入输入
|
||||
/// </summary>
|
||||
public void UpdateSInputs()
|
||||
{
|
||||
|
||||
SInputs.Clear();
|
||||
//解析输入
|
||||
foreach (var kInput in frame)
|
||||
{
|
||||
var input = kInput.Value;
|
||||
var tClass = TClass.Value2Key(input.NId);
|
||||
|
||||
if (!(SInputs.TryGetValue(input.NId,out var inputs)))
|
||||
{
|
||||
SInputs.Add(input.NId, inputs = new Dictionary<int, JNInputBase>());
|
||||
}
|
||||
|
||||
inputs.Add(input.ClientId,TNewClass[tClass].Decoder(input.Input.ToByteArray()) as JNInputBase);
|
||||
|
||||
}
|
||||
frame.Clear();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6c14ce606094eecbc415faab78bb5ad
|
||||
timeCreated: 1721635056
|
||||
56
JNFrame2/Assets/JNGame/Sync/System/Logic/JNRandomSystem.cs
Normal file
56
JNFrame2/Assets/JNGame/Sync/System/Logic/JNRandomSystem.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using JNGame.Math;
|
||||
using JNGame.Sync.System;
|
||||
using Plugins.JNGame.Util;
|
||||
|
||||
namespace JNGame.Sync.Frame.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// 随机数
|
||||
/// </summary>
|
||||
public class JNRandomSystem : SLogicSystem
|
||||
{
|
||||
|
||||
//随机数
|
||||
private Func<LFloat,LFloat,LFloat> nRandomFloat;
|
||||
private Func<int,int,int> nRandomInt;
|
||||
|
||||
//Id
|
||||
private long _id = 0;
|
||||
|
||||
public JNRandomSystem(int seed)
|
||||
{
|
||||
nRandomFloat = RandomUtil.SyncRandomFloat(seed);
|
||||
nRandomInt = RandomUtil.SyncRandomInt(seed);
|
||||
}
|
||||
|
||||
public LFloat Float()
|
||||
{
|
||||
return Float(0,1);
|
||||
}
|
||||
|
||||
public LFloat Float(LFloat min,LFloat max)
|
||||
{
|
||||
return nRandomFloat(min,max);
|
||||
}
|
||||
|
||||
public int Int(int max,int min)
|
||||
{
|
||||
return nRandomInt(max,min);
|
||||
}
|
||||
|
||||
public long NextId()
|
||||
{
|
||||
return ++_id;
|
||||
}
|
||||
|
||||
public void SetIdValue(long id)
|
||||
{
|
||||
if (_id < id)
|
||||
{
|
||||
_id = id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30a8e21f1e344fb59f39047129fce186
|
||||
timeCreated: 1721187856
|
||||
Reference in New Issue
Block a user