Attributes.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using XGame.Framework.Interfaces;
  3. namespace XGame.Database
  4. {
  5. /// <summary>
  6. /// 通用属性列表
  7. /// </summary>
  8. public class Attributes : IReference
  9. {
  10. private Dictionary<EAttributeType, long> _attributeMap = new();
  11. public long GetValue(EAttributeType type)
  12. {
  13. if (_attributeMap.TryGetValue(type, out var val))
  14. {
  15. return val;
  16. }
  17. return 0;
  18. }
  19. public void SetValue(EAttributeType type, long value)
  20. {
  21. _attributeMap[type] = value;
  22. }
  23. public void AddValue(EAttributeType type, long addVal)
  24. {
  25. if (!_attributeMap.TryGetValue(type, out var val))
  26. {
  27. val = 0;
  28. }
  29. val += addVal;
  30. _attributeMap[type] = val;
  31. }
  32. public bool IsContains(EAttributeType type)
  33. {
  34. return _attributeMap.ContainsKey(type);
  35. }
  36. /// <summary>
  37. /// 返回当前存储的属性类型
  38. /// </summary>
  39. /// <param name="attributeTypes"></param>
  40. /// <param name="option">0:全部属性; 1:只取基础属性, 2:只取装备特殊属性</param>
  41. /// <returns></returns>
  42. public bool GetAll(ref List<EAttributeType> attributeTypes, int option = 0)
  43. {
  44. if (attributeTypes == null)
  45. attributeTypes = new List<EAttributeType>();
  46. foreach (var key in _attributeMap.Keys)
  47. {
  48. if (option == 1 && key.IsBasic() == false)
  49. {
  50. continue;
  51. }
  52. if (option == 2 && key.IsItemSpecial() == false)
  53. {
  54. continue;
  55. }
  56. attributeTypes.Add(key);
  57. }
  58. return attributeTypes.Count > 0;
  59. }
  60. /// <summary>
  61. /// 清空所有属性
  62. /// </summary>
  63. public void Clear()
  64. {
  65. _attributeMap.Clear();
  66. }
  67. /// <summary>
  68. /// 数据拷贝
  69. /// </summary>
  70. /// <param name="target"></param>
  71. public void CopyTo(Attributes target)
  72. {
  73. foreach (var item in _attributeMap)
  74. {
  75. target.SetValue(item.Key, item.Value);
  76. }
  77. }
  78. #region 扩展
  79. /// <summary>
  80. /// 基础生命
  81. /// </summary>
  82. public long Hp { get => GetValue(EAttributeType.Hp); set => SetValue(EAttributeType.Hp, value); }
  83. /// <summary>
  84. /// 总生命值/生命上限
  85. /// </summary>
  86. public long HpLimit { get => GetValue(EAttributeType.HpTotal); set => SetValue(EAttributeType.HpTotal, value); }
  87. /// <summary>
  88. /// 移动速度
  89. /// </summary>
  90. public long MoveSpeed { get => GetValue(EAttributeType.Speed); set => SetValue(EAttributeType.Speed, value); }
  91. /// <summary>
  92. /// 攻击力
  93. /// </summary>
  94. public long Attack { get => GetValue(EAttributeType.AtkTotal); set => SetValue(EAttributeType.AtkTotal, value); }
  95. #endregion
  96. }
  97. }