using System.Collections.Generic; using SepCore.Definition; using SepCore.Entity; namespace SepCore.Battle { /// /// 武器标签羁绊管理器。 /// 维护当前装备武器的标签统计、计算最高阶梯羁绊,并向属性管线派发/撤销带来源标记(Source)的 StatModifier。 /// 纯 C# 类,保证数据唯一真相源与零高频 GC。 /// public sealed class WeaponSynergyManager { private readonly Dictionary> _tagTierConfigs = new(); private readonly Dictionary _tagCounts = new(); private readonly Dictionary _activeTiers = new(); public IReadOnlyDictionary TagCounts => _tagCounts; public IReadOnlyDictionary ActiveTiers => _activeTiers; public void Initialize() { _tagTierConfigs.Clear(); if (GameEntry.Luban == null || !GameEntry.Luban.IsReady) { return; } var allConfigs = GameEntry.Luban.GetTable(); for (int i = 0; i < allConfigs.Count; i++) { WeaponTagConfig config = allConfigs[i]; if (config.Tag == WeaponTag.None) { continue; } if (!_tagTierConfigs.TryGetValue(config.Tag, out List list)) { list = new List(); _tagTierConfigs.Add(config.Tag, list); } list.Add(config); } foreach (var kvp in _tagTierConfigs) { kvp.Value.Sort((a, b) => a.RequiredCount.CompareTo(b.RequiredCount)); } } public int GetTagCount(WeaponTag tag) { return _tagCounts.TryGetValue(tag, out int count) ? count : 0; } public WeaponTagConfig GetActiveTier(WeaponTag tag) { return _activeTiers.TryGetValue(tag, out WeaponTagConfig tier) ? tier : null; } public IReadOnlyList GetTiersForTag(WeaponTag tag) { return _tagTierConfigs.TryGetValue(tag, out List list) ? list : System.Array.Empty(); } /// /// 全量刷新羁绊状态并同步属性管线(强幂等)。 /// 档位变动时执行旧档位 Source 移除与新档位挂载。 /// public void RefreshSynergies(IReadOnlyList weapons, AttributePipeline pipeline) { if (pipeline == null) { return; } // 1. 重新统计 Tag 计数 _tagCounts.Clear(); if (weapons != null) { for (int i = 0; i < weapons.Count; i++) { WeaponBase weapon = weapons[i]; if (weapon?.WeaponData == null) { continue; } IReadOnlyList tags = weapon.WeaponData.Tags; if (tags == null) { continue; } for (int t = 0; t < tags.Count; t++) { WeaponTag tag = tags[t]; if (tag == WeaponTag.None) { continue; } _tagCounts[tag] = _tagCounts.GetValueOrDefault(tag, 0) + 1; } } } // 2. 检查每个 Tag 的阶梯阈值达成情况(最高生效原则 / Milestone Replacement) // 使用 BeginBatchUpdate 作用域原子性批量替换修饰器,确保旧档卸载与新档挂载完成后统一通知一次属性变更, // 避免生命值上限临时下降导致当前生命值被意外裁剪。 using (pipeline.BeginBatchUpdate()) { foreach (var kvp in _tagTierConfigs) { WeaponTag tag = kvp.Key; List tiers = kvp.Value; int currentCount = _tagCounts.GetValueOrDefault(tag, 0); WeaponTagConfig bestTier = null; for (int i = tiers.Count - 1; i >= 0; i--) { if (currentCount >= tiers[i].RequiredCount) { bestTier = tiers[i]; break; } } _activeTiers.TryGetValue(tag, out WeaponTagConfig oldTier); if (oldTier != bestTier) { // 卸载旧档位的修饰器 if (oldTier != null) { pipeline.RemoveAllModifiersFromSource(oldTier); } // 挂载新档位的修饰器 if (bestTier != null && bestTier.StatModifierConfigs != null) { for (int m = 0; m < bestTier.StatModifierConfigs.Count; m++) { pipeline.AddModifier(bestTier.StatModifierConfigs[m], bestTier); } } _activeTiers[tag] = bestTier; } } } } } }