Files
ArachnaeSwarm/Source/ArachnaeSwarm/ARA_HuggingFace/CompAbilityEffect_Possess.cs
2025-09-05 12:52:17 +08:00

44 lines
1.7 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 RimWorld;
using Verse;
namespace ArachnaeSwarm
{
public class CompAbilityEffect_Possess : CompAbilityEffect
{
public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
{
base.Apply(target, dest);
Pawn caster = this.parent.pawn;
Pawn targetPawn = target.Pawn;
if (targetPawn == null || caster == null) return;
Log.Message($"[夺舍] 开始执行。施法者: {caster.LabelShort}, 目标: {targetPawn.LabelShort}");
// 步骤 1: 创建Hediff实例
Hediff_Possession hediff = (Hediff_Possession)HediffMaker.MakeHediff(HediffDef.Named("ARA_Possession"), targetPawn);
// 步骤 2: 使用SplitOff(1)将施法者的一个安全、独立的副本存入容器。
// 这是从地图上移除Pawn并将其存入容器的标准、原子性操作。
if (hediff.GetDirectlyHeldThings().TryAdd(caster.SplitOff(1), true))
{
Log.Message($"[夺舍] 成功将 {caster.LabelShort} 的副本存入Hediff。");
}
else
{
Log.Error($"[夺舍] 无法将 {caster.LabelShort} 的副本存入Hediff。中止操作。");
return;
}
// 步骤 3: 使用原始施法者的数据覆盖目标Pawn。
// 即使caster的stackCount变为0其数据在当前Tick中依然可读。
PawnDataUtility.TransferSoul(caster, targetPawn);
// 步骤 4: 将准备好的Hediff添加到目标身上。
targetPawn.health.AddHediff(hediff);
Log.Message($"[夺舍] {targetPawn.LabelShort} (原 {caster.LabelShort}) 夺舍完成。");
}
}
}