Files
Cielonos/Assets/Scripts/MainGame/Items/Submodules/AttributeSubmodule.cs

83 lines
3.8 KiB
C#
Raw Normal View History

2026-01-03 18:19:39 -05:00
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
2026-03-20 12:07:44 -04:00
namespace Cielonos.MainGame.Characters.Inventory
2026-01-03 18:19:39 -05:00
{
public class AttributeSubmodule : SubmoduleBase<ItemBase>
{
2026-04-18 13:57:19 -04:00
public int level;
public AttributeGroup attributeGroup;
2026-01-03 18:19:39 -05:00
public Dictionary<string, float> numericChange;
public Dictionary<string, float> percentageChangeOfAccumulation;
public Dictionary<string, float> percentageChangeOfMultiplication;
2026-04-18 13:57:19 -04:00
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;
}
2026-01-03 18:19:39 -05:00
2026-04-18 13:57:19 -04:00
public AttributeSubmodule(ItemBase owner, AttributeData data, UpgradeData upgradeData = null) : base(owner)
2026-01-03 18:19:39 -05:00
{
2026-04-18 13:57:19 -04:00
this.attributeGroup = new AttributeGroup(data.itemAttributes.ToDictionary());
2026-02-13 09:22:11 -05:00
this.numericChange = new Dictionary<string, float>(data.chaAttrNumericChange);
this.percentageChangeOfAccumulation = new Dictionary<string, float>(data.chaAttrPercentageChangeOfAccumulation);
this.percentageChangeOfMultiplication = new Dictionary<string, float>(data.chaAttrPercentageChangeOfMultiplication);
2026-04-18 13:57:19 -04:00
this._upgradeData = upgradeData;
2026-01-03 18:19:39 -05:00
}
public void GetAttributeChanges(string attributeName, out float numeric, out float pAccumulation, out float pMultiplication)
{
2026-04-18 13:57:19 -04:00
numeric = numericChange.GetValueOrDefault(attributeName, 0f) + GetUpgradeNumericValue(attributeName);
2026-01-03 18:19:39 -05:00
pAccumulation = percentageChangeOfAccumulation.GetValueOrDefault(attributeName, 0f);
pMultiplication = percentageChangeOfMultiplication.GetValueOrDefault(attributeName, 1f);
}
public void ApplyAttributeChanges(string attributeName, ref float numeric, ref float pAccumulation, ref float pMultiplication)
{
2026-04-18 13:57:19 -04:00
numeric += numericChange.GetValueOrDefault(attributeName, 0f) + GetUpgradeNumericValue(attributeName);
2026-01-03 18:19:39 -05:00
pAccumulation += percentageChangeOfAccumulation.GetValueOrDefault(attributeName, 0f);
pMultiplication *= percentageChangeOfMultiplication.GetValueOrDefault(attributeName, 1f);
}
2026-04-18 13:57:19 -04:00
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;
}
2026-01-03 18:19:39 -05:00
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));
2026-04-18 13:57:19 -04:00
if (_upgradeData != null)
{
modifiedAttributes.AddRange(_upgradeData.upgrades.Select(u => u.attributeKey));
}
modifiedAttributes = modifiedAttributes.Distinct().ToList();
2026-01-03 18:19:39 -05:00
modifiedAttributes.ForEach(attr => owner.player.attributeSm.RefreshAttribute(attr));
return modifiedAttributes;
}
}
}