This commit is contained in:
2025-09-04 22:21:01 +08:00
parent 3df5314d4e
commit 00d43cfba3
4 changed files with 27 additions and 24 deletions

Binary file not shown.

View File

@@ -24,9 +24,21 @@ namespace ArachnaeSwarm
// return; // return;
// } // }
Log.Message("[夺舍] Apply方法开始执行。施法者: " + caster.LabelShort + ", 目标: " + targetPawn.LabelShort);
Log.Message("[夺舍] 步骤1: 创建Hediff实例...");
Hediff_Possession hediff = (Hediff_Possession)HediffMaker.MakeHediff(HediffDef.Named("ARA_Possession"), targetPawn); Hediff_Possession hediff = (Hediff_Possession)HediffMaker.MakeHediff(HediffDef.Named("ARA_Possession"), targetPawn);
hediff.SetCaster(caster);
Log.Message("[夺舍] 步骤2: 将施法者 " + caster.LabelShort + " 安全地转移到Hediff容器中...");
// 使用标准的游戏框架方法将施法者从其当前的持有者地图转移到Hediff的内部容器中。
// 这会自动处理施法者从地图上消失的逻辑。
caster.holdingOwner.TryTransferToContainer(caster, hediff.GetDirectlyHeldThings(), 1);
Log.Message("[夺舍] 步骤3: 调用Hediff的PostPossessionApply方法来执行数据覆盖...");
// 我们创建一个新的方法来处理夺舍后的逻辑而不是依赖PostAdd
hediff.PostPossessionApply(caster, targetPawn);
Log.Message("[夺舍] 步骤4: 将Hediff添加到目标Pawn身上...");
targetPawn.health.AddHediff(hediff); targetPawn.health.AddHediff(hediff);
} }
} }

View File

@@ -28,27 +28,19 @@ namespace ArachnaeSwarm
return innerContainer; return innerContainer;
} }
public override void PostAdd(DamageInfo? dinfo) // PostAdd在游戏加载时也会被调用因此不适合放置一次性的夺舍逻辑。
// 我们创建一个新的公共方法由CompAbilityEffect在正确的时机调用。
public void PostPossessionApply(Pawn caster, Pawn target)
{ {
base.PostAdd(dinfo); Log.Message("[夺舍] Hediff.PostPossessionApply 开始执行,施法者: " + caster.LabelShort + ", 目标: " + target.LabelShort);
if (this.originalCaster == null)
{
Log.Error("Hediff_Possession was added without an original caster.");
return;
}
// 正确的做法:从原始施法者身上“分裂”出一个副本存入容器
// 这确保了容器里的是一个独立的对象,而不是一个会被销毁的引用
this.innerContainer.TryAdd(this.originalCaster.SplitOff(1), true);
PawnDataUtility.TransferSoul(this.originalCaster, this.pawn);
if (!this.originalCaster.Destroyed)
{
this.originalCaster.Destroy(DestroyMode.Vanish);
}
Log.Message($"{this.pawn.LabelShort} has been possessed by {StoredCasterPawn.LabelShort}!"); // 将施法者的引用保存下来,用于将来的重生逻辑
this.originalCaster = caster;
Log.Message("[夺舍] 步骤5: 调用 TransferSoul 将灵魂从 " + caster.LabelShort + " 转移到 " + target.LabelShort);
PawnDataUtility.TransferSoul(caster, target);
Log.Message($"[夺舍] {target.LabelShort} 已被 {StoredCasterPawn.LabelShort} 夺舍成功!");
} }
public override void Notify_PawnDied(DamageInfo? dinfo, Hediff culprit = null) public override void Notify_PawnDied(DamageInfo? dinfo, Hediff culprit = null)

View File

@@ -56,11 +56,10 @@ namespace ArachnaeSwarm
{ {
foreach (RecordDef recordDef in DefDatabase<RecordDef>.AllDefs) foreach (RecordDef recordDef in DefDatabase<RecordDef>.AllDefs)
{ {
// Correct way: Get value from source and add the difference to target.
// This effectively sets the value.
float sourceValue = soulSource.records.GetValue(recordDef); float sourceValue = soulSource.records.GetValue(recordDef);
float targetValue = bodyTarget.records.GetValue(recordDef); // For Time records, we must set them directly to avoid warnings.
bodyTarget.records.AddTo(recordDef, sourceValue - targetValue); // The AddTo(delta) trick works for all types and is the standard way to "set" a value.
bodyTarget.records.AddTo(recordDef, sourceValue - bodyTarget.records.GetValue(recordDef));
} }
} }