加入武器 tag 的羁绊效果

This commit is contained in:
2026-10-04 14:45:25 +08:00
parent 0077e3449d
commit c76a421f4a
130 changed files with 3248 additions and 25453 deletions
@@ -12,6 +12,8 @@ namespace SepCore.Battle
public sealed class AttributePipeline : IAttributeProvider
{
private readonly Dictionary<StatType, CharacterStat> _stats = new();
private int _batchDepth;
private readonly HashSet<StatType> _batchedChangedStats = new();
/// <summary>
/// 当某个属性的值发生变动(添加/移除修饰器、修改基础值)时触发。
@@ -19,6 +21,65 @@ namespace SepCore.Battle
/// </summary>
public event Action<StatType, float> OnStatChanged;
/// <summary>
/// 开启批量修饰器更新作用域。在作用域内添加/移除修饰器不会立即触发 OnStatChanged,
/// 而是在作用域 Dispose 时对所有发生变更的属性统一触发一次最终值的 OnStatChanged。
/// </summary>
public BatchScope BeginBatchUpdate()
{
_batchDepth++;
return new BatchScope(this);
}
public void EndBatchUpdate()
{
if (_batchDepth <= 0)
{
return;
}
_batchDepth--;
if (_batchDepth == 0 && _batchedChangedStats.Count > 0)
{
StatType[] changed = new StatType[_batchedChangedStats.Count];
_batchedChangedStats.CopyTo(changed);
_batchedChangedStats.Clear();
for (int i = 0; i < changed.Length; i++)
{
StatType statType = changed[i];
OnStatChanged?.Invoke(statType, GetValue(statType));
}
}
}
private void NotifyStatChanged(StatType statType, float newValue)
{
if (_batchDepth > 0)
{
_batchedChangedStats.Add(statType);
}
else
{
OnStatChanged?.Invoke(statType, newValue);
}
}
public readonly struct BatchScope : IDisposable
{
private readonly AttributePipeline _pipeline;
public BatchScope(AttributePipeline pipeline)
{
_pipeline = pipeline;
}
public void Dispose()
{
_pipeline?.EndBatchUpdate();
}
}
public AttributePipeline()
{
// 预初始化已定义的所有 StatType 枚举
@@ -64,7 +125,7 @@ namespace SepCore.Battle
if (Math.Abs(oldValue - newValue) > 1e-6f)
{
OnStatChanged?.Invoke(statType, newValue);
NotifyStatChanged(statType, newValue);
}
}
@@ -80,7 +141,7 @@ namespace SepCore.Battle
CharacterStat stat = Get(statType);
stat.AddModifier(modifier);
OnStatChanged?.Invoke(statType, stat.Value);
NotifyStatChanged(statType, stat.Value);
}
/// <summary>
@@ -97,7 +158,7 @@ namespace SepCore.Battle
bool removed = stat.RemoveModifier(modifier);
if (removed)
{
OnStatChanged?.Invoke(statType, stat.Value);
NotifyStatChanged(statType, stat.Value);
}
return removed;
@@ -165,7 +226,7 @@ namespace SepCore.Battle
if (removed > 0)
{
totalRemoved += removed;
OnStatChanged?.Invoke(pair.Key, pair.Value.Value);
NotifyStatChanged(pair.Key, pair.Value.Value);
}
}
@@ -180,7 +241,7 @@ namespace SepCore.Battle
foreach (KeyValuePair<StatType, CharacterStat> pair in _stats)
{
pair.Value.ClearModifiers();
OnStatChanged?.Invoke(pair.Key, pair.Value.Value);
NotifyStatChanged(pair.Key, pair.Value.Value);
}
}
}
@@ -0,0 +1,157 @@
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;
}
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c5575d5d9f07dd147b64aa0070d63914
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using SepCore.Battle;
using SepCore.Definition;
using SepCore.Entity;
using UnityEngine;
@@ -27,6 +28,11 @@ namespace SepCore.Components
/// </summary>
private StatComponent _statComponent;
/// <summary>
/// 武器标签羁绊管理器。
/// </summary>
private readonly WeaponSynergyManager _synergyManager = new();
/// <summary>
/// 武器装备槽,在 Editor 中进行配置,表示武器在世界中的显示位置。
/// </summary>
@@ -35,6 +41,7 @@ namespace SepCore.Components
public IReadOnlyList<WeaponBase> Weapons => _weapons;
public IReadOnlyList<PropItem> Props => _props;
public int WeaponCapacity => GameEntry.Luban.Global.WeaponCapacity;
public WeaponSynergyManager SynergyManager => _synergyManager;
public void OnInit(Player player)
{
@@ -43,12 +50,14 @@ namespace SepCore.Components
_weapons = new List<WeaponBase>(WeaponCapacity);
_props = new List<PropItem>();
_statComponent = GetComponent<StatComponent>();
_synergyManager.Initialize();
}
public void OnReset()
{
_weapons.Clear();
_props.Clear();
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
}
public bool AttachWeapon(WeaponBase weapon)
@@ -57,6 +66,7 @@ namespace SepCore.Components
GameEntry.Entity.AttachEntity(weapon.Id, _player.Id, _weaponSlots[_weapons.Count]);
_weapons.Add(weapon);
weapon.SetEnabled(true);
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
return true;
}
@@ -66,6 +76,7 @@ namespace SepCore.Components
if (result)
{
GameEntry.Entity.DetachEntity(weapon.Id, _player.Id);
_synergyManager.RefreshSynergies(_weapons, _statComponent.Pipeline);
}
return result;
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using SepCore.Definition;
using SepCore.Entity;
using UnityEngine;
@@ -22,5 +23,7 @@ namespace SepCore.UI
public int Price;
public Vector3 TargetPos;
public Action<int, int> OnRecycle;
public int LastWaveDamage;
public IReadOnlyDictionary<WeaponTag, int> CurrentTagCounts;
}
}