Files
WulaFallenEmpireRW/Source/WulaFallenEmpire/Pawn_Comps/HediffGiver/CompHediffGiver.cs
2026-02-25 17:30:59 +08:00

71 lines
2.2 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 System;
using System.Collections.Generic;
using Verse;
using RimWorld;
namespace WulaFallenEmpire
{
public class CompHediffGiver : ThingComp
{
private bool hediffsApplied = false; // 新增标记是否已经应用过hediff
public CompProperties_HediffGiver Props => (CompProperties_HediffGiver)this.props;
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
// 只有当thing是pawn时才添加hediff
if (this.parent is Pawn pawn)
{
// 新增检查是否已经应用过hediff或者是否是读档
if (!hediffsApplied && !respawningAfterLoad)
{
AddHediffsToPawn(pawn);
hediffsApplied = true; // 标记为已应用
}
}
}
private void AddHediffsToPawn(Pawn pawn)
{
// 检查是否有hediff列表
if (Props.hediffs == null || Props.hediffs.Count == 0)
return;
// 检查概率
if (Props.addChance < 1.0f && Rand.Value > Props.addChance)
return;
// 为每个hediff添加到pawn
foreach (HediffDef hediffDef in Props.hediffs)
{
// 检查是否允许重复添加
if (!Props.allowDuplicates && pawn.health.hediffSet.HasHediff(hediffDef))
continue;
// 添加hediff
pawn.health.AddHediff(hediffDef);
}
}
// 新增序列化hediffsApplied标记
public override void PostExposeData()
{
base.PostExposeData();
Scribe_Values.Look(ref hediffsApplied, "hediffsApplied", false);
}
// 新增调试方法用于手动触发hediff添加仅开发模式
public void DebugApplyHediffs()
{
if (this.parent is Pawn pawn && !hediffsApplied)
{
AddHediffsToPawn(pawn);
hediffsApplied = true;
WulaLog.Debug($"Debug: Applied hediffs to {pawn.Label}");
}
}
}
}