Files
ArachnaeSwarm/Source/ArachnaeSwarm/Building_Comps/ARA_NutrientNetwork/CompLineDrawer.cs
2025-09-17 16:28:23 +08:00

89 lines
2.9 KiB
C#

using RimWorld;
using Verse;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
namespace ArachnaeSwarm
{
public class CompProperties_LineDrawer : CompProperties
{
public List<ThingDef> linkableBuildings;
public float maxDistance = 999f;
public string lineTexturePath = "Things/Special/Power/Wire";
public CompProperties_LineDrawer()
{
compClass = typeof(CompLineDrawer);
}
}
public class CompLineDrawer : ThingComp
{
private CompProperties_LineDrawer Props => (CompProperties_LineDrawer)props;
private List<Thing> linkedBuildings = new List<Thing>();
private Material lineMat;
private Material LineMat
{
get
{
if (lineMat == null)
{
lineMat = MaterialPool.MatFrom(Props.lineTexturePath, ShaderDatabase.Transparent, Color.white);
}
return lineMat;
}
}
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
FindAndLinkBuildings();
}
public override void CompTick()
{
base.CompTick();
if (parent.IsHashIntervalTick(120))
{
FindAndLinkBuildings();
}
}
private void FindAndLinkBuildings()
{
int previousCount = linkedBuildings.Count;
linkedBuildings.Clear();
if (Props.linkableBuildings.NullOrEmpty()) return;
var potentialTargets = parent.Map.listerBuildings.allBuildingsColonist.Where(b =>
b != parent &&
Props.linkableBuildings.Contains(b.def) &&
parent.Position.DistanceTo(b.Position) <= Props.maxDistance
);
linkedBuildings.AddRange(potentialTargets);
if (linkedBuildings.Count != previousCount)
{
parent.Map.mapDrawer.MapMeshDirty(parent.Position, MapMeshFlagDefOf.Things, true, false);
}
}
public override void PostPrintOnto(SectionLayer layer)
{
base.PostPrintOnto(layer);
foreach (var building in linkedBuildings)
{
Vector3 center = (this.parent.TrueCenter() + this.parent.Graphic.DrawOffset(this.parent.Rotation) + building.TrueCenter() + building.Graphic.DrawOffset(building.Rotation)) / 2f;
center.y = AltitudeLayer.SmallWire.AltitudeFor();
Vector3 v = building.TrueCenter() - this.parent.TrueCenter();
Vector2 size = new Vector2(1f, v.MagnitudeHorizontal());
float rot = v.AngleFlat();
Printer_Plane.PrintPlane(layer, center, size, this.LineMat, rot);
}
}
}
}