Files
ArachnaeSwarm/Source/ArachnaeSwarm/Abilities/ARA_ShowInteractiveThing/CompAbilityEffect_ShowInteractiveThing.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

215 lines
8.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using RimWorld;
using Verse;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace ArachnaeSwarm
{
public class CompAbilityEffect_ShowInteractiveThing : CompAbilityEffect
{
public new CompProperties_AbilityShowInteractiveThing Props => (CompProperties_AbilityShowInteractiveThing)props;
public override void Apply(LocalTargetInfo target, LocalTargetInfo dest)
{
// 这个组件只用于显示信息,不执行实际效果
}
public override bool Valid(LocalTargetInfo target, bool throwMessages = false)
{
return true;
}
// 重写工具提示方法,显示可孵化的物品列表
public override string ExtraTooltipPart()
{
StringBuilder stringBuilder = new StringBuilder();
// 获取所有可孵化的物品
var incubatableItems = GetIncubatableItems();
if (incubatableItems.Count == 0)
{
return null; // 没有可显示的内容
}
stringBuilder.AppendLine(Props.customLabel + ":");
stringBuilder.AppendLine();
// 显示每个可孵化的物品
foreach (var item in incubatableItems)
{
string entryText = $" • {item.thingDef.LabelCap}";
// 显示营养需求
if (Props.showNutritionCost)
{
float nutritionCost = GetIncubationCost(item.thingDef);
entryText += $" ({nutritionCost.ToString("F1")} 营养)";
}
// 显示孵化时间
if (Props.showIncubationTime)
{
int incubationTicks = GetIncubationTimeTicks(item.thingDef);
entryText += $" [{incubationTicks.ToStringTicksToPeriod()}]";
}
// 显示科技需求
if (Props.showResearchRequirements && item.requiredResearch != null)
{
bool researched = item.requiredResearch.IsFinished;
string researchStatus = researched ? "✓" : "✗";
entryText += $" <color={(researched ? "green" : "red")}>[{researchStatus}{item.requiredResearch.LabelCap}]</color>";
}
stringBuilder.AppendLine(entryText);
}
return stringBuilder.ToString().TrimEndNewlines();
}
// 获取所有可孵化的物品(基于 CompExtraIncubationInfo
private List<ProcessDef> GetIncubatableItems()
{
var result = new List<ProcessDef>();
if (Props.cocoonBuildingDef == null)
{
ArachnaeLog.Debug("CompAbilityEffect_ShowInteractiveThing: cocoonBuildingDef is null");
return result;
}
// 扫描所有定义了 CompExtraIncubationInfo 的物品
foreach (ThingDef thingDef in DefDatabase<ThingDef>.AllDefs)
{
var incubationCompProps = thingDef.GetCompProperties<CompProperties_ExtraIncubationInfo>();
if (incubationCompProps != null)
{
bool isMatch = false;
if (!incubationCompProps.cocoonDefs.NullOrEmpty())
{
isMatch = incubationCompProps.cocoonDefs.Contains(Props.cocoonBuildingDef);
}
else if (incubationCompProps.cocoonDef != null)
{
isMatch = incubationCompProps.cocoonDef == Props.cocoonBuildingDef;
}
if (isMatch)
{
// 获取研究前提
ResearchProjectDef researchPrerequisite = GetResearchPrerequisite(thingDef);
// 创建 ProcessDef与 CompInteractiveProducer 中的结构一致)
ProcessDef process = new ProcessDef
{
thingDef = thingDef,
productionTicks = GetIncubationTimeTicks(thingDef),
totalNutritionNeeded = GetIncubationCost(thingDef),
requiredResearch = researchPrerequisite
};
result.Add(process);
}
}
}
// 按物品名称排序
result.SortBy(p => p.thingDef.label);
return result;
}
// 获取研究前提(与 CompInteractiveProducer 中的逻辑一致)
private ResearchProjectDef GetResearchPrerequisite(ThingDef thingDef)
{
ResearchProjectDef researchPrerequisite = null;
// 方法1从 recipeMaker.researchPrerequisite 获取
if (thingDef.recipeMaker?.researchPrerequisite != null)
{
researchPrerequisite = thingDef.recipeMaker.researchPrerequisite;
}
// 方法2从 recipeMaker.researchPrerequisites 获取第一个
else if (thingDef.recipeMaker?.researchPrerequisites?.Count > 0)
{
researchPrerequisite = thingDef.recipeMaker.researchPrerequisites[0];
}
// 方法3从 thingDef.researchPrerequisites 获取(备用)
else if (thingDef.researchPrerequisites?.Count > 0)
{
researchPrerequisite = thingDef.researchPrerequisites[0];
}
return researchPrerequisite;
}
// 获取孵化时间(与 CompInteractiveProducer 中的逻辑一致)
private int GetIncubationTimeTicks(ThingDef thingDef)
{
StatDef incubationTimeStat = DefDatabase<StatDef>.GetNamedSilentFail("ARA_IncubationTime");
if (incubationTimeStat != null && thingDef.statBases != null)
{
var statValue = thingDef.statBases.FirstOrDefault(s => s.stat == incubationTimeStat);
if (statValue != null)
{
// ARA_IncubationTime 是以天为单位,转换为 ticks (1天 = 60000 ticks)
return Mathf.RoundToInt(statValue.value * 60000f);
}
}
// 默认值1 天
return 60000;
}
// 获取孵化所需营养(与 CompInteractiveProducer 中的逻辑一致)
private float GetIncubationCost(ThingDef thingDef)
{
StatDef incubationCostStat = DefDatabase<StatDef>.GetNamedSilentFail("ARA_IncubationCost");
if (incubationCostStat != null && thingDef.statBases != null)
{
var statValue = thingDef.statBases.FirstOrDefault(s => s.stat == incubationCostStat);
if (statValue != null)
{
return statValue.value;
}
}
// 默认值10 营养
return 10f;
}
// 获取所有可孵化的物品种类(用于其他用途)
public List<ThingDef> GetIncubatableThingDefs()
{
var incubatableItems = GetIncubatableItems();
var result = new List<ThingDef>();
foreach (var item in incubatableItems)
{
if (item.thingDef != null && (item.requiredResearch == null || item.requiredResearch.IsFinished))
{
result.Add(item.thingDef);
}
}
return result;
}
// 检查特定物品是否可孵化
public bool CanIncubateThingDef(ThingDef thingDef)
{
var incubatableItems = GetIncubatableItems();
foreach (var item in incubatableItems)
{
if (item.thingDef == thingDef &&
(item.requiredResearch == null || item.requiredResearch.IsFinished))
{
return true;
}
}
return false;
}
}
}