Files
Cielonos/Assets/Scripts/MainGame/Items/Submodules/AttributeSubmodule.cs
2026-05-10 11:47:55 -04:00

83 lines
3.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Cielonos.MainGame.Characters.Inventory
{
public class AttributeSubmodule : SubmoduleBase<ItemBase>
{
public int level;
public AttributeGroup attributeGroup;
public Dictionary<string, float> numericChange;
public Dictionary<string, float> percentageChangeOfAccumulation;
public Dictionary<string, float> percentageChangeOfMultiplication;
private UpgradeData _upgradeData;
public bool Has(string attributeName) => attributeGroup.current.ContainsKey(attributeName);
public float Get(string attributeName, float defaultValue) => attributeGroup.current.GetValueOrDefault(attributeName, defaultValue);
public float this[string attributeName]
{
get => attributeGroup.current.GetValueOrDefault(attributeName, attributeName.Contains("Multiplier") ? 1 : 0);
set => attributeGroup.current[attributeName] = value;
}
public AttributeSubmodule(ItemBase owner, AttributeData data, UpgradeData upgradeData = null) : base(owner)
{
this.attributeGroup = new AttributeGroup(data.itemAttributes.ToDictionary());
this.numericChange = new Dictionary<string, float>(data.chaAttrNumericChange);
this.percentageChangeOfAccumulation = new Dictionary<string, float>(data.chaAttrPercentageChangeOfAccumulation);
this.percentageChangeOfMultiplication = new Dictionary<string, float>(data.chaAttrPercentageChangeOfMultiplication);
this._upgradeData = upgradeData;
}
public void GetAttributeChanges(string attributeName, out float numeric, out float pAccumulation, out float pMultiplication)
{
numeric = numericChange.GetValueOrDefault(attributeName, 0f) + GetUpgradeNumericValue(attributeName);
pAccumulation = percentageChangeOfAccumulation.GetValueOrDefault(attributeName, 0f);
pMultiplication = percentageChangeOfMultiplication.GetValueOrDefault(attributeName, 1f);
}
public void ApplyAttributeChanges(string attributeName, ref float numeric, ref float pAccumulation, ref float pMultiplication)
{
numeric += numericChange.GetValueOrDefault(attributeName, 0f) + GetUpgradeNumericValue(attributeName);
pAccumulation += percentageChangeOfAccumulation.GetValueOrDefault(attributeName, 0f);
pMultiplication *= percentageChangeOfMultiplication.GetValueOrDefault(attributeName, 1f);
}
private float GetUpgradeNumericValue(string attributeName)
{
if (_upgradeData == null || level == 0) return 0f;
foreach (var upgradeInfo in _upgradeData.upgrades)
{
if (upgradeInfo.attributeKey == attributeName)
{
return upgradeInfo.GetValue(level);
}
}
return 0f;
}
public List<string> RefreshAllModifiedAttributes()
{
List<string> modifiedAttributes = new List<string>();
modifiedAttributes.AddRange(numericChange.Select(kvp => kvp.Key));
modifiedAttributes.AddRange(percentageChangeOfAccumulation.Select(kvp => kvp.Key));
modifiedAttributes.AddRange(percentageChangeOfMultiplication.Select(kvp => kvp.Key));
if (_upgradeData != null)
{
modifiedAttributes.AddRange(_upgradeData.upgrades.Select(u => u.attributeKey));
}
modifiedAttributes = modifiedAttributes.Distinct().ToList();
modifiedAttributes.ForEach(attr => owner.player.attributeSm.RefreshAttribute(attr));
return modifiedAttributes;
}
}
}