AttributeSet

4.9k words

AttributeSet

具体派生类根据编辑器工具生成(定义属性(atk、def)等、属性初始化、属性赋值(Min、Max、Current、Base))

1
2
3
4
5
6
7
8
9
10
11

public abstract class AttributeSet
{
protected AbilitySystemComponent _owner;

public abstract AttributeBase this[string key] { get; }
public abstract string[] AttributeNames { get; }
public abstract void SetOwner(AbilitySystemComponent owner);
public void ChangeAttributeBase(string attributeShortName, float value);
}

CalculateMode

1
2
3
4
5
6
7
8
9
10
11
12
13

public enum CalculateMode
{
[LabelText(SdfIconType.Stack, Text = "叠加计算")]
Stacking,

[LabelText(SdfIconType.GraphDownArrow, Text = "取最小值")]
MinValueOnly,

[LabelText(SdfIconType.GraphUpArrow, Text = "取最大值")]
MaxValueOnly,
}

AttributeValue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

public struct AttributeValue
{
public AttributeValue(float baseValue,
CalculateMode calculateMode = CalculateMode.Stacking,
SupportedOperation supportedOperation = SupportedOperation.All,
float minValue = float.MinValue, float maxValue = float.MaxValue)
{
BaseValue = baseValue;
SupportedOperation = supportedOperation;
CurrentValue = baseValue;
CalculateMode = calculateMode;
MinValue = minValue;
MaxValue = maxValue;
}

public CalculateMode CalculateMode { get; }
public SupportedOperation SupportedOperation { get; }

public float BaseValue { get; private set; }
public float CurrentValue { get; private set; }

public float MinValue { get; private set; }
public float MaxValue { get; private set; }

}

AttributeBase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

public class AttributeBase
{
public readonly string Name;
public readonly string SetName;
public readonly string ShortName;
protected event Action<AttributeBase, float, float> _onPostCurrentValueChange;
protected event Action<AttributeBase, float, float> _onPostBaseValueChange;
protected event Action<AttributeBase, float> _onPreCurrentValueChange;
protected event Func<AttributeBase, float, float> _onPreBaseValueChange;
protected IEnumerable<Func<AttributeBase, float, float>> _preBaseValueChangeListeners;

private AttributeValue _value;
public AttributeValue Value => _value;
public float BaseValue => _value.BaseValue;
public float CurrentValue => _value.CurrentValue;
public float MinValue => _value.MinValue;
public float MaxValue => _value.MaxValue;
public CalculateMode CalculateMode => _value.CalculateMode;
public SupportedOperation SupportedOperation => _value.SupportedOperation;

private AbilitySystemComponent _owner;
public AbilitySystemComponent Owner => _owner;

public AttributeBase(string attrSetName, string attrName, float value = 0,
CalculateMode calculateMode = CalculateMode.Stacking,
SupportedOperation supportedOperation = SupportedOperation.All,
float minValue = float.MinValue, float maxValue = float.MaxValue);

// modify value
public void SetCurrentValue(float value);
public void SetCurrentValueWithoutEvent(float value);
public void SetBaseValue(float value);
public void SetBaseValueWithoutEvent(float value);

// event reigster
// 用法:_asc.AttrSet<AS_Fight>().Hp.RegisterPostBaseValueChange(OnHpChange);
public void RegisterPreBaseValueChange(Func<AttributeBase, float, float> func);
public void RegisterPostBaseValueChange(Action<AttributeBase, float, float> action);
public void RegisterPreCurrentValueChange(Action<AttributeBase, float> action);
public void RegisterPostCurrentValueChange(Action<AttributeBase, float, float> action);
public void UnregisterPreBaseValueChange(Func<AttributeBase, float, float> func);
public void UnregisterPostBaseValueChange(Action<AttributeBase, float, float> action);
public void UnregisterPreCurrentValueChange(Action<AttributeBase, float> action);
public void UnregisterPostCurrentValueChange(Action<AttributeBase, float, float> action);


}

AttributeSetContainer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

public class AttributeSetContainer
{
private readonly AbilitySystemComponent _owner;
private readonly Dictionary<string, AttributeSet> _attributeSets = new Dictionary<string, AttributeSet>();

private readonly Dictionary<AttributeBase, AttributeAggregator> _attributeAggregators =
new Dictionary<AttributeBase, AttributeAggregator>();

public Dictionary<string, AttributeSet> Sets => _attributeSets;

public AttributeSetContainer(AbilitySystemComponent owner);

public void AddAttributeSet<T>() where T : AttributeSet;
public void AddAttributeSet(Type attrSetType);
public void RemoveAttributeSet<T>() where T : AttributeSet;

public bool TryGetAttributeSet<T>(out T attributeSet) where T : AttributeSet;
bool TryGetAttributeSet(Type attrSetType, out AttributeSet attributeSet);
public AttributeValue? GetAttributeAttributeValue(string attrSetName, string attrShortName);
public CalculateMode? GetAttributeCalculateMode(string attrSetName, string attrShortName);
public float? GetAttributeBaseValue(string attrSetName, string attrShortName);
public float? GetAttributeCurrentValue(string attrSetName, string attrShortName);

//快照
public Dictionary<string, float> Snapshot();



}