Files
WulaFallenEmpireRW/Source/WulaFallenEmpire/HarmonyPatches/DamageShieldPatch.cs
2025-08-26 18:33:49 +08:00

40 lines
1.6 KiB
C#
Raw 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 HarmonyLib;
using Verse;
using System.Reflection;
using UnityEngine; // Add UnityEngine for MoteMaker and Color
namespace WulaFallenEmpire.HarmonyPatches
{
[HarmonyPatch(typeof(Pawn_HealthTracker), "PreApplyDamage")]
public static class DamageShieldPatch
{
// 使用 Harmony 的 AccessTools.Field 来获取私有的 pawn 字段
private static readonly FieldInfo PawnField = AccessTools.Field(typeof(Pawn_HealthTracker), "pawn");
public static bool Prefix(Pawn_HealthTracker __instance, ref DamageInfo dinfo, out bool absorbed)
{
// 获取 Pawn 实例
Pawn pawn = (Pawn)PawnField.GetValue(__instance);
// 查找 Pawn 身上是否有 Hediff_DamageShield
Hediff_DamageShield damageShield = pawn.health.hediffSet.GetFirstHediff<Hediff_DamageShield>();
if (damageShield != null && damageShield.ShieldCharges > 0)
{
// 如果有护盾层数,则消耗一层并抵挡伤害
damageShield.ShieldCharges--;
// MoteMaker.ThrowText(pawn.DrawPos, pawn.Map, "伤害被护盾抵挡!", Color.cyan, 1.2f); // 视觉反馈,明确指定 Verse.MoteMaker此行将被删除
// 设置 absorbed 为 true表示伤害被完全吸收
absorbed = true;
// 返回 false阻止原始方法执行即伤害不会被应用
return false;
}
// 如果没有护盾 Hediff 或者层数用尽,则正常处理伤害
absorbed = false;
return true; // 继续执行原始方法
}
}
}