Files
Cielonos/Assets/Scripts/MainGame/Items/Submodules/AttributeSubmodule.cs
SoulliesOfficial 649b7a5ddc 更新
2026-05-23 08:27:50 -04:00

109 lines
5.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Cielonos.MainGame.Inventory
{
public class AttributeSubmodule : SubmoduleBase<ItemBase>
{
public int level;
private UpgradeData _upgradeData;
public AttributeGroup itemAttributeGroup;
public Dictionary<string, float> charAttrNumericChange;
public Dictionary<string, float> chaAttrPercentageChangeOfAccumulation;
public Dictionary<string, float> 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<string, float>(data.chaAttrNumericChange);
this.chaAttrPercentageChangeOfAccumulation = new Dictionary<string, float>(data.chaAttrPercentageChangeOfAccumulation);
this.chaAttrPercentageChangeOfMultiplication = new Dictionary<string, float>(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);
}
/// <summary>
/// 获取指定道具属性的有效值(基础值 + 升级增量)。
/// </summary>
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<string> RefreshAllModifiedAttributes()
{
List<string> modifiedAttributes = new List<string>();
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;
}
}
}