// using System; // using System.Collections.Generic; // using System.Text; // using cfg; // using UnityEngine; // // namespace NBF // { // /// // /// 钓组类型 // /// // public enum GearType // { // SpinningFloat = 0, // Spinning = 1, // Hand = 2, // Feeder = 3, // Pole = 4 // } // // /// // /// 装备物品类型枚举(用于快速判断) // /// // public enum GearItemType // { // None = 0, // Rod = 1, // Reel = 2, // Bobber = 3, // Hook = 4, // Bait = 5, // Lure = 6, // Weight = 7, // Line = 8, // Leader = 9, // Feeder = 10 // } // // [Serializable] public enum PlayerState : uint { None = 0, /// /// 闲置等待中 /// Idle = 1, /// /// 准备抛竿 /// Prepare = 2, /// /// 抛竿中 /// Throw = 3, /// /// 钓鱼中 /// Fishing = 4, /// /// 溜鱼中 /// Fight = 5 } // // public enum HeldItemType // { // None = 0, // Gear = 1, // 钓组 // Tool = 2, // 工具(铲子等) // Consumable = 3, // 消耗品 // Special = 4 // 特殊物品 // } // // /// // /// 手持物品信息 // /// // [Serializable] // public class HeldItemInfo // { // public HeldItemType ItemType; // 物品类型 // public int ConfigID; // 配置 ID(钓组或普通物品) // // // 如果是钓组,包含完整钓组数据 // public FPlayerGearData GearData; // } // // // [Serializable] // public class FPlayerData // { // // ========== 基础标识 ========== // public int PlayerID; // public bool IsLocalPlayer; // 是否为本地玩家 // public bool IsServer; // 是否由服务器控制 // // // ========== 持久化数据(低频同步) ========== // /// // /// 所有可用钓组 // /// // public List AllGears = new(); // // // ========== 当前状态(中高频同步) ========== // /// // /// 当前手持的物品 // /// // public HeldItemInfo CurrentHeldItem = new(); // // /// // /// 当前装备的钓组(从 CurrentHeldItem 派生,方便访问) // /// // public FPlayerGearData CurrentGear => // CurrentHeldItem.ItemType == HeldItemType.Gear ? CurrentHeldItem.GearData : null; // // // ========== 物理状态(高频同步) ========== // public Vector3 position; // public Quaternion rotation; // public Vector2 MoveInput; // public float Speed; // public float RotationSpeed; // public bool IsGrounded; // public bool Run; // // // ========== 钓鱼相关状态(中频同步) ========== // public float currentReelingSpeed; // public bool isHandOnHandle; // public float lineLength; // public float reelSpeed; // public float EyeAngle; // public bool IsLureRod; // // // ========== 动作和道具状态(低频同步) ========== // public bool openLight; // public bool openTelescope; // public bool ChangeItem; // // // ========== 状态机 ========== // private PlayerState _previousPlayerState = PlayerState.Idle; // private PlayerState _playerState; // public PlayerState PreviousState => _previousPlayerState; // // /// // /// 当前状态的进入参数(本地和远程都适用) // /// // public StateEnterParams CurrentStateParams { get; private set; } = new StateEnterParams(); // // public PlayerState State // { // get => _playerState; // set // { // if (_playerState != value) // { // _previousPlayerState = _playerState; // _playerState = value; // NextState = value; // // // 先触发事件(带参数) // OnStateChange?.Invoke(_playerState, CurrentStateParams); // // // 触发网络同步(如果有) // if (PlayerDataManager.Instance != null) // { // PlayerDataManager.Instance.OnPlayerStateChanged(this, value); // } // } // } // } // // /// // /// 设置状态并传入参数 // /// // public void SetState(PlayerState newState, StateEnterParams enterParams = null) // { // // 复制参数到当前状态 // if (enterParams != null) // { // CurrentStateParams = enterParams.Clone(); // } // else // { // CurrentStateParams.Clear(); // } // // State = newState; // } // // [SerializeField] private PlayerState NextState; // public event Action OnStateChange; // // // // ========== 网络同步相关 ========== // /// // /// 最后接收到的网络快照时间 // /// // public float LastNetworkSyncTime { get; set; } // // /// // /// 是否需要网络插值 // /// // public bool NeedInterpolation => !IsLocalPlayer && !IsServer; // // // ========== 脏标记系统(关键优化) ========== // private SyncFlags _dirtyFlags = SyncFlags.None; // // /// // /// 标记某个数据块已变化,需要同步 // /// // public void MarkDirty(SyncFlags flag) // { // _dirtyFlags |= flag; // } // // /// // /// 检查是否有数据需要同步 // /// // public bool HasDirtyData => _dirtyFlags != SyncFlags.None; // // /// // /// 获取并清除脏标记 // /// // public SyncFlags GetAndClearDirtyFlags() // { // var flags = _dirtyFlags; // _dirtyFlags = SyncFlags.None; // return flags; // } // // /// // /// 清除所有脏标记 // /// // public void ClearDirtyFlags() // { // _dirtyFlags = SyncFlags.None; // } // // // // ========== 生命周期 ========== // private void Start() // { // NextState = State; // InitializeDefaultItems(); // } // // private void Update() // { // if (NextState != State) // { // State = NextState; // } // } // // // ========== 物品系统方法 ========== // // /// // /// 切换手持物品 // /// // public void SwitchHeldItem(HeldItemInfo newItem) // { // CurrentHeldItem = newItem; // ChangeItem = true; // // // 如果切换到钓组,更新 CurrentGear 引用 // if (newItem.ItemType == HeldItemType.Gear) // { // // 确保钓组数据有效 // if (newItem.GearData == null) // { // newItem.GearData = GetGearByConfigID(newItem.ConfigID); // } // } // // // 通知网络层 // if (PlayerDataManager.Instance != null) // { // PlayerDataManager.Instance.OnHeldItemChanged(this, newItem); // } // } // // /// // /// 装备钓组 // /// // public void EquipGear(int gearIndex) // { // if (gearIndex < 0 || gearIndex >= AllGears.Count) return; // // var gear = AllGears[gearIndex]; // var heldItem = new HeldItemInfo // { // ItemType = HeldItemType.Gear, // ConfigID = gear.GetUnitId(), // GearData = gear // }; // // SwitchHeldItem(heldItem); // } // // /// // /// 根据配置 ID 获取钓组 // /// // public FPlayerGearData GetGearByConfigID(int configID) // { // return AllGears.Find(g => g.GetUnitId() == configID); // } // // /// // /// 初始化默认物品(示例) // /// // private void InitializeDefaultItems() // { // // 这里可以加载默认钓组和物品 // // 实际逻辑应该从存档或服务器加载 // } // // // ========== 网络快照转换 ========== // // /// // /// 转换为网络快照 // /// // public PlayerStateSnapshot ToNetworkSnapshot(uint sequenceNumber) // { // var snapshot = new PlayerStateSnapshot // { // PlayerID = PlayerID, // SequenceNumber = sequenceNumber, // Timestamp = Time.time, // // Position = position, // Rotation = rotation, // MoveInput = MoveInput, // Speed = Speed, // IsGrounded = IsGrounded, // IsRunning = Run, // // State = State, // LineLength = lineLength, // ReelSpeed = reelSpeed, // EyeAngle = EyeAngle, // IsHandOnHandle = isHandOnHandle, // // HeldItem = CurrentHeldItem, // // // 同步状态参数 // StateParams = CurrentStateParams, // // FloatPosition = null, // 根据需要设置 // HookPosition = null, // CaughtFishID = 0 // }; // // // 设置标志位 // snapshot.SetFlag(0, openLight); // snapshot.SetFlag(1, openTelescope); // snapshot.SetFlag(2, ChangeItem); // snapshot.SetFlag(3, IsLureRod); // // return snapshot; // } // // /// // /// 从网络快照应用状态 // /// // public void ApplyFromNetworkSnapshot(PlayerStateSnapshot snapshot) // { // if (snapshot.PlayerID != PlayerID) return; // // LastNetworkSyncTime = (float)snapshot.Timestamp; // // // 保存旧状态用于对比 // var oldState = State; // var stateChanged = snapshot.State != oldState; // // position = snapshot.Position; // rotation = snapshot.Rotation; // MoveInput = snapshot.MoveInput; // Speed = snapshot.Speed; // IsGrounded = snapshot.IsGrounded; // Run = snapshot.IsRunning; // // // 如果状态改变,先更新状态和参数 // if (stateChanged) // { // _previousPlayerState = oldState; // _playerState = snapshot.State; // NextState = snapshot.State; // // // 应用状态参数 // if (snapshot.StateParams != null) // { // CurrentStateParams = snapshot.StateParams.Clone(); // } // // // 触发事件(带参数) // OnStateChange?.Invoke(_playerState, CurrentStateParams); // } // // lineLength = snapshot.LineLength; // reelSpeed = snapshot.ReelSpeed; // EyeAngle = snapshot.EyeAngle; // isHandOnHandle = snapshot.IsHandOnHandle; // // CurrentHeldItem = snapshot.HeldItem; // // // 读取标志位 // openLight = snapshot.GetFlag(0); // openTelescope = snapshot.GetFlag(1); // ChangeItem = snapshot.GetFlag(2); // IsLureRod = snapshot.GetFlag(3); // } // } // // /// // /// 同步标志位枚举 // /// // [Flags] // public enum SyncFlags : byte // { // None = 0, // Physics = 1 << 0, // 位置、旋转、移动 // Fishing = 1 << 1, // 钓鱼相关状态 // HeldItem = 1 << 2, // 手持物品 // Flags = 1 << 3, // 布尔标志 // Extension = 1 << 4, // 扩展数据 // All = Physics | Fishing | HeldItem | Flags | Extension // } // // /// // /// 网络同步用的玩家状态快照(精简、可序列化) // /// // [Serializable] // public class PlayerStateSnapshot // { // // ========== 元数据 ========== // public int PlayerID; // public uint SequenceNumber; // 序列号,处理乱序包 // public double Timestamp; // 时间戳,用于插值 // // // ========== 同步标志位(关键优化) ========== // /// // /// 指示哪些数据块需要同步(位掩码) // /// bit0: 基础物理状态 | bit1: 钓鱼状态 | bit2: 手持物品 | bit3: 布尔标志 | bit4: 扩展数据 // /// // public SyncFlags SyncFlags = SyncFlags.All; // // // ========== 基础状态(高频同步 - 可选) ========== // public Vector3 Position; // public Quaternion Rotation; // public Vector2 MoveInput; // public float Speed; // public bool IsGrounded; // public bool IsRunning; // // // ========== 钓鱼状态(中频同步 - 可选) ========== // public PlayerState State; // public float LineLength; // public float ReelSpeed; // public float EyeAngle; // public bool IsHandOnHandle; // // // ========== 手持物品信息(低频同步 - 可选) ========== // public HeldItemInfo HeldItem; // // // ========== 状态进入参数(状态变化时同步 - 可选) ========== // public StateEnterParams StateParams; // // // ========== 布尔标志位(用 byte 压缩 - 可选) ========== // public byte Flags; // bit0:开灯 bit1:望远镜 bit2:换物品 bit3:路亚竿 // // // ========== 扩展数据(按需同步 - 可选) ========== // public Vector3? FloatPosition; // 浮漂位置(nullable) // public Vector3? HookPosition; // 鱼钩位置 // public uint CaughtFishID; // 钓到的鱼 ID // // // ========== 辅助方法 ========== // // public bool GetFlag(int index) => (Flags & (1 << index)) != 0; // // public void SetFlag(int index, bool value) // { // if (value) Flags |= (byte)(1 << index); // else Flags &= (byte)~(1 << index); // } // // public bool HasFlag(SyncFlags flag) => (SyncFlags & flag) != 0; // // public void SetFlag(SyncFlags flag, bool value) // { // if (value) SyncFlags |= flag; // else SyncFlags &= ~flag; // } // // /// // /// 创建完整快照(所有字段) // /// // public static PlayerStateSnapshot CreateFull(FPlayerData player, uint sequenceNumber) // { // var snapshot = new PlayerStateSnapshot // { // PlayerID = player.PlayerID, // SequenceNumber = sequenceNumber, // Timestamp = Time.time, // SyncFlags = SyncFlags.All, // // Position = player.position, // Rotation = player.rotation, // MoveInput = player.MoveInput, // Speed = player.Speed, // IsGrounded = player.IsGrounded, // IsRunning = player.Run, // // State = player.State, // LineLength = player.lineLength, // ReelSpeed = player.reelSpeed, // EyeAngle = player.EyeAngle, // IsHandOnHandle = player.isHandOnHandle, // // HeldItem = player.CurrentHeldItem, // StateParams = player.CurrentStateParams?.Clone(), // // FloatPosition = null, // HookPosition = null, // CaughtFishID = 0 // }; // // snapshot.SetFlag(0, player.openLight); // snapshot.SetFlag(1, player.openTelescope); // snapshot.SetFlag(2, player.ChangeItem); // snapshot.SetFlag(3, player.IsLureRod); // // return snapshot; // } // // /// // /// 创建增量快照(仅包含标记的字段) // /// // public static PlayerStateSnapshot CreateDelta(FPlayerData player, uint sequenceNumber, SyncFlags flags) // { // var snapshot = new PlayerStateSnapshot // { // PlayerID = player.PlayerID, // SequenceNumber = sequenceNumber, // Timestamp = Time.time, // SyncFlags = flags // }; // // // 根据标志位选择性地填充数据 // if (flags.HasFlag(SyncFlags.Physics)) // { // snapshot.Position = player.position; // snapshot.Rotation = player.rotation; // snapshot.MoveInput = player.MoveInput; // snapshot.Speed = player.Speed; // snapshot.IsGrounded = player.IsGrounded; // snapshot.IsRunning = player.Run; // } // // if (flags.HasFlag(SyncFlags.Fishing)) // { // snapshot.State = player.State; // snapshot.LineLength = player.lineLength; // snapshot.ReelSpeed = player.reelSpeed; // snapshot.EyeAngle = player.EyeAngle; // snapshot.IsHandOnHandle = player.isHandOnHandle; // snapshot.StateParams = player.CurrentStateParams?.Clone(); // } // // if (flags.HasFlag(SyncFlags.HeldItem)) // { // snapshot.HeldItem = player.CurrentHeldItem; // } // // if (flags.HasFlag(SyncFlags.Flags)) // { // snapshot.Flags = 0; // snapshot.SetFlag(0, player.openLight); // snapshot.SetFlag(1, player.openTelescope); // snapshot.SetFlag(2, player.ChangeItem); // snapshot.SetFlag(3, player.IsLureRod); // } // // if (flags.HasFlag(SyncFlags.Extension)) // { // snapshot.FloatPosition = null; // 根据需要设置 // snapshot.HookPosition = null; // snapshot.CaughtFishID = 0; // } // // return snapshot; // } // // /// // /// 应用快照到玩家(智能合并) // /// // public void ApplyTo(FPlayerData player) // { // if (PlayerID != player.PlayerID) return; // // player.LastNetworkSyncTime = (float)Timestamp; // // // 根据标志位选择性应用 // if (SyncFlags.HasFlag(SyncFlags.Physics)) // { // player.position = Position; // player.rotation = Rotation; // player.MoveInput = MoveInput; // player.Speed = Speed; // player.IsGrounded = IsGrounded; // player.Run = IsRunning; // } // // if (SyncFlags.HasFlag(SyncFlags.Fishing)) // { // // 状态变化时才触发事件 // if (State != player.State) // { // player.SetState(State, StateParams); // } // // player.lineLength = LineLength; // player.reelSpeed = ReelSpeed; // player.EyeAngle = EyeAngle; // player.isHandOnHandle = IsHandOnHandle; // } // // if (SyncFlags.HasFlag(SyncFlags.HeldItem)) // { // player.CurrentHeldItem = HeldItem; // } // // if (SyncFlags.HasFlag(SyncFlags.Flags)) // { // player.openLight = GetFlag(0); // player.openTelescope = GetFlag(1); // player.ChangeItem = GetFlag(2); // player.IsLureRod = GetFlag(3); // } // // if (SyncFlags.HasFlag(SyncFlags.Extension)) // { // if (FloatPosition.HasValue) // { // /* 应用浮漂位置 */ // } // // if (HookPosition.HasValue) // { // /* 应用鱼钩位置 */ // } // // if (CaughtFishID != 0) // { // /* 应用鱼 ID */ // } // } // } // } // // // /// // /// 玩家钓组数据 // /// // [Serializable] // public class FPlayerGearData // { // public GearType Type = GearType.Spinning; // // public FRodData rod; // public FReelData reel; // public FBobberData bobber; // public FHookData hook; // public FBaitData bait; // public FLureData lure; // public FWeightData weight; // public FLineData line; // public FLeaderData leader; // public FFeederData feeder; // // /// // /// 获得唯一 id // /// // /// // public int GetUnitId() // { // int result = 0; // if (rod != null) // { // result += rod.configId; // } // // if (reel != null) // { // result += reel.configId; // } // // if (bobber != null) // { // result += bobber.configId; // } // // if (hook != null) // { // result += hook.configId; // } // // if (bait != null) // { // result += bait.configId; // } // // if (lure != null) // { // result += lure.configId; // } // // if (weight != null) // { // result += weight.configId; // } // // if (line != null) // { // result += line.configId; // } // // if (leader != null) // { // result += leader.configId; // } // // if (feeder != null) // { // result += feeder.configId; // } // // return result; // } // // public void SetBobberLastSetGroundValue(float value) // { // bobber.lastSetGroundValue = value; // } // // public void SetReelSettings(int setType, float val, bool saveToPrefs = false) // { // if (setType == 0) // { // reel.currentSpeed = val; // } // // if (setType == 1) // { // reel.currentDrag = val; // } // // if (saveToPrefs) // { // // Instance._playerData.SaveEquipment(ItemType.Reel); // } // } // // /// // /// 获取所有非空装备部件 // /// // public List GetAllComponents() // { // var components = new List(); // // if (rod != null) components.Add(rod); // if (reel != null) components.Add(reel); // if (bobber != null) components.Add(bobber); // if (hook != null) components.Add(hook); // if (bait != null) components.Add(bait); // if (lure != null) components.Add(lure); // if (weight != null) components.Add(weight); // if (line != null) components.Add(line); // if (leader != null) components.Add(leader); // if (feeder != null) components.Add(feeder); // // return components; // } // } // // [Serializable] // public abstract class FGearData // { // /// // /// 唯一 id(运行时生成,区分同一配置的多个实例) // /// // public int id; // // /// // /// 配置 id(来自表格) // /// // public int configId; // // /// // /// 物品类型(用于快速判断) // /// // public abstract GearItemType ItemType { get; } // // /// // /// 获取基础物品配置 // /// // public Item ItemConfig => Game.Tables.TbItem.Get(configId); // // /// // /// 创建该类型的默认数据(用于网络同步时重建) // /// // public abstract FGearData CreateDefault(); // // /// // /// 是否已加载配置 // /// // public bool HasConfigLoaded => configId != 0; // // /// // /// 复制基础数据到新实例 // /// // public void CopyBaseTo(FGearData other) // { // other.id = this.id; // other.configId = this.configId; // } // } // // /// // /// 鱼竿数据 // /// // [Serializable] // public class FRodData : FGearData // { // public override GearItemType ItemType => GearItemType.Rod; // // [NonSerialized] private Rod _config; // // public Rod Config => _config ??= Game.Tables.TbRod.Get(configId); // // public override FGearData CreateDefault() // { // return new FRodData { configId = this.configId }; // } // } // // /// // /// 线轴数据 // /// // [Serializable] // public class FReelData : FGearData // { // public override GearItemType ItemType => GearItemType.Reel; // // [NonSerialized] private Reel _config; // // public Reel Config => _config ??= Game.Tables.TbReel.Get(configId); // // public float currentSpeed = 0.5f; // public float currentDrag = 0.7f; // // public override FGearData CreateDefault() // { // return new FReelData // { // configId = this.configId, // currentSpeed = this.currentSpeed, // currentDrag = this.currentDrag // }; // } // } // // /// // /// 浮漂数据 // /// // [Serializable] // public class FBobberData : FGearData // { // public override GearItemType ItemType => GearItemType.Bobber; // // [NonSerialized] private Bobber _config; // // public Bobber Config => _config ??= Game.Tables.TbBobber.Get(configId); // // public float lastSetGroundValue; // // public override FGearData CreateDefault() // { // return new FBobberData // { // configId = this.configId, // lastSetGroundValue = this.lastSetGroundValue // }; // } // } // // /// // /// 鱼钩数据 // /// // [Serializable] // public class FHookData : FGearData // { // public override GearItemType ItemType => GearItemType.Hook; // // [NonSerialized] private Hook _config; // // public Hook Config => _config ??= Game.Tables.TbHook.Get(configId); // // public override FGearData CreateDefault() // { // return new FHookData { configId = this.configId }; // } // } // // /// // /// 鱼饵数据 // /// // [Serializable] // public class FBaitData : FGearData // { // public override GearItemType ItemType => GearItemType.Bait; // // [NonSerialized] private Bait _config; // // public Bait Config => _config ??= Game.Tables.TbBait.Get(configId); // // public override FGearData CreateDefault() // { // return new FBaitData { configId = this.configId }; // } // } // // /// // /// 假饵数据 // /// // [Serializable] // public class FLureData : FGearData // { // public override GearItemType ItemType => GearItemType.Lure; // // [NonSerialized] private Lure _config; // // public Lure Config => _config ??= Game.Tables.TbLure.Get(configId); // // public override FGearData CreateDefault() // { // return new FLureData { configId = this.configId }; // } // } // // /// // /// 沉子数据 // /// // [Serializable] // public class FWeightData : FGearData // { // public override GearItemType ItemType => GearItemType.Weight; // // public override FGearData CreateDefault() // { // return new FWeightData { configId = this.configId }; // } // } // // /// // /// 主线数据 // /// // [Serializable] // public class FLineData : FGearData // { // public override GearItemType ItemType => GearItemType.Line; // // [NonSerialized] private Line _config; // // public Line Config => _config ??= Game.Tables.TbLine.Get(configId); // // public override FGearData CreateDefault() // { // return new FLineData { configId = this.configId }; // } // } // // /// // /// 前导线数据 // /// // [Serializable] // public class FLeaderData : FGearData // { // public override GearItemType ItemType => GearItemType.Leader; // // public override FGearData CreateDefault() // { // return new FLeaderData { configId = this.configId }; // } // } // // /// // /// 菲德数据 // /// // [Serializable] // public class FFeederData : FGearData // { // public override GearItemType ItemType => GearItemType.Feeder; // // [NonSerialized] private Feeder _config; // // public Feeder Config => _config ??= Game.Tables.TbFeeder.Get(configId); // // public override FGearData CreateDefault() // { // return new FFeederData { configId = this.configId }; // } // } // }