Files
ArachnaeSwarm/Source/ArachnaeSwarm/Hediff_DynamicInterceptor.cs
2025-09-28 23:31:28 +08:00

68 lines
2.3 KiB
C#

using Verse;
using RimWorld;
using System.Linq;
using System; // For Activator
namespace ArachnaeSwarm
{
public class Hediff_DynamicInterceptor : HediffWithComps
{
public CompProperties_GuardianPsyField GuardianProps
{
get
{
var hediffCompProps = def.comps?.FirstOrDefault(c => c is HediffCompProperties_DynamicInterceptor) as HediffCompProperties_DynamicInterceptor;
return hediffCompProps?.guardianProps;
}
}
public override void PostAdd(DamageInfo? dinfo)
{
base.PostAdd(dinfo);
var props = GuardianProps;
if (pawn != null && props != null)
{
if (pawn.GetComp<ThingComp_GuardianPsyField>() == null)
{
Log.Message($"[DynamicInterceptor] Adding ThingComp_GuardianPsyField to {pawn.LabelShort}.");
var newComp = (ThingComp_GuardianPsyField)Activator.CreateInstance(typeof(ThingComp_GuardianPsyField));
newComp.parent = pawn;
// Initialize with the actual properties from the HediffDef
newComp.Initialize(props);
pawn.AllComps.Add(newComp);
}
}
}
public override void PostRemoved()
{
base.PostRemoved();
if (pawn != null)
{
var comp = pawn.GetComp<ThingComp_GuardianPsyField>();
if (comp != null)
{
Log.Message($"[DynamicInterceptor] Removing ThingComp_GuardianPsyField from {pawn.LabelShort}.");
pawn.AllComps.Remove(comp);
}
}
}
}
// This comp will hold the properties for our custom interceptor
public class HediffCompProperties_DynamicInterceptor : HediffCompProperties
{
public CompProperties_GuardianPsyField guardianProps; // Nested properties
public HediffCompProperties_DynamicInterceptor()
{
this.compClass = typeof(HediffComp_DynamicInterceptor);
}
}
// A simple HediffComp to go with the properties
public class HediffComp_DynamicInterceptor : HediffComp
{
public HediffCompProperties_DynamicInterceptor Props => props as HediffCompProperties_DynamicInterceptor;
}
}