106 lines
3.7 KiB
C#
106 lines
3.7 KiB
C#
using RimWorld;
|
||
using Verse;
|
||
|
||
namespace ArachnaeSwarm
|
||
{
|
||
public class Building_Morphable : Building
|
||
{
|
||
private CompMorphable compMorphable;
|
||
|
||
public override void SpawnSetup(Map map, bool respawningAfterLoad)
|
||
{
|
||
base.SpawnSetup(map, respawningAfterLoad);
|
||
this.compMorphable = GetComp<CompMorphable>();
|
||
}
|
||
|
||
public override string Label
|
||
{
|
||
get
|
||
{
|
||
if (compMorphable?.StoredPawn != null)
|
||
{
|
||
return $"{base.Label} ({compMorphable.StoredPawn.LabelShort})";
|
||
}
|
||
return base.Label;
|
||
}
|
||
}
|
||
|
||
public override string GetInspectString()
|
||
{
|
||
string text = base.GetInspectString();
|
||
if (compMorphable?.StoredPawn != null)
|
||
{
|
||
if (!text.NullOrEmpty())
|
||
{
|
||
text += "\n";
|
||
}
|
||
text += "StoredPawn".Translate() + ": " + compMorphable.StoredPawn.LabelShort;
|
||
}
|
||
return text;
|
||
}
|
||
|
||
public override void PreApplyDamage(ref DamageInfo dinfo, out bool absorbed)
|
||
{
|
||
// 先让基类处理,我们不打断正常流程
|
||
base.PreApplyDamage(ref dinfo, out absorbed);
|
||
if (absorbed)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 如果建筑即将被摧毁,则由Destroy方法处理,避免重复逻辑
|
||
if (this.HitPoints - dinfo.Amount <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (compMorphable?.StoredPawn != null)
|
||
{
|
||
Pawn pawn = compMorphable.StoredPawn;
|
||
float damageProportion = dinfo.Amount / this.def.statBases.GetStatValueFromList(StatDefOf.MaxHitPoints, 500f);
|
||
|
||
// --- 立即强制解除变形 ---
|
||
Map map = this.Map;
|
||
IntVec3 position = this.Position;
|
||
|
||
// 1. 将Pawn放回地图
|
||
GenSpawn.Spawn(pawn, position, map, WipeMode.Vanish);
|
||
PawnComponentsUtility.AddComponentsForSpawn(pawn);
|
||
|
||
// 2. 对Pawn施加等比例伤害
|
||
float pawnDamage = pawn.MaxHitPoints * damageProportion;
|
||
DamageInfo pawnDinfo = new DamageInfo(dinfo.Def, pawnDamage, dinfo.ArmorPenetrationInt, dinfo.Angle, dinfo.Instigator, null, dinfo.Weapon, dinfo.Category, dinfo.IntendedTarget);
|
||
pawn.TakeDamage(pawnDinfo);
|
||
|
||
Messages.Message("PawnTransformer_ForcedRevert".Translate(pawn.Named("PAWN")), pawn, MessageTypeDefOf.NegativeEvent);
|
||
|
||
// 3. 移除建筑
|
||
// 注意:这里不调用base.Destroy(),以避免循环和重复的恢复逻辑
|
||
this.Destroy(DestroyMode.Vanish);
|
||
}
|
||
}
|
||
|
||
public override void Destroy(DestroyMode mode)
|
||
{
|
||
// 只有在建筑还存在于地图上时,才执行恢复逻辑
|
||
if (this.Spawned)
|
||
{
|
||
if (compMorphable != null && compMorphable.StoredPawn != null)
|
||
{
|
||
Pawn pawn = compMorphable.StoredPawn;
|
||
Map map = this.Map;
|
||
IntVec3 position = this.Position;
|
||
|
||
GenSpawn.Spawn(pawn, position, map, WipeMode.Vanish);
|
||
PawnComponentsUtility.AddComponentsForSpawn(pawn);
|
||
|
||
if (mode == DestroyMode.KillFinalize)
|
||
{
|
||
Messages.Message("PawnTransformer_BuildingDestroyed".Translate(pawn.Named("PAWN"), this.Named("BUILDING")), pawn, MessageTypeDefOf.NegativeEvent);
|
||
}
|
||
}
|
||
}
|
||
base.Destroy(mode);
|
||
}
|
||
}
|
||
} |