This commit is contained in:
Tourswen
2025-11-04 00:45:25 +08:00
parent 0c02135040
commit 9905782298
36 changed files with 1851 additions and 1238 deletions

View File

@@ -0,0 +1,78 @@
using RimWorld;
using Verse;
namespace WulaFallenEmpire
{
public class CompProperties_FactionSetter : CompProperties
{
// 派系配置
public FactionDef factionDef = null; // 指定的派系定义
public bool usePlayerFactionIfNull = true; // 如果没有指定派系,是否使用玩家派系
public bool overrideExistingFaction = true; // 是否覆盖已有的派系归属
public CompProperties_FactionSetter()
{
compClass = typeof(CompFactionSetter);
}
}
public class CompFactionSetter : ThingComp
{
private bool factionSet = false;
public CompProperties_FactionSetter Props => (CompProperties_FactionSetter)props;
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
if (!respawningAfterLoad && !factionSet)
{
SetFaction();
factionSet = true;
}
}
public override void PostExposeData()
{
base.PostExposeData();
Scribe_Values.Look(ref factionSet, "factionSet", false);
}
private void SetFaction()
{
// 如果不需要覆盖已有派系且建筑已有派系,则跳过
if (!Props.overrideExistingFaction && parent.Faction != null)
return;
Faction faction = GetTargetFaction();
if (faction != null && faction != parent.Faction)
{
parent.SetFaction(faction);
Log.Message($"Set faction for {parent.Label} to {faction.Name}");
}
}
private Faction GetTargetFaction()
{
// 1. 如果指定了派系定义,使用该派系
if (Props.factionDef != null)
{
Faction faction = Find.FactionManager.FirstFactionOfDef(Props.factionDef);
if (faction != null)
return faction;
}
// 2. 默认使用玩家派系
if (Props.usePlayerFactionIfNull)
return Faction.OfPlayer;
return null;
}
public override string CompInspectStringExtra()
{
return base.CompInspectStringExtra();
}
}
}

View File

@@ -44,29 +44,9 @@ namespace WulaFallenEmpire
// 是否已经生成初始单位
private bool initialUnitsSpawned = false;
// 是否已经执行过归属权转换
private bool ownershipTransferred = false;
public int StoredCount => storedMechanoids.Count;
public int MaxStorage => Props.maxStorageCapacity;
// 强制归属权转换
private void TransferOwnership()
{
if (ownershipTransferred)
return;
// 获取目标派系(默认为玩家派系)
Faction targetFaction = Props.ownershipFaction ?? Faction.OfPlayer;
if (Faction != targetFaction)
{
Log.Message($"[MechanoidRecycler] Transferring ownership from {Faction?.Name ?? "NULL"} to {targetFaction.Name}");
SetFaction(targetFaction);
}
ownershipTransferred = true;
}
// 生成初始单位
private void SpawnInitialUnits()
@@ -105,12 +85,6 @@ namespace WulaFallenEmpire
{
base.SpawnSetup(map, respawningAfterLoad);
// 执行归属权转换
if (!respawningAfterLoad)
{
TransferOwnership();
}
// 如果不是从存档加载,生成初始单位
if (!respawningAfterLoad)
{
@@ -400,7 +374,6 @@ namespace WulaFallenEmpire
Scribe_Collections.Look(ref storedMechanoids, "storedMechanoids", LookMode.Reference);
Scribe_Collections.Look(ref spawnQueue, "spawnQueue", LookMode.Deep);
Scribe_Values.Look(ref initialUnitsSpawned, "initialUnitsSpawned", false);
Scribe_Values.Look(ref ownershipTransferred, "ownershipTransferred", false);
if (Scribe.mode == LoadSaveMode.PostLoadInit)
{