123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System.Collections.Generic;
- using XGame.Framework.Interfaces;
- namespace XGame.Database
- {
- /// <summary>
- /// 通用属性列表
- /// </summary>
- public class Attributes : IReference
- {
- private Dictionary<EAttributeType, long> _attributeMap = new();
- public long GetValue(EAttributeType type)
- {
- if (_attributeMap.TryGetValue(type, out var val))
- {
- return val;
- }
- return 0;
- }
- public void SetValue(EAttributeType type, long value)
- {
- _attributeMap[type] = value;
- }
- public void AddValue(EAttributeType type, long addVal)
- {
- if (!_attributeMap.TryGetValue(type, out var val))
- {
- val = 0;
- }
- val += addVal;
- _attributeMap[type] = val;
- }
- public bool IsContains(EAttributeType type)
- {
- return _attributeMap.ContainsKey(type);
- }
- /// <summary>
- /// 返回当前存储的属性类型
- /// </summary>
- /// <param name="attributeTypes"></param>
- /// <param name="option">0:全部属性; 1:只取基础属性, 2:只取装备特殊属性</param>
- /// <returns></returns>
- public bool GetAll(ref List<EAttributeType> attributeTypes, int option = 0)
- {
- if (attributeTypes == null)
- attributeTypes = new List<EAttributeType>();
- foreach (var key in _attributeMap.Keys)
- {
- if (option == 1 && key.IsBasic() == false)
- {
- continue;
- }
- if (option == 2 && key.IsItemSpecial() == false)
- {
- continue;
- }
- attributeTypes.Add(key);
- }
- return attributeTypes.Count > 0;
- }
- /// <summary>
- /// 清空所有属性
- /// </summary>
- public void Clear()
- {
- _attributeMap.Clear();
- }
- /// <summary>
- /// 数据拷贝
- /// </summary>
- /// <param name="target"></param>
- public void CopyTo(Attributes target)
- {
- foreach (var item in _attributeMap)
- {
- target.SetValue(item.Key, item.Value);
- }
- }
- #region 扩展
- /// <summary>
- /// 基础生命
- /// </summary>
- public long Hp { get => GetValue(EAttributeType.Hp); set => SetValue(EAttributeType.Hp, value); }
- /// <summary>
- /// 总生命值/生命上限
- /// </summary>
- public long HpLimit { get => GetValue(EAttributeType.HpTotal); set => SetValue(EAttributeType.HpTotal, value); }
- /// <summary>
- /// 移动速度
- /// </summary>
- public long MoveSpeed { get => GetValue(EAttributeType.Speed); set => SetValue(EAttributeType.Speed, value); }
- /// <summary>
- /// 攻击力
- /// </summary>
- public long Attack { get => GetValue(EAttributeType.AtkTotal); set => SetValue(EAttributeType.AtkTotal, value); }
- #endregion
- }
- }
|