using System.Collections.Generic; using XGame.Framework.Interfaces; namespace XGame.Database { /// /// 通用属性列表 /// public class Attributes : IReference { private Dictionary _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); } /// /// 返回当前存储的属性类型 /// /// /// 0:全部属性; 1:只取基础属性, 2:只取装备特殊属性 /// public bool GetAll(ref List attributeTypes, int option = 0) { if (attributeTypes == null) attributeTypes = new List(); 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; } /// /// 清空所有属性 /// public void Clear() { _attributeMap.Clear(); } /// /// 数据拷贝 /// /// public void CopyTo(Attributes target) { foreach (var item in _attributeMap) { target.SetValue(item.Key, item.Value); } } #region 扩展 /// /// 基础生命 /// public long Hp { get => GetValue(EAttributeType.Hp); set => SetValue(EAttributeType.Hp, value); } /// /// 总生命值/生命上限 /// public long HpLimit { get => GetValue(EAttributeType.HpTotal); set => SetValue(EAttributeType.HpTotal, value); } /// /// 移动速度 /// public long MoveSpeed { get => GetValue(EAttributeType.Speed); set => SetValue(EAttributeType.Speed, value); } /// /// 攻击力 /// public long Attack { get => GetValue(EAttributeType.AtkTotal); set => SetValue(EAttributeType.AtkTotal, value); } #endregion } }