using System; using System.Collections.Generic; using System.Text; using DotRecast.Core.Collections; using Entitas; using Game.JNGFrame.Logic.Entity; using Game.JNGFrame.Logic.Entity.Contexts; using JNGame.Math; using JNGame.Sync.State.Tile.Entity; using JNGame.Sync.System; using JNGame.Sync.System.Data; using Newtonsoft.Json; using UnityEngine; namespace Game.JNGState.Logic.Data { [Serializable] public class EDPlayerValue { public int? Auth = null; public DValuePosition Position = null; } public class EDPlayerData : ISTileData { public readonly EDPlayerValue Value = new (); private readonly EDPlayer _data; public override bool IsHost => _data is not null && _data.IsHost; public EDPlayerData() { } public EDPlayerData(EDPlayer node){ Id = node.Id; Value.Auth = node.Controller.Auth; Value.Position = new DValuePosition() { x = node.Position.x.rawValue, y = node.Position.y.rawValue, z = node.Position.z.rawValue, }; _data = node; } public override bool IsEquals(ISData data) { var node = data as EDPlayerData; if (node is null) return false; return Value.Position.Equals(node.Value.Position) && Value.Auth.Equals(node.Value.Auth); } public override byte[] GetByte() { return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Value)); } public override byte[] GetByteDifference(ISData diffValue = null) { var diff = diffValue as EDPlayerData; if (diff is null || IsEquals(diffValue)) return Array.Empty(); var value = new EDPlayerValue(); if (diff.Value.Position is not null) value.Position = diff.Value.Position; if (diff.Value.Auth is not null) value.Auth = diff.Value.Auth; return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)); } /// /// 生效字节 /// /// /// public override void UByte(byte[] bytes) { if (bytes.Length == 0) return; var value = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bytes)); if (value.Position is not null) Value.Position = value.Position; if (value.Auth is not null) Value.Auth = value.Auth; } } public class EDPlayerDataSystem : DStateDataSystem { public EDPlayerDataSystem(SStateDataEnum type) : base(type) { } public override int NetID => (int)NetDataEnum.EDPlayerData; public override JNTileContext NodeContext => Contexts.GetContext(); public override Dictionary GetLatest() { var nodes = new Dictionary(); NodeContext.GetEntities().ForEach(child => { nodes.Add(child.Id,new EDPlayerData(child)); }); return nodes; } } }