Files
ArachnaeSwarm/Source/ArachnaeSwarm/Building_Comps/CompNutritionToFuelConverter.cs
ProjectKoi-Kalo\Kalo 675ac8b298 创建了 ArachnaeSwarmSettings.cs - 包含 enableDebugLogs 字段
 创建了 ArachnaeLog.cs - 中央化日志类,仅检查mod设置(不检查DevMode)
 创建了 ArachnaeSwarmMod.cs - Mod主类,提供UI设置选项
 修改了 MainHarmony.cs - 移除重复的Harmony初始化(现在由ArachnaeSwarmMod处理)
 修改了 .csproj - 添加了3个新文件到编译列表
 替换了所有582个 Log.Message/Error/Warning 调用为 ArachnaeLog.Debug()
2025-12-15 13:11:45 +08:00

177 lines
6.8 KiB
C#

using RimWorld;
using System.Collections.Generic;
using Verse;
namespace ArachnaeSwarm
{
public class CompProperties_NutritionToFuelConverter : CompProperties
{
public int checkInterval = 240;
public float nutritionCost = 20f;
public int workAmount = 6000;
public float fuelAmount = 1f;
public CompProperties_NutritionToFuelConverter()
{
compClass = typeof(CompNutritionToFuelConverter);
}
}
public class CompNutritionToFuelConverter : ThingComp
{
private CompRefuelableNutrition nutritionComp;
private CompProductStorage productStorageComp; // 改为使用 CompProductStorage
public float nutritionProgress = 0f;
public float workProgress = 0f;
private CompProperties_NutritionToFuelConverter Props => (CompProperties_NutritionToFuelConverter)props;
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
nutritionComp = parent.GetComp<CompRefuelableNutrition>();
productStorageComp = parent.GetComp<CompProductStorage>(); // 改为获取 CompProductStorage
if (nutritionComp == null)
{
ArachnaeLog.Debug($"[ArachnaeSwarm] {parent.def.defName} has CompNutritionToFuelConverter but no CompRefuelableNutrition.");
}
if (productStorageComp == null)
{
ArachnaeLog.Debug($"[ArachnaeSwarm] {parent.def.defName} has CompNutritionToFuelConverter but no CompProductStorage.");
}
}
public override void PostExposeData()
{
base.PostExposeData();
Scribe_Values.Look(ref nutritionProgress, "nutritionProgress", 0f);
Scribe_Values.Look(ref workProgress, "workProgress", 0f);
}
public override void CompTick()
{
base.CompTick();
if (parent.IsHashIntervalTick(Props.checkInterval))
{
TryProcess();
}
}
private void TryProcess()
{
if (productStorageComp == null || productStorageComp.IsFull)
{
return;
}
// Stage 1: Gather nutrition
if (nutritionProgress < Props.nutritionCost)
{
if (nutritionComp != null && nutritionComp.Fuel > 0)
{
float needed = Props.nutritionCost - nutritionProgress;
float canTake = System.Math.Min(needed, nutritionComp.Fuel);
nutritionComp.ConsumeFuel(canTake);
nutritionProgress += canTake;
}
// Reset work progress if we are still gathering nutrition
workProgress = 0;
return;
}
// Stage 2: Process work
workProgress += Props.checkInterval;
// Stage 3: Finish crafting
if (workProgress >= Props.workAmount)
{
int unitsToCraft = (int)(workProgress / Props.workAmount);
float fuelToProduce = unitsToCraft * Props.fuelAmount;
float spaceInStorage = productStorageComp.StorageSpaceRemaining;
if (spaceInStorage > 0)
{
// 使用 CompProductStorage 的 API 添加产物
float fuelToAdd = System.Math.Min(fuelToProduce, spaceInStorage);
bool added = productStorageComp.TryAddProduct(fuelToAdd);
if (added)
{
// Consume the full cost, regardless of how much was actually added.
nutritionProgress -= unitsToCraft * Props.nutritionCost;
workProgress -= unitsToCraft * Props.workAmount;
// 记录日志用于调试
ArachnaeLog.Debug($"[NutritionToFuel] Added {fuelToAdd} fuel to storage. Remaining space: {productStorageComp.StorageSpaceRemaining}");
}
else
{
ArachnaeLog.Debug($"[NutritionToFuel] Failed to add fuel to storage. Requested: {fuelToAdd}, Space: {spaceInStorage}");
}
}
// If spaceInStorage is 0, do nothing and let progress build up.
}
}
public override string CompInspectStringExtra()
{
if (productStorageComp == null) return null;
if (productStorageComp.IsFull)
{
return "ARA_NutritionConverter_State_TankFull".Translate();
}
if (nutritionProgress < Props.nutritionCost)
{
return "ARA_NutritionConverter_Gathering".Translate(nutritionProgress.ToString("F1"), Props.nutritionCost);
}
return "ARA_NutritionConverter_Working".Translate(workProgress.ToString("F0"), Props.workAmount);
}
// 新增:调试命令
public override IEnumerable<Gizmo> CompGetGizmosExtra()
{
foreach (var gizmo in base.CompGetGizmosExtra())
{
yield return gizmo;
}
if (DebugSettings.ShowDevGizmos)
{
// 调试命令:显示状态信息
yield return new Command_Action
{
defaultLabel = "DEV: Show Converter Status",
action = delegate
{
string status = $"Nutrition Converter Status:\n" +
$"Nutrition Progress: {nutritionProgress:F1}/{Props.nutritionCost}\n" +
$"Work Progress: {workProgress:F0}/{Props.workAmount}\n" +
$"Product Storage: {productStorageComp?.StorageAmount:F1}/{productStorageComp?.StorageCapacity:F1}\n" +
$"Nutrition Comp: {(nutritionComp != null ? "Found" : "Missing")}\n" +
$"Product Storage Comp: {(productStorageComp != null ? "Found" : "Missing")}";
Messages.Message(status, MessageTypeDefOf.SilentInput);
ArachnaeLog.Debug(status);
}
};
// 调试命令:重置进度
yield return new Command_Action
{
defaultLabel = "DEV: Reset Progress",
action = delegate
{
nutritionProgress = 0f;
workProgress = 0f;
Messages.Message("Converter progress reset", MessageTypeDefOf.SilentInput);
}
};
}
}
}
}