Files

297 lines
12 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using NUnit.Framework;
using SepCore.Battle;
using SepCore.CustomUtility;
using SepCore.Definition;
using SepCore.Entity;
using SepCore.UI;
using UnityEngine;
namespace VampireLike.Tests.Gameplay.EditMode
{
[TestFixture]
public class WeaponSynergyTests
{
[Test]
public void TagMilestoneReplacement_ReplacesLowerTierWithHigherTier()
{
var pipeline = new AttributePipeline();
pipeline.SetBaseValue(StatType.MeleeDamage, 10f);
var tier2 = new WeaponTagConfig(CreateBufferForTagConfig(101, (int)WeaponTag.Blade, 2, StatType.MeleeDamage, 1f, ModifierType.Flat));
var tier4 = new WeaponTagConfig(CreateBufferForTagConfig(102, (int)WeaponTag.Blade, 4, StatType.MeleeDamage, 3f, ModifierType.Flat));
// 模拟 2 级生效
for (int i = 0; i < tier2.StatModifierConfigs.Count; i++)
{
pipeline.AddModifier(tier2.StatModifierConfigs[i], tier2);
}
Assert.AreEqual(11f, pipeline.GetValue(StatType.MeleeDamage), 1e-4f);
// 升级到 4 级:卸载 tier2,挂载 tier4
pipeline.RemoveAllModifiersFromSource(tier2);
for (int i = 0; i < tier4.StatModifierConfigs.Count; i++)
{
pipeline.AddModifier(tier4.StatModifierConfigs[i], tier4);
}
// 验证不是 10 + 1 + 3 = 14,而是最高档生效 10 + 3 = 13
Assert.AreEqual(13f, pipeline.GetValue(StatType.MeleeDamage), 1e-4f);
// 降级回 2 级:卸载 tier4,重新挂载 tier2
pipeline.RemoveAllModifiersFromSource(tier4);
for (int i = 0; i < tier2.StatModifierConfigs.Count; i++)
{
pipeline.AddModifier(tier2.StatModifierConfigs[i], tier2);
}
Assert.AreEqual(11f, pipeline.GetValue(StatType.MeleeDamage), 1e-4f);
// 全部卸载
pipeline.RemoveAllModifiersFromSource(tier2);
Assert.AreEqual(10f, pipeline.GetValue(StatType.MeleeDamage), 1e-4f);
}
[Test]
public void ItemTooltipContext_ConstructsBasicAndDetailProperly()
{
var rawData = new ItemTooltipRawData
{
Index = 0,
ItemType = ItemType.Weapon,
Price = 100,
Rarity = Rarity.Blue,
TargetPos = new Vector3(100f, 200f, 0f),
LastWaveDamage = 361,
CurrentTagCounts = new Dictionary<WeaponTag, int>
{
{ WeaponTag.Gun, 6 }
}
};
var context = new ItemTooltipContext(rawData);
Assert.IsNotNull(context.BasicContext);
Assert.IsNotNull(context.DetailContext);
Assert.AreEqual(100, context.BasicContext.Price);
Assert.AreEqual(361, context.DetailContext.LastWaveDamage);
Assert.IsTrue(context.DetailContext.HasLastWaveDamage);
Assert.IsTrue(context.DetailContext.HasAnyContent);
}
[Test]
public void ItemTooltipContext_WithoutLastWaveDamage_HidesDamageCard()
{
var rawData = new ItemTooltipRawData
{
Index = 1,
ItemType = ItemType.Prop,
Price = 50,
Rarity = Rarity.White,
LastWaveDamage = 0 // 无上一轮伤害
};
var context = new ItemTooltipContext(rawData);
Assert.IsFalse(context.DetailContext.HasLastWaveDamage);
Assert.IsFalse(context.DetailContext.HasTagSynergy);
Assert.IsFalse(context.DetailContext.HasAnyContent);
}
[Test]
public void BatchUpdate_AtomicModifierReplacement_PreventsHealthClamping()
{
var pipeline = new AttributePipeline();
pipeline.SetBaseValue(StatType.MaxHp, 100f);
var tier1 = new WeaponTagConfig(CreateBufferForTagConfig(201, (int)WeaponTag.Primitive, 2, StatType.MaxHp, 3f, ModifierType.Flat));
var tier2 = new WeaponTagConfig(CreateBufferForTagConfig(202, (int)WeaponTag.Primitive, 3, StatType.MaxHp, 6f, ModifierType.Flat));
// 初始达成 2 件套:最大生命 100 + 3 = 103,当前生命满血 103
pipeline.AddModifier(tier1.StatModifierConfigs[0], tier1);
Assert.AreEqual(103f, pipeline.GetValue(StatType.MaxHp), 1e-4f);
float currentHealth = 103f;
int notificationCount = 0;
// 模拟 HealthComponent 的监听逻辑:当 MaxHp 发生变化时,如果当前生命大于新的上限,则裁剪到新的上限
pipeline.OnStatChanged += (statType, newValue) =>
{
if (statType == StatType.MaxHp)
{
notificationCount++;
if (currentHealth > newValue)
{
currentHealth = newValue;
}
}
};
// 羁绊换档 2→3 件(升级):使用 BeginBatchUpdate 原子性替换
using (pipeline.BeginBatchUpdate())
{
pipeline.RemoveAllModifiersFromSource(tier1);
pipeline.AddModifier(tier2.StatModifierConfigs[0], tier2);
}
// 验证:
// 1. 最终最大生命为 100 + 6 = 106
Assert.AreEqual(106f, pipeline.GetValue(StatType.MaxHp), 1e-4f);
// 2. 只触发了一次通知,直接通知 106
Assert.AreEqual(1, notificationCount);
// 3. 当前生命未被临时下降的 100 裁剪,仍为 103!
Assert.AreEqual(103f, currentHealth, 1e-4f);
}
[Test]
public void BuildTagSynergyInfo_MultiTagWeapon_RendersAllTagsAndTiers()
{
var configs = new List<WeaponTagConfig>
{
new(CreateBufferForTagConfig(301, (int)WeaponTag.Blade, 2, StatType.MeleeDamage, 2f, ModifierType.Flat)),
new(CreateBufferForTagConfig(302, (int)WeaponTag.Blade, 4, StatType.MeleeDamage, 6f, ModifierType.Flat)),
new(CreateBufferForTagConfig(303, (int)WeaponTag.Precise, 2, StatType.CritChance, 0.05f, ModifierType.Flat)),
new(CreateBufferForTagConfig(304, (int)WeaponTag.Precise, 3, StatType.CritChance, 0.10f, ModifierType.Flat))
};
var tags = new List<WeaponTag> { WeaponTag.Blade, WeaponTag.Precise };
var tagCounts = new Dictionary<WeaponTag, int>
{
{ WeaponTag.Blade, 2 },
{ WeaponTag.Precise, 2 }
};
SepCore.CustomUtility.ItemDescUtility.BuildTagSynergyInfo(tags, tagCounts, configs, out string tagTitle, out string tagTiersContent);
// 双标签时,tagTitle 应留空由 tagTiersContent 渲染各标签标题
Assert.IsEmpty(tagTitle);
Assert.IsNotEmpty(tagTiersContent);
// 验证同时包含了 刀剑 和 精准 两个标签的标题
Assert.IsTrue(tagTiersContent.Contains("刀剑"));
Assert.IsTrue(tagTiersContent.Contains("精准"));
// 验证两者的 2 阶档位均处于激活高亮状态(包含绿色高亮标签 #38E038)
Assert.IsTrue(tagTiersContent.Contains("#38E038"));
Assert.IsTrue(tagTiersContent.Contains("(2)"));
Assert.IsTrue(tagTiersContent.Contains("(4)"));
Assert.IsTrue(tagTiersContent.Contains("(3)"));
}
[Test]
public void WeaponUtility_TryGetNextTierWeaponId_CalculatesAndStopsAtRed()
{
// 1001 = WeaponKnife White -> 1002 Blue -> 1003 Purple -> 1004 Red -> Stops
Assert.IsTrue(WeaponUtility.TryGetNextTierWeaponId(1001, out int next1));
Assert.AreEqual(1002, next1);
Assert.IsTrue(WeaponUtility.TryGetNextTierWeaponId(1002, out int next2));
Assert.AreEqual(1003, next2);
Assert.IsTrue(WeaponUtility.TryGetNextTierWeaponId(1003, out int next3));
Assert.AreEqual(1004, next3);
// Red (4) 无法再升阶
Assert.IsFalse(WeaponUtility.TryGetNextTierWeaponId(1004, out int next4));
Assert.AreEqual(0, next4);
}
[Test]
public void ItemTooltipContext_CanCombineFlag_ControlsShowCombineButton()
{
var rawDataCombinable = new ItemTooltipRawData
{
Index = 0,
ItemType = ItemType.Weapon,
Price = 100,
Rarity = Rarity.White,
CanCombine = true
};
var contextCombinable = new ItemTooltipContext(rawDataCombinable);
Assert.IsTrue(contextCombinable.BasicContext.ShowCombineButton);
var rawDataNonCombinable = new ItemTooltipRawData
{
Index = 0,
ItemType = ItemType.Weapon,
Price = 100,
Rarity = Rarity.Red,
CanCombine = false
};
var contextNonCombinable = new ItemTooltipContext(rawDataNonCombinable);
Assert.IsFalse(contextNonCombinable.BasicContext.ShowCombineButton);
}
[Test]
public void WeaponSynthesis_NestedBatchUpdate_AtomicallyReplacesMilestonesWithoutIntermediateDip()
{
var pipeline = new AttributePipeline();
pipeline.SetBaseValue(StatType.MaxHp, 100f);
var tagTier1 = new WeaponTagConfig(CreateBufferForTagConfig(401, (int)WeaponTag.Primitive, 2, StatType.MaxHp, 5f, ModifierType.Flat));
var tagTier2 = new WeaponTagConfig(CreateBufferForTagConfig(402, (int)WeaponTag.Primitive, 4, StatType.MaxHp, 12f, ModifierType.Flat));
// 初始 4 件达成 2 档:100 + 12 = 112 HP
pipeline.AddModifier(tagTier2.StatModifierConfigs[0], tagTier2);
Assert.AreEqual(112f, pipeline.GetValue(StatType.MaxHp), 1e-4f);
float currentHealth = 112f;
int notificationCount = 0;
pipeline.OnStatChanged += (statType, newValue) =>
{
if (statType == StatType.MaxHp)
{
notificationCount++;
if (currentHealth > newValue)
{
currentHealth = newValue;
}
}
};
// 模拟合成流程:外层 BeginBatchUpdate 包裹
// 合成两把武器使得件数从 4 变 3(降至 1 档 5 HP),期间移除 4 档、甚至中间可能短暂降至 0 档
using (pipeline.BeginBatchUpdate())
{
// 模拟第一把武器移除时的内部刷新(嵌套 BatchUpdate)
using (pipeline.BeginBatchUpdate())
{
pipeline.RemoveAllModifiersFromSource(tagTier2);
}
// 此时外层尚未结束,不应触发任何 OnStatChanged,当前血量不应被扣减
Assert.AreEqual(0, notificationCount);
Assert.AreEqual(112f, currentHealth);
// 模拟新合成武器挂载后的内部刷新(嵌套 BatchUpdate)
using (pipeline.BeginBatchUpdate())
{
pipeline.AddModifier(tagTier1.StatModifierConfigs[0], tagTier1);
}
Assert.AreEqual(0, notificationCount);
}
// 外层作用域结束后,统一结算一次变化
Assert.AreEqual(1, notificationCount);
// 最终生命上限为 100 + 5 = 105
Assert.AreEqual(105f, pipeline.GetValue(StatType.MaxHp), 1e-4f);
// 当前生命平滑裁剪至 105,而不是在中间被 100 裁剪
Assert.AreEqual(105f, currentHealth, 1e-4f);
}
private static Luban.ByteBuf CreateBufferForTagConfig(int id, int tag, int requiredCount, StatType statType, float value, ModifierType modType)
{
var buf = new Luban.ByteBuf();
buf.WriteInt(id);
buf.WriteInt(tag);
buf.WriteInt(requiredCount);
buf.WriteSize(1); // 1 modifier
buf.WriteInt((int)statType);
buf.WriteFloat(value);
buf.WriteInt((int)modType);
return buf;
}
}
}