This commit is contained in:
2025-10-03 14:11:17 +08:00
parent 134ce2214f
commit c664c2b424
2 changed files with 16 additions and 101 deletions

View File

@@ -10,6 +10,7 @@ namespace ArachnaeSwarm
public CompProperties_RefuelableNutrition()
{
compClass = typeof(CompRefuelableNutrition);
// 默认启用这些Gizmo除非在XML中明确设置为false
this.targetFuelLevelConfigurable = true;
this.showAllowAutoRefuelToggle = true;
}
@@ -22,44 +23,10 @@ namespace ArachnaeSwarm
public float NutritionStored => Fuel;
public new CompProperties_RefuelableNutrition Props => (CompProperties_RefuelableNutrition)props;
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
// 确保自动补充目标燃料水平正确设置
if (TargetFuelLevel <= 0f || respawningAfterLoad)
{
SetTargetFuelLevelFromAutoRefuelPercent();
}
}
public override void PostExposeData()
{
base.PostExposeData();
// 在加载后确保目标燃料水平正确
if (Scribe.mode == LoadSaveMode.PostLoadInit)
{
SetTargetFuelLevelFromAutoRefuelPercent();
}
}
private void SetTargetFuelLevelFromAutoRefuelPercent()
{
// 使用 autoRefuelPercent 设置目标燃料水平
if (Props.autoRefuelPercent > 0f)
{
TargetFuelLevel = Props.fuelCapacity * Props.autoRefuelPercent;
}
else
{
// 默认设置为满容量
TargetFuelLevel = Props.fuelCapacity;
}
}
public override void CompTick()
{
// 调用基类的Tick让它处理真空等情况。
// 基类的燃料消耗逻辑将因为 fuelConsumptionRate 为0而无效。
base.CompTick();
// 我们自己的动态消耗逻辑
@@ -125,7 +92,6 @@ namespace ArachnaeSwarm
if (Props.targetFuelLevelConfigurable)
{
text += "\n" + "ConfiguredTargetFuelLevel".Translate(TargetFuelLevel.ToStringDecimalIfSmall());
text += " (" + "AutoRefuelPercent".Translate((Props.autoRefuelPercent * 100f).ToString("F0") + "%)");
}
return text;
@@ -139,4 +105,4 @@ namespace ArachnaeSwarm
}
}
}
}
}

View File

@@ -2,25 +2,23 @@ using RimWorld;
using Verse;
using System.Reflection;
using HarmonyLib;
using System.Collections.Generic;
using System.Linq;
namespace ArachnaeSwarm
{
public class CompProperties_RefuelableNutrition_WithKey : CompProperties_RefuelableNutrition
{
public string saveKeysPrefix;
// 修改:禁止的具体物品定义列表
public List<ThingDef> disallowedThingDefs;
public CompProperties_RefuelableNutrition_WithKey()
{
compClass = typeof(CompRefuelableNutrition_WithKey);
}
}
public class CompRefuelableNutrition_WithKey : CompRefuelableNutrition, IFuelSource
{
public new CompProperties_RefuelableNutrition_WithKey Props => (CompProperties_RefuelableNutrition_WithKey)props;
public override void PostExposeData()
{
string prefix = Props.saveKeysPrefix;
@@ -30,19 +28,22 @@ namespace ArachnaeSwarm
base.PostExposeData();
return;
}
// --- Accessing private fields from CompRefuelable (base of CompRefuelableNutrition) ---
FieldInfo fuelField = AccessTools.Field(typeof(CompRefuelable), "fuel");
FieldInfo configuredTargetFuelLevelField = AccessTools.Field(typeof(CompRefuelable), "configuredTargetFuelLevel");
FieldInfo allowAutoRefuelField = AccessTools.Field(typeof(CompRefuelable), "allowAutoRefuel");
// Get current values
float currentFuel = (float)fuelField.GetValue(this);
float currentConfiguredLevel = (float)configuredTargetFuelLevelField.GetValue(this);
bool currentAllowAuto = (bool)allowAutoRefuelField.GetValue(this);
// Scribe with prefix
Scribe_Values.Look(ref currentFuel, prefix + "_fuel", 0f);
Scribe_Values.Look(ref currentConfiguredLevel, prefix + "_configuredTargetFuelLevel", -1f);
Scribe_Values.Look(ref currentAllowAuto, prefix + "_allowAutoRefuel", true);
// Set values back if loading
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
@@ -50,65 +51,14 @@ namespace ArachnaeSwarm
configuredTargetFuelLevelField.SetValue(this, currentConfiguredLevel);
allowAutoRefuelField.SetValue(this, currentAllowAuto);
}
}
// 重写燃料处理方法,添加物品类型限制
public new void Refuel(List<Thing> fuelThings)
{
// 过滤掉禁止物品类型的物品
List<Thing> allowedFuelThings = new List<Thing>();
List<Thing> disallowedFuelThings = new List<Thing>();
foreach (var thing in fuelThings)
{
if (IsFuelAllowed(thing))
{
allowedFuelThings.Add(thing);
}
else
{
disallowedFuelThings.Add(thing);
}
}
// 如果有被禁止的物品,给出提示
if (disallowedFuelThings.Count > 0)
{
string disallowedNames = string.Join(", ", disallowedFuelThings.Select(t => t.LabelCap));
Messages.Message("CannotUseDisallowedFuel".Translate(disallowedNames, parent.LabelCap), disallowedFuelThings, MessageTypeDefOf.RejectInput);
}
// 只对允许的物品调用基类的Refuel方法
if (allowedFuelThings.Count > 0)
{
base.Refuel(allowedFuelThings);
}
// --- Accessing private fields from CompRefuelableNutrition ---
// (Assuming there are any. If not, this part is not needed)
// Example:
// FieldInfo someOtherField = AccessTools.Field(typeof(CompRefuelableNutrition), "someOtherPrivateField");
// ... and so on
}
// 检查燃料是否被允许
private bool IsFuelAllowed(Thing fuel)
{
// 检查是否在禁止的物品类型列表中
if (IsInDisallowedThingDefs(fuel))
return false;
return true;
}
// 检查物品是否在禁止的物品类型列表中
private bool IsInDisallowedThingDefs(Thing thing)
{
if (Props.disallowedThingDefs == null || Props.disallowedThingDefs.Count == 0)
return false;
return Props.disallowedThingDefs.Contains(thing.def);
}
// 可选:重写燃料描述以显示限制信息
public override string CompInspectStringExtra()
{
string baseString = base.CompInspectStringExtra();
if (Props.disallowedThingDefs != null && Props.disallowedThingDefs.Count > 0)
{
string thingNames = string.Join(", ", Props.disallowedThingDefs.Select(d => d.label));
return baseString + $"\n{"DisallowedItems".Translate()}: {thingNames}";
}
return baseString;
}
public new void Notify_UsedThisTick()
{
if (Props.consumeFuelOnlyWhenUsed)
@@ -117,5 +67,4 @@ namespace ArachnaeSwarm
}
}
}
}