158 lines
5.7 KiB
C#
158 lines
5.7 KiB
C#
using System.Collections.Generic;
|
||
using SepCore.Definition;
|
||
using SepCore.Entity;
|
||
|
||
namespace SepCore.Battle
|
||
{
|
||
/// <summary>
|
||
/// 武器标签羁绊管理器。
|
||
/// 维护当前装备武器的标签统计、计算最高阶梯羁绊,并向属性管线派发/撤销带来源标记(Source)的 StatModifier。
|
||
/// 纯 C# 类,保证数据唯一真相源与零高频 GC。
|
||
/// </summary>
|
||
public sealed class WeaponSynergyManager
|
||
{
|
||
private readonly Dictionary<WeaponTag, List<WeaponTagConfig>> _tagTierConfigs = new();
|
||
private readonly Dictionary<WeaponTag, int> _tagCounts = new();
|
||
private readonly Dictionary<WeaponTag, WeaponTagConfig> _activeTiers = new();
|
||
|
||
public IReadOnlyDictionary<WeaponTag, int> TagCounts => _tagCounts;
|
||
public IReadOnlyDictionary<WeaponTag, WeaponTagConfig> ActiveTiers => _activeTiers;
|
||
|
||
public void Initialize()
|
||
{
|
||
_tagTierConfigs.Clear();
|
||
if (GameEntry.Luban == null || !GameEntry.Luban.IsReady)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var allConfigs = GameEntry.Luban.GetTable<WeaponTagConfig>();
|
||
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<WeaponTagConfig> list))
|
||
{
|
||
list = new List<WeaponTagConfig>();
|
||
_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<WeaponTagConfig> GetTiersForTag(WeaponTag tag)
|
||
{
|
||
return _tagTierConfigs.TryGetValue(tag, out List<WeaponTagConfig> list)
|
||
? list
|
||
: System.Array.Empty<WeaponTagConfig>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 全量刷新羁绊状态并同步属性管线(强幂等)。
|
||
/// 档位变动时执行旧档位 Source 移除与新档位挂载。
|
||
/// </summary>
|
||
public void RefreshSynergies(IReadOnlyList<WeaponBase> 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<WeaponTag> 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<WeaponTagConfig> 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;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|