|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using GameFrameX.Apps.Player.Attribute; |
| 4 | + |
| 5 | +namespace GameFrameX.Hotfix.Logic.Player.Attribute; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// 玩家属性状态写入与最终值重算逻辑。 |
| 9 | +/// </summary> |
| 10 | +public static class PlayerAttributeMutation |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// 将属性值写入状态字典,并返回最终属性变化判定。 |
| 14 | + /// </summary> |
| 15 | + /// <param name="values">状态字典。</param> |
| 16 | + /// <param name="attributeType">属性编号。</param> |
| 17 | + /// <param name="value">新属性值。</param> |
| 18 | + /// <param name="silent">是否静默写入。</param> |
| 19 | + /// <returns>属性写入结果。</returns> |
| 20 | + public static AttributeChangeResult ApplyValue(Dictionary<int, long> values, AttributeType attributeType, long value, bool silent) |
| 21 | + { |
| 22 | + if (values == null) |
| 23 | + { |
| 24 | + throw new ArgumentNullException(nameof(values)); |
| 25 | + } |
| 26 | + |
| 27 | + var oldValue = GetValue(values, attributeType); |
| 28 | + if (oldValue == value) |
| 29 | + { |
| 30 | + return AttributeChangeResult.NotChanged(attributeType, value); |
| 31 | + } |
| 32 | + |
| 33 | + values[(int)attributeType] = value; |
| 34 | + |
| 35 | + if (AttributeCore.TryGetFinalAttribute(attributeType, out var finalAttribute)) |
| 36 | + { |
| 37 | + var oldFinalValue = GetValue(values, finalAttribute); |
| 38 | + var newFinalValue = AttributeCore.RecalculateFinal(ToAttributeDictionary(values), finalAttribute); |
| 39 | + values[(int)finalAttribute] = newFinalValue; |
| 40 | + return new AttributeChangeResult(attributeType, finalAttribute, oldFinalValue, newFinalValue, true, oldFinalValue != newFinalValue && !silent); |
| 41 | + } |
| 42 | + |
| 43 | + if (AttributeCore.IsFinalAttribute(attributeType)) |
| 44 | + { |
| 45 | + return new AttributeChangeResult(attributeType, attributeType, oldValue, value, true, !silent); |
| 46 | + } |
| 47 | + |
| 48 | + return new AttributeChangeResult(attributeType, AttributeType.None, 0L, 0L, true, false); |
| 49 | + } |
| 50 | + |
| 51 | + private static long GetValue(Dictionary<int, long> values, AttributeType attributeType) |
| 52 | + { |
| 53 | + long value; |
| 54 | + return values.TryGetValue((int)attributeType, out value) ? value : 0L; |
| 55 | + } |
| 56 | + |
| 57 | + private static Dictionary<AttributeType, long> ToAttributeDictionary(Dictionary<int, long> values) |
| 58 | + { |
| 59 | + var attributes = new Dictionary<AttributeType, long>(); |
| 60 | + foreach (var item in values) |
| 61 | + { |
| 62 | + attributes[(AttributeType)item.Key] = item.Value; |
| 63 | + } |
| 64 | + |
| 65 | + return attributes; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// <summary> |
| 70 | +/// 属性写入后的最终属性变化判定。 |
| 71 | +/// </summary> |
| 72 | +public sealed class AttributeChangeResult |
| 73 | +{ |
| 74 | + public AttributeType SourceAttributeType { get; } |
| 75 | + |
| 76 | + public AttributeType FinalAttributeType { get; } |
| 77 | + |
| 78 | + public long OldFinalValue { get; } |
| 79 | + |
| 80 | + public long NewFinalValue { get; } |
| 81 | + |
| 82 | + public bool StateChanged { get; } |
| 83 | + |
| 84 | + public bool ShouldDispatch { get; } |
| 85 | + |
| 86 | + public AttributeChangeResult(AttributeType sourceAttributeType, AttributeType finalAttributeType, long oldFinalValue, long newFinalValue, bool stateChanged, bool shouldDispatch) |
| 87 | + { |
| 88 | + SourceAttributeType = sourceAttributeType; |
| 89 | + FinalAttributeType = finalAttributeType; |
| 90 | + OldFinalValue = oldFinalValue; |
| 91 | + NewFinalValue = newFinalValue; |
| 92 | + StateChanged = stateChanged; |
| 93 | + ShouldDispatch = shouldDispatch; |
| 94 | + } |
| 95 | + |
| 96 | + public static AttributeChangeResult NotChanged(AttributeType sourceAttributeType, long value) |
| 97 | + { |
| 98 | + return new AttributeChangeResult(sourceAttributeType, AttributeType.None, value, value, false, false); |
| 99 | + } |
| 100 | +} |
0 commit comments