using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Cielonos.MainGame.Inventory { public class AttributeSubmodule : SubmoduleBase { public int level; private UpgradeData _upgradeData; public AttributeGroup itemAttributeGroup; public Dictionary charAttrNumericChange; public Dictionary chaAttrPercentageChangeOfAccumulation; public Dictionary chaAttrPercentageChangeOfMultiplication; public bool Has(string attributeName) => itemAttributeGroup.current.ContainsKey(attributeName); public float Get(string attributeName, float defaultValue) => itemAttributeGroup.current.GetValueOrDefault(attributeName, defaultValue); public AttributeSubmodule(ItemBase owner, AttributeData data, UpgradeData upgradeData = null) : base(owner) { this.itemAttributeGroup = new AttributeGroup(data.itemAttributes.ToDictionary()); this.charAttrNumericChange = new Dictionary(data.chaAttrNumericChange); this.chaAttrPercentageChangeOfAccumulation = new Dictionary(data.chaAttrPercentageChangeOfAccumulation); this.chaAttrPercentageChangeOfMultiplication = new Dictionary(data.chaAttrPercentageChangeOfMultiplication); this._upgradeData = upgradeData; } public void GetCharacterAttributeChanges(string attributeName, out float numeric, out float pAccumulation, out float pMultiplication) { numeric = charAttrNumericChange.GetValueOrDefault(attributeName, 0f) + GetUpgradeValue(attributeName, UpgradeInfo.Category.Numeric); pAccumulation = chaAttrPercentageChangeOfAccumulation.GetValueOrDefault(attributeName, 0f); pMultiplication = chaAttrPercentageChangeOfMultiplication.GetValueOrDefault(attributeName, 1f); } public void ApplyCharacterAttributeChanges(string attributeName, ref float numeric, ref float pAccumulation, ref float pMultiplication) { numeric += charAttrNumericChange.GetValueOrDefault(attributeName, 0f) + GetUpgradeValue(attributeName, UpgradeInfo.Category.Numeric); pAccumulation += chaAttrPercentageChangeOfAccumulation.GetValueOrDefault(attributeName, 0f) + GetUpgradeValue(attributeName, UpgradeInfo.Category.PercentageOfAccumulation); pMultiplication *= chaAttrPercentageChangeOfMultiplication.GetValueOrDefault(attributeName, 1f) + GetUpgradeValue(attributeName, UpgradeInfo.Category.PercentageOfMultiplication); } /// /// 获取指定道具属性的有效值(基础值 + 升级增量)。 /// public float GetItemAttribute(string attributeName) { float baseValue = itemAttributeGroup.current.GetValueOrDefault(attributeName, 0f); return baseValue + GetUpgradeValue(attributeName, UpgradeInfo.Category.Item); } private float GetUpgradeValue(string attributeName, UpgradeInfo.Category category) { if (_upgradeData == null || level == 0) return 0f; if (category == UpgradeInfo.Category.Item) { foreach (var upgradeInfo in _upgradeData.upgrades) { if (upgradeInfo.upgradeTarget == UpgradeInfo.UpgradeTarget.ItemAttribute && upgradeInfo.itemAttributeKey == attributeName) { return upgradeInfo.GetValue(level); } } } else { foreach (var upgradeInfo in _upgradeData.upgrades) { if (upgradeInfo.characterAttributeKey == attributeName && upgradeInfo.category == category) { return upgradeInfo.GetValue(level); } } } return 0f; } public List RefreshAllModifiedAttributes() { List modifiedAttributes = new List(); modifiedAttributes.AddRange(charAttrNumericChange.Select(kvp => kvp.Key)); modifiedAttributes.AddRange(chaAttrPercentageChangeOfAccumulation.Select(kvp => kvp.Key)); modifiedAttributes.AddRange(chaAttrPercentageChangeOfMultiplication.Select(kvp => kvp.Key)); if (_upgradeData != null) { modifiedAttributes.AddRange(_upgradeData.upgrades .Where(u => u.upgradeTarget == UpgradeInfo.UpgradeTarget.CharacterAttribute) .Select(u => u.characterAttributeKey)); } modifiedAttributes = modifiedAttributes.Distinct().ToList(); modifiedAttributes.ForEach(attr => owner.player.attributeSm.RefreshAttribute(attr)); return modifiedAttributes; } } }