1
This commit is contained in:
151
Source/ArachnaeSwarm/Pawn_Comps/ARA_UniquePawn/CompUniquePawn.cs
Normal file
151
Source/ArachnaeSwarm/Pawn_Comps/ARA_UniquePawn/CompUniquePawn.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using RimWorld;
|
||||
using Verse;
|
||||
|
||||
namespace ArachnaeSwarm
|
||||
{
|
||||
public class CompUniquePawn : ThingComp
|
||||
{
|
||||
private bool _checked = false;
|
||||
private bool _scheduledForCheck = false;
|
||||
|
||||
public CompProperties_UniquePawn Props => (CompProperties_UniquePawn)this.props;
|
||||
|
||||
public override void PostSpawnSetup(bool respawningAfterLoad)
|
||||
{
|
||||
base.PostSpawnSetup(respawningAfterLoad);
|
||||
|
||||
// 基本安全检查
|
||||
if (this.parent == null || !(this.parent is Pawn pawn) || !pawn.Spawned)
|
||||
return;
|
||||
|
||||
// 只在首次生成时检查,不是加载存档时
|
||||
if (respawningAfterLoad || _checked || _scheduledForCheck)
|
||||
return;
|
||||
|
||||
// 确保属性有效
|
||||
if (this.props == null || string.IsNullOrEmpty(Props?.globalVariable))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
// 延迟整个检查过程到下一帧
|
||||
_scheduledForCheck = true;
|
||||
|
||||
LongEventHandler.QueueLongEvent(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (this.parent is Pawn delayedPawn && delayedPawn.Spawned && !_checked)
|
||||
{
|
||||
CheckAndHandleUniquePawn(delayedPawn);
|
||||
_checked = true;
|
||||
}
|
||||
_scheduledForCheck = false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Error in delayed unique pawn check: {ex}");
|
||||
_scheduledForCheck = false;
|
||||
}
|
||||
}, "ArachnaeSwarm_UniquePawnCheck", false, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Error in CompUniquePawn.PostSpawnSetup: {ex}");
|
||||
_scheduledForCheck = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckAndHandleUniquePawn(Pawn pawn)
|
||||
{
|
||||
try
|
||||
{
|
||||
string variable = Props.globalVariable;
|
||||
|
||||
if (string.IsNullOrEmpty(variable))
|
||||
{
|
||||
Log.Error("CompUniquePawn: globalVariable is null or empty");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查变量是否已存在
|
||||
if (GlobalVariableManager.HasVariable(variable))
|
||||
{
|
||||
// 变量已存在,杀死pawn
|
||||
KillPawn(pawn, variable);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 变量不存在,添加变量
|
||||
GlobalVariableManager.SetVariable(variable);
|
||||
|
||||
if (Prefs.DevMode)
|
||||
{
|
||||
Log.Message($"Added global variable '{variable}' for pawn {pawn.Label}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Error in CheckAndHandleUniquePawn: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
private void KillPawn(Pawn pawn, string variable)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 显示死亡消息
|
||||
if (Props.showDeathMessage && !string.IsNullOrEmpty(Props.deathMessageKey))
|
||||
{
|
||||
string deathMessage = Props.deathMessageKey.Translate(pawn.Label, variable);
|
||||
if (!string.IsNullOrEmpty(deathMessage))
|
||||
{
|
||||
Messages.Message(deathMessage, MessageTypeDefOf.NegativeEvent);
|
||||
}
|
||||
}
|
||||
|
||||
if (Prefs.DevMode)
|
||||
{
|
||||
Log.Message($"Killing pawn {pawn.Label} because global variable '{variable}' already exists");
|
||||
}
|
||||
|
||||
// 使用更安全的延迟执行
|
||||
LongEventHandler.QueueLongEvent(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (pawn != null && pawn.Spawned && !pawn.Destroyed)
|
||||
{
|
||||
if (Props.killDamageDef != null)
|
||||
{
|
||||
var damageInfo = new DamageInfo(Props.killDamageDef, 99999f, -1f, -1f);
|
||||
pawn.TakeDamage(damageInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
pawn.Kill(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Error in delayed pawn kill: {ex}");
|
||||
}
|
||||
}, "ArachnaeSwarm_KillDuplicatePawn", false, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Error in KillPawn: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void PostExposeData()
|
||||
{
|
||||
base.PostExposeData();
|
||||
Scribe_Values.Look(ref _checked, "checked", false);
|
||||
Scribe_Values.Look(ref _scheduledForCheck, "scheduledForCheck", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user