Files
ArachnaeSwarm/Source/ArachnaeSwarm/Plants/Plant_Transforming.cs

75 lines
2.2 KiB
C#

using RimWorld;
using Verse;
namespace ArachnaeSwarm
{
// Define a DefModExtension to hold our custom properties in XML
public class PlantTransformExtension : DefModExtension
{
public ThingDef buildingDef;
public float transformAtGrowth = 0.8f;
}
public class Plant_Transforming : Plant
{
private PlantTransformExtension extension;
private bool transformed = false;
// Caching the extension for performance
public PlantTransformExtension Extension
{
get
{
if (extension == null)
{
extension = this.def.GetModExtension<PlantTransformExtension>();
}
return extension;
}
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref transformed, "transformed", false);
}
public override void TickLong()
{
base.TickLong(); // Execute base plant logic first
if (transformed || this.Destroyed || Extension == null)
{
return;
}
if (this.Growth >= Extension.transformAtGrowth)
{
TryTransform();
transformed = true;
}
}
private void TryTransform()
{
if (this.Destroyed || Extension.buildingDef == null) return;
Map map = this.Map;
IntVec3 position = this.Position;
Faction factionToSet = null;
Zone zone = map.zoneManager.ZoneAt(position);
if (zone is Zone_Growing) factionToSet = Faction.OfPlayer;
if (factionToSet == null && map.IsPlayerHome) factionToSet = Faction.OfPlayer;
Thing building = ThingMaker.MakeThing(Extension.buildingDef, null);
if (factionToSet != null && building.def.CanHaveFaction)
{
building.SetFaction(factionToSet);
}
this.Destroy(DestroyMode.Vanish);
GenSpawn.Spawn(building, position, map, WipeMode.Vanish);
}
}
}