JisolGame/JNFrame2/Assets/Scripts/Game/Data/GDataBaseSystem.cs

132 lines
3.5 KiB
C#
Raw Normal View History

2024-08-22 20:37:39 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using AppGame;
using Google.Protobuf;
using JNGame.Math;
using JNGame.Sync.Entity;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Newtonsoft.Json;
using Plugins.JNGame.Network.Action;
namespace Game.JNGState.Logic.Data
{
[Serializable]
public class GDataValue
{
public DValuePosition Position = null;
}
public class GDataBase<Self,T,N> : ISTileData where Self : GDataBase<Self,T,N> where T : GDataValue,new() where N : JNTileEntity
{
public readonly T Value = new ();
public N Node => Entity as N;
public override bool IsHost => Node is not null && Node.IsHost;
public GDataBase()
{
}
public GDataBase(N node)
{
Id = node.Id;
Value.Position = new DValuePosition()
{
x = node.Position.x.rawValue,
y = node.Position.y.rawValue,
z = node.Position.z.rawValue,
};
BindEntity(node);
}
public override bool IsEquals(ISData data)
{
var node = data as Self;
if (node is null) return false;
return Value.Position.Equals(node.Value.Position);
}
public sealed override byte[] GetByte()
{
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Value));
}
public sealed override byte[] GetByteDifference(ISData diffValue = null)
{
var diff = GetDifference(diffValue);
if (diff is not null)
{
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(diff));
}
else
{
return Array.Empty<byte>();
}
}
public virtual T GetDifference(ISData diffValue = null)
{
var diff = diffValue as Self;
if (diff is null || IsEquals(diffValue)) return null;
var value = new T();
if (diff.Value.Position is not null) value.Position = diff.Value.Position;
return value;
}
public sealed override void UByte(byte[] bytes)
{
if (bytes.Length == 0) return;
var value = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes));
UData(value);
}
/// <summary>
/// 更新数据
/// </summary>
public virtual void UData(T data)
{
if (data.Position is not null) Value.Position = data.Position;
}
public override LVector3 GetDataPosition()
{
return Value.Position.ToLVector3();
}
}
public abstract class GDataBaseSystem<T,E> : STileDataSystem<T,E> where T : ISTileData,new() where E : JNTileEntity, new()
{
protected GDataBaseSystem(SStateDataEnum type) : base(type)
{
}
2024-08-23 10:48:19 +08:00
public override void OnSendUBytes(Dictionary<ulong, byte[]> bytes)
2024-08-22 20:37:39 +08:00
{
JNStateItemData data = new JNStateItemData();
data.NetID = NetID;
foreach (var byteItem in bytes)
{
data.Messages.Add(byteItem.Key,new JNStateData()
{
Data = ByteString.CopyFrom(byteItem.Value)
});
}
App.Server.AllSend((int)NActionEnum.NSyncStateDataUpdate,data);
}
}
}