引入属性、伤害管线
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SepCore.Battle;
|
||||
using SepCore.Definition;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -7,8 +8,12 @@ namespace SepCore.Components
|
||||
{
|
||||
public class StatComponent : MonoBehaviour
|
||||
{
|
||||
private readonly Dictionary<StatType, List<modifier>> _statModifiers = new();
|
||||
private readonly Dictionary<StatType, Action<modifier, bool>> _statModifierCallbacks = new();
|
||||
private readonly AttributePipeline _pipeline = new();
|
||||
|
||||
/// <summary>
|
||||
/// 纯逻辑属性修饰器管线。
|
||||
/// </summary>
|
||||
public AttributePipeline Pipeline => _pipeline;
|
||||
|
||||
public void OnInit()
|
||||
{
|
||||
@@ -16,82 +21,42 @@ namespace SepCore.Components
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_statModifiers.Clear();
|
||||
_statModifierCallbacks.Clear();
|
||||
_pipeline.Clear();
|
||||
}
|
||||
|
||||
public void AddModifier(modifier modifier)
|
||||
public void AddModifier(StatType statType, StatModifier modifier)
|
||||
{
|
||||
if (!_statModifiers.TryGetValue(modifier.StatType, out var modifiers))
|
||||
{
|
||||
modifiers = new List<modifier>();
|
||||
_statModifiers.Add(modifier.StatType, modifiers);
|
||||
}
|
||||
modifiers.Add(modifier);
|
||||
|
||||
if (_statModifierCallbacks.TryGetValue(modifier.StatType, out var callbacks))
|
||||
callbacks?.Invoke(modifier, true);
|
||||
_pipeline.AddModifier(statType, modifier);
|
||||
}
|
||||
|
||||
public void RemoveModifier(modifier modifier)
|
||||
public bool RemoveModifier(StatType statType, StatModifier modifier)
|
||||
{
|
||||
if (_statModifiers.TryGetValue(modifier.StatType, out var modifiers))
|
||||
{
|
||||
modifiers.Remove(modifier);
|
||||
}
|
||||
|
||||
if (_statModifierCallbacks.TryGetValue(modifier.StatType, out var callbacks))
|
||||
callbacks?.Invoke(modifier, false);
|
||||
return _pipeline.RemoveModifier(statType, modifier);
|
||||
}
|
||||
|
||||
public void Subscribe(StatType statType, Action<modifier, bool> callback)
|
||||
public void AddModifier(StatModifierConfig statModifierConfig, object source = null)
|
||||
{
|
||||
if (!_statModifierCallbacks.TryAdd(statType, callback))
|
||||
{
|
||||
_statModifierCallbacks[statType] += callback;
|
||||
}
|
||||
_pipeline.AddModifier(statModifierConfig, source);
|
||||
}
|
||||
|
||||
public void Unsubscribe(StatType statType, Action<modifier, bool> callback)
|
||||
public void RemoveModifier(StatModifierConfig statModifierConfig, object source = null)
|
||||
{
|
||||
if (!_statModifierCallbacks.TryGetValue(statType, out var callbacks))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
callbacks -= callback;
|
||||
if (callbacks == null)
|
||||
{
|
||||
_statModifierCallbacks.Remove(statType);
|
||||
}
|
||||
_pipeline.RemoveModifier(statModifierConfig, source);
|
||||
}
|
||||
|
||||
public StatProperty GetStat(StatType statType)
|
||||
public int RemoveAllModifiersFromSource(object source)
|
||||
{
|
||||
var result = new StatProperty();
|
||||
|
||||
if (!_statModifiers.TryGetValue(statType, out var modifiers)) return result;
|
||||
|
||||
foreach (var modifier in modifiers)
|
||||
{
|
||||
if (modifier.IsPercent) result.Percent += modifier.RealValue;
|
||||
else result.Value += modifier.RealValue;
|
||||
}
|
||||
|
||||
return result;
|
||||
return _pipeline.RemoveAllModifiersFromSource(source);
|
||||
}
|
||||
|
||||
public void UpdateStat(StatProperty stat, modifier modifier, bool isApply)
|
||||
public float GetStatValue(StatType statType)
|
||||
{
|
||||
float coef = isApply ? 1f : -1f;
|
||||
if (modifier.IsPercent)
|
||||
{
|
||||
stat.Percent += modifier.RealValue * coef;
|
||||
}
|
||||
else
|
||||
{
|
||||
stat.Value += modifier.RealValue * coef;
|
||||
}
|
||||
return _pipeline.GetValue(statType);
|
||||
}
|
||||
|
||||
public CharacterStat GetCharacterStat(StatType statType)
|
||||
{
|
||||
return _pipeline.Get(statType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user