加入武器 tag 的羁绊效果
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user