几个comps修改

This commit is contained in:
2025-10-09 12:02:11 +08:00
parent d16f1cee31
commit 64847d6e88
16 changed files with 681 additions and 77 deletions

View File

@@ -0,0 +1,214 @@
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)
{
Log.Error("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;
}
}
}