Files

93 lines
3.1 KiB
C#

using System.Collections.Generic;
using SepCore.Definition;
using UnityEngine;
namespace SepCore.UI
{
public class StatContext : UIContext
{
public StatRawData RawData { get; }
public Dictionary<StatType, string> FormattedValues { get; } = new Dictionary<StatType, string>();
public StatContext()
{
}
public StatContext(StatRawData rawData)
{
RawData = rawData;
if (rawData != null)
{
foreach (KeyValuePair<StatType, float> pair in rawData.Values)
{
FormattedValues[pair.Key] = FormatStatValue(pair.Key, pair.Value);
}
}
}
public string GetFormattedValue(StatType statType)
{
return FormattedValues.TryGetValue(statType, out string text) ? text : string.Empty;
}
public void SetFormattedValue(StatType statType, string text)
{
FormattedValues[statType] = text;
}
public static string FormatStatValue(StatType statType, float value)
{
StatConfig config = (GameEntry.Luban != null && GameEntry.Luban.IsReady)
? GameEntry.Luban.Get<StatConfig>((int)statType)
: null;
float baseValue = config?.DefaultBaseValue ?? (statType is StatType.DamagePercent or StatType.AttackSpeed ? 1f : 0f);
bool isPercent = config?.IsPercentDisplay ?? (statType is StatType.DamagePercent or StatType.AttackSpeed or StatType.CritChance or StatType.Dodge or StatType.LifeSteal);
string colorTag = null;
if (value > baseValue + 1e-4f)
{
colorTag = "green";
}
else if (value < baseValue - 1e-4f)
{
colorTag = "red";
}
string text;
if (statType is StatType.DamagePercent or StatType.AttackSpeed)
{
// 增伤与攻速基准为 1.0(即 0% 加成)。面板显示相对基准的增量,如 0%、+5%、-10%
float bonusPercent = (value - 1f) * 100f;
string sign = bonusPercent > 0 ? "+" : "";
text = $"{sign}{bonusPercent:0.##}%";
}
else if (isPercent || statType == StatType.LifeSteal)
{
// 概率/比率属性(暴击率、闪避率、生命窃取):以百分比形式呈现
float displayPercent = value * 100f;
string sign = (value > baseValue && baseValue != 0) ? "+" : "";
text = $"{sign}{displayPercent:0.##}%";
}
else
{
if (Mathf.Approximately(value, Mathf.Round(value)))
{
text = Mathf.RoundToInt(value).ToString();
}
else
{
text = value.ToString("0.##");
}
}
if (!string.IsNullOrEmpty(colorTag))
{
return $"<color={colorTag}>{text}</color>";
}
return text;
}
}
}