397 lines
14 KiB
C#
397 lines
14 KiB
C#
using System.Collections.Generic;
|
||
using System.Text;
|
||
using SepCore.Definition;
|
||
using SepCore.Entity;
|
||
using UnityGameFramework.Runtime;
|
||
|
||
namespace SepCore.CustomUtility
|
||
{
|
||
public static class ItemDescUtility
|
||
{
|
||
private static readonly string[] _statTypeNames =
|
||
{
|
||
"无效", // None = 0
|
||
"最大生命", // MaxHp = 1
|
||
"生命恢复", // HpRegen = 2
|
||
"生命窃取", // LifeSteal = 3
|
||
"伤害加成", // DamagePercent = 4
|
||
"近战伤害", // MeleeDamage = 5
|
||
"远程伤害", // RangedDamage = 6
|
||
"元素伤害", // ElementalDamage = 7
|
||
"攻击速度", // AttackSpeed = 8
|
||
"暴击率", // CritChance = 9
|
||
"工程学", // Engineering = 10
|
||
"攻击范围", // Range = 11
|
||
"护甲", // Armor = 12
|
||
"闪避率", // Dodge = 13
|
||
"移动速度", // MoveSpeed = 14
|
||
"收获", // Harvesting = 15
|
||
"幸运" // Luck = 16
|
||
};
|
||
|
||
private static readonly Dictionary<WeaponTag, string> _weaponTagNames = new()
|
||
{
|
||
{ WeaponTag.Blade, "刀剑" },
|
||
{ WeaponTag.Precise, "精准" },
|
||
{ WeaponTag.Gun, "枪械" },
|
||
{ WeaponTag.Engineering, "工程" },
|
||
{ WeaponTag.Blunt, "钝器" },
|
||
{ WeaponTag.Primitive, "原始" },
|
||
{ WeaponTag.Elemental, "元素" },
|
||
{ WeaponTag.Unarmed, "徒手" },
|
||
{ WeaponTag.Medical, "医疗" },
|
||
{ WeaponTag.Heavy, "重型" },
|
||
};
|
||
|
||
public static string GetWeaponTagName(WeaponTag tag)
|
||
{
|
||
return _weaponTagNames.TryGetValue(tag, out string name) ? name : tag.ToString();
|
||
}
|
||
|
||
public static string FormatWeaponTags(IReadOnlyList<WeaponTag> tags)
|
||
{
|
||
if (tags == null || tags.Count == 0)
|
||
{
|
||
return "武器";
|
||
}
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < tags.Count; i++)
|
||
{
|
||
WeaponTag tag = tags[i];
|
||
if (tag == WeaponTag.None)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
if (sb.Length > 0)
|
||
{
|
||
sb.Append(" - ");
|
||
}
|
||
|
||
sb.Append(GetWeaponTagName(tag));
|
||
}
|
||
|
||
return sb.Length > 0 ? sb.ToString() : "武器";
|
||
}
|
||
|
||
public static string FormatWeaponTags(WeaponConfig config)
|
||
{
|
||
return FormatWeaponTags(config.Tags);
|
||
}
|
||
|
||
public static string FormatWeaponTags(WeaponData data)
|
||
{
|
||
if (data == null)
|
||
{
|
||
Log.Error("Weapon data is invalid.");
|
||
return "武器";
|
||
}
|
||
|
||
return FormatWeaponTags(data.Tags);
|
||
}
|
||
|
||
public static string Describe(StatModifierConfig statModifierConfig)
|
||
{
|
||
if (statModifierConfig == null)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
int index = (int)statModifierConfig.StatType;
|
||
string name = (index >= 0 && index < _statTypeNames.Length) ? _statTypeNames[index] : statModifierConfig.StatType.ToString();
|
||
string colorTag = statModifierConfig.Value >= 0 ? "green" : "red";
|
||
string sign = statModifierConfig.Value >= 0 ? "+" : "";
|
||
|
||
StatConfig statConfig = null;
|
||
if (GameEntry.Luban != null && GameEntry.Luban.IsReady)
|
||
{
|
||
statConfig = GameEntry.Luban.Get<StatConfig>(index);
|
||
}
|
||
|
||
if (statConfig != null && !string.IsNullOrEmpty(statConfig.Name))
|
||
{
|
||
name = statConfig.Name;
|
||
}
|
||
|
||
// 是否以百分比展示:由 StatConfig.IsPercentDisplay 真正驱动(Luban 未就绪时 fallback 到已知百分比属性)
|
||
bool isPercentDisplay = statConfig != null
|
||
? statConfig.IsPercentDisplay
|
||
: (statModifierConfig.StatType is StatType.DamagePercent or StatType.AttackSpeed or StatType.CritChance or StatType.Dodge);
|
||
|
||
// 1. 修饰类型判断
|
||
bool isPercentAdd = statModifierConfig.ModifierType == ModifierType.PercentAdd;
|
||
bool isPercentMult = statModifierConfig.ModifierType == ModifierType.PercentMult;
|
||
bool isFlat = statModifierConfig.ModifierType == ModifierType.Flat;
|
||
|
||
// 2. UI 百分比展示裁决:
|
||
// - 任意属性的 PercentAdd 词条属于比例加成(如 攻速 +19%、伤害 +5%、移速 +15%)
|
||
// - 或属性自身在配置表中标记为百分比显示(IsPercentDisplay 为 true,如暴击率、闪避率等),其 Flat 加成应以百分比呈现(如 0.1 -> +10%)
|
||
bool shouldDisplayAsPercent = isPercentAdd || (isFlat && isPercentDisplay);
|
||
|
||
if (shouldDisplayAsPercent)
|
||
{
|
||
float displayPercent = statModifierConfig.Value * 100f;
|
||
return $"{name}: <color={colorTag}>{sign}{displayPercent:0.##}%</color>";
|
||
}
|
||
|
||
if (isPercentMult)
|
||
{
|
||
return $"{name}: <color={colorTag}>x{statModifierConfig.Value:0.##}</color>";
|
||
}
|
||
|
||
return $"{name}: <color={colorTag}>{sign}{statModifierConfig.Value:0.##}</color>";
|
||
}
|
||
|
||
public static string CreatePropDescription(IEnumerable<StatModifierConfig> modifiers)
|
||
{
|
||
if (modifiers == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
foreach (StatModifierConfig modifier in modifiers)
|
||
{
|
||
sb.Append(Describe(modifier));
|
||
sb.Append('\n');
|
||
}
|
||
|
||
return sb.ToString();
|
||
}
|
||
|
||
public static string CreateWeaponDescription(WeaponConfig weaponConfig)
|
||
{
|
||
return CreateWeaponDescription(weaponConfig.Attack, weaponConfig.Cooldown,
|
||
weaponConfig.AttackRange, weaponConfig.StatModifierConfigs);
|
||
}
|
||
|
||
public static string CreateWeaponDescription(WeaponData weaponData)
|
||
{
|
||
if (weaponData == null)
|
||
{
|
||
Log.Error("Weapon data is invalid.");
|
||
return string.Empty;
|
||
}
|
||
|
||
return CreateWeaponDescription(weaponData.Attack, weaponData.Cooldown,
|
||
weaponData.AttackRange, weaponData.Modifiers);
|
||
}
|
||
|
||
public static string CreatePropDescription(PropItem prop)
|
||
{
|
||
if (prop == null)
|
||
{
|
||
Log.Error("Prop is invalid.");
|
||
return string.Empty;
|
||
}
|
||
|
||
return CreatePropDescription(prop.Modifiers);
|
||
}
|
||
|
||
public static string CreateWeaponDescription(int attack, float cooldown, float attackRange,
|
||
IEnumerable<StatModifierConfig> modifiers = null)
|
||
{
|
||
float baseCrit = GameEntry.Luban.Global.WeaponBaseCrit;
|
||
float critMult = GameEntry.Luban.Global.CritMultiplier;
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.AppendLine($"伤害: {attack}");
|
||
sb.AppendLine($"暴击: x{critMult:0.##} ({baseCrit * 100:0.##}%)");
|
||
sb.AppendLine($"冷却: {cooldown:0.##}s");
|
||
sb.AppendLine($"范围: {attackRange:0.##}");
|
||
|
||
string modifiersDesc = CreatePropDescription(modifiers);
|
||
if (!string.IsNullOrEmpty(modifiersDesc))
|
||
{
|
||
sb.AppendLine();
|
||
sb.Append(modifiersDesc.TrimEnd());
|
||
}
|
||
|
||
return sb.ToString().TrimEnd();
|
||
}
|
||
|
||
public static void BuildTagSynergyInfo(WeaponData weaponData, IReadOnlyDictionary<WeaponTag, int> tagCounts,
|
||
out string tagTitle, out string tagTiersContent)
|
||
{
|
||
if (weaponData == null || weaponData.Tags == null || weaponData.Tags.Count == 0)
|
||
{
|
||
tagTitle = string.Empty;
|
||
tagTiersContent = string.Empty;
|
||
return;
|
||
}
|
||
|
||
BuildTagSynergyInfo(weaponData.Tags, tagCounts, out tagTitle, out tagTiersContent);
|
||
}
|
||
|
||
public static void BuildTagSynergyInfo(IReadOnlyList<WeaponTag> tags, IReadOnlyDictionary<WeaponTag, int> tagCounts,
|
||
out string tagTitle, out string tagTiersContent)
|
||
{
|
||
if (GameEntry.Luban == null || !GameEntry.Luban.IsReady)
|
||
{
|
||
tagTitle = string.Empty;
|
||
tagTiersContent = string.Empty;
|
||
return;
|
||
}
|
||
|
||
var allTagConfigs = GameEntry.Luban.GetTable<WeaponTagConfig>();
|
||
BuildTagSynergyInfo(tags, tagCounts, allTagConfigs, out tagTitle, out tagTiersContent);
|
||
}
|
||
|
||
public static void BuildTagSynergyInfo(IReadOnlyList<WeaponTag> tags, IReadOnlyDictionary<WeaponTag, int> tagCounts,
|
||
IReadOnlyList<WeaponTagConfig> allTagConfigs, out string tagTitle, out string tagTiersContent)
|
||
{
|
||
if (tags == null || tags.Count == 0 || allTagConfigs == null || allTagConfigs.Count == 0)
|
||
{
|
||
tagTitle = string.Empty;
|
||
tagTiersContent = string.Empty;
|
||
return;
|
||
}
|
||
|
||
List<WeaponTag> validTags = new List<WeaponTag>();
|
||
for (int i = 0; i < tags.Count; i++)
|
||
{
|
||
WeaponTag tag = tags[i];
|
||
if (tag != WeaponTag.None && !validTags.Contains(tag))
|
||
{
|
||
validTags.Add(tag);
|
||
}
|
||
}
|
||
|
||
if (validTags.Count == 0)
|
||
{
|
||
tagTitle = string.Empty;
|
||
tagTiersContent = string.Empty;
|
||
return;
|
||
}
|
||
|
||
if (validTags.Count == 1)
|
||
{
|
||
WeaponTag singleTag = validTags[0];
|
||
string tiers = BuildSingleTagTiers(singleTag, tagCounts, allTagConfigs);
|
||
if (string.IsNullOrEmpty(tiers))
|
||
{
|
||
tagTitle = string.Empty;
|
||
tagTiersContent = string.Empty;
|
||
return;
|
||
}
|
||
|
||
tagTitle = GetWeaponTagName(singleTag);
|
||
tagTiersContent = tiers;
|
||
}
|
||
else
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int t = 0; t < validTags.Count; t++)
|
||
{
|
||
WeaponTag tag = validTags[t];
|
||
string tiers = BuildSingleTagTiers(tag, tagCounts, allTagConfigs);
|
||
if (string.IsNullOrEmpty(tiers))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
if (sb.Length > 0)
|
||
{
|
||
sb.AppendLine();
|
||
}
|
||
|
||
sb.AppendLine($"<b><size=117%><color=#F5F5F5>{GetWeaponTagName(tag)}</color></size></b>");
|
||
sb.AppendLine(tiers);
|
||
}
|
||
|
||
tagTitle = string.Empty;
|
||
tagTiersContent = sb.ToString().TrimEnd();
|
||
}
|
||
}
|
||
|
||
private static string BuildSingleTagTiers(WeaponTag tag, IReadOnlyDictionary<WeaponTag, int> tagCounts,
|
||
IReadOnlyList<WeaponTagConfig> allTagConfigs)
|
||
{
|
||
int currentCount = tagCounts != null && tagCounts.TryGetValue(tag, out int c) ? c : 0;
|
||
List<WeaponTagConfig> tiers = new List<WeaponTagConfig>();
|
||
for (int i = 0; i < allTagConfigs.Count; i++)
|
||
{
|
||
WeaponTagConfig cfg = allTagConfigs[i];
|
||
if (cfg.Tag == tag)
|
||
{
|
||
tiers.Add(cfg);
|
||
}
|
||
}
|
||
|
||
if (tiers.Count == 0)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
tiers.Sort((a, b) => a.RequiredCount.CompareTo(b.RequiredCount));
|
||
|
||
int activeCount = -1;
|
||
for (int i = tiers.Count - 1; i >= 0; i--)
|
||
{
|
||
if (currentCount >= tiers[i].RequiredCount)
|
||
{
|
||
activeCount = tiers[i].RequiredCount;
|
||
break;
|
||
}
|
||
}
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
for (int i = 0; i < tiers.Count; i++)
|
||
{
|
||
WeaponTagConfig tier = tiers[i];
|
||
bool isActive = tier.RequiredCount == activeCount;
|
||
|
||
StringBuilder modSb = new StringBuilder();
|
||
if (tier.StatModifierConfigs != null)
|
||
{
|
||
for (int m = 0; m < tier.StatModifierConfigs.Count; m++)
|
||
{
|
||
StatModifierConfig mod = tier.StatModifierConfigs[m];
|
||
if (modSb.Length > 0)
|
||
{
|
||
modSb.Append(", ");
|
||
}
|
||
|
||
int statIdx = (int)mod.StatType;
|
||
string statName = (statIdx >= 0 && statIdx < _statTypeNames.Length)
|
||
? _statTypeNames[statIdx]
|
||
: mod.StatType.ToString();
|
||
|
||
StatConfig statConfig = (GameEntry.Luban != null && GameEntry.Luban.IsReady)
|
||
? GameEntry.Luban.Get<StatConfig>(statIdx)
|
||
: null;
|
||
if (statConfig != null && !string.IsNullOrEmpty(statConfig.Name))
|
||
{
|
||
statName = statConfig.Name;
|
||
}
|
||
|
||
string sign = mod.Value >= 0 ? "+" : "";
|
||
bool isPercent = statConfig != null && statConfig.IsPercentDisplay;
|
||
if (isPercent || mod.ModifierType == ModifierType.PercentAdd)
|
||
{
|
||
modSb.Append($"{sign}{mod.Value * 100:0.##}% {statName}");
|
||
}
|
||
else
|
||
{
|
||
modSb.Append($"{sign}{mod.Value:0.##} {statName}");
|
||
}
|
||
}
|
||
}
|
||
|
||
string tierText = modSb.ToString();
|
||
if (isActive)
|
||
{
|
||
sb.AppendLine($"<b><color=#38E038>({tier.RequiredCount}) {tierText}</color></b>");
|
||
}
|
||
else
|
||
{
|
||
sb.AppendLine($"<color=#707070>({tier.RequiredCount}) {tierText}</color>");
|
||
}
|
||
}
|
||
|
||
return sb.ToString().TrimEnd();
|
||
}
|
||
}
|
||
}
|