498 lines
19 KiB
C#
498 lines
19 KiB
C#
// File: Comps/CompProperties_IncubatorData.cs
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using RimWorld;
|
||
using UnityEngine;
|
||
using Verse;
|
||
|
||
namespace ArachnaeSwarm
|
||
{
|
||
// 质量阈值和Hediff奖励配置
|
||
public class QualityHediffReward : IExposable
|
||
{
|
||
// 质量阈值(0-1)
|
||
public float qualityThreshold = 0f;
|
||
|
||
// 达到该阈值时给予的Hediff数量
|
||
public int hediffCount = 0;
|
||
|
||
// 是否给予全部Hediff(true=给予列表中所有Hediff,false=随机选择指定数量的Hediff)
|
||
public bool giveAllHediffs = false;
|
||
|
||
// 消息文本(可选)
|
||
public string messageKey = null;
|
||
|
||
public QualityHediffReward() { }
|
||
|
||
public QualityHediffReward(float threshold, int count, bool giveAll = false, string message = null)
|
||
{
|
||
qualityThreshold = threshold;
|
||
hediffCount = count;
|
||
giveAllHediffs = giveAll;
|
||
messageKey = message;
|
||
}
|
||
|
||
public void ExposeData()
|
||
{
|
||
Scribe_Values.Look(ref qualityThreshold, "qualityThreshold", 0f);
|
||
Scribe_Values.Look(ref hediffCount, "hediffCount", 0);
|
||
Scribe_Values.Look(ref giveAllHediffs, "giveAllHediffs", false);
|
||
Scribe_Values.Look(ref messageKey, "messageKey");
|
||
}
|
||
}
|
||
|
||
// IncubationConfig 保持不变
|
||
public class IncubationConfig : IExposable
|
||
{
|
||
public PawnKindDef pawnKind;
|
||
public ResearchProjectDef requiredResearch;
|
||
public float daysRequired;
|
||
public string buttonIconPath;
|
||
|
||
// === 新增:每个配置可以有自己的Hediff奖励列表 ===
|
||
public List<HediffDef> extraHediffs = new List<HediffDef>();
|
||
public List<QualityHediffReward> hediffRewards = new List<QualityHediffReward>();
|
||
|
||
public IncubationConfig() { }
|
||
|
||
public IncubationConfig(PawnKindDef pawnKind, ResearchProjectDef requiredResearch = null,
|
||
float daysRequired = 1f, string buttonIconPath = null)
|
||
{
|
||
this.pawnKind = pawnKind;
|
||
this.requiredResearch = requiredResearch;
|
||
this.daysRequired = daysRequired;
|
||
this.buttonIconPath = buttonIconPath;
|
||
extraHediffs = new List<HediffDef>();
|
||
hediffRewards = CreateDefaultRewards(); // 创建默认奖励配置
|
||
}
|
||
|
||
// 创建默认奖励配置(30%、50%、85%、99%阈值)
|
||
private List<QualityHediffReward> CreateDefaultRewards()
|
||
{
|
||
return new List<QualityHediffReward>
|
||
{
|
||
new QualityHediffReward(0.30f, 1, false, "ARA_QualityReward_1Hediff"),
|
||
new QualityHediffReward(0.50f, 2, false, "ARA_QualityReward_2Hediff"),
|
||
new QualityHediffReward(0.85f, 3, false, "ARA_QualityReward_3Hediff"),
|
||
new QualityHediffReward(0.99f, 4, false, "ARA_QualityReward_4Hediff")
|
||
};
|
||
}
|
||
|
||
public void ExposeData()
|
||
{
|
||
Scribe_Defs.Look(ref pawnKind, "pawnKind");
|
||
Scribe_Defs.Look(ref requiredResearch, "requiredResearch");
|
||
Scribe_Values.Look(ref daysRequired, "daysRequired", 1f);
|
||
Scribe_Values.Look(ref buttonIconPath, "buttonIconPath");
|
||
|
||
// === 新增:保存Hediff相关数据 ===
|
||
Scribe_Collections.Look(ref extraHediffs, "extraHediffs", LookMode.Def);
|
||
Scribe_Collections.Look(ref hediffRewards, "hediffRewards", LookMode.Deep);
|
||
|
||
if (Scribe.mode == LoadSaveMode.PostLoadInit)
|
||
{
|
||
if (extraHediffs == null)
|
||
extraHediffs = new List<HediffDef>();
|
||
if (hediffRewards == null)
|
||
hediffRewards = CreateDefaultRewards();
|
||
}
|
||
}
|
||
|
||
// 检查是否满足研究要求
|
||
public bool IsResearchComplete => requiredResearch == null || requiredResearch.IsFinished;
|
||
|
||
// 获取描述 - 添加Hediff奖励信息
|
||
public string GetDescription()
|
||
{
|
||
var builder = new StringBuilder();
|
||
builder.AppendLine(pawnKind?.description ?? "NoDescriptionAvailable".Translate());
|
||
builder.AppendLine();
|
||
builder.AppendLine("IncubationTime".Translate() + " " + daysRequired + " " + "DaysRequired".Translate());
|
||
|
||
if (requiredResearch != null)
|
||
{
|
||
if (requiredResearch.IsFinished)
|
||
builder.AppendLine("Research".Translate() + ": " + requiredResearch.LabelCap + " (" + "Completed".Translate() + ")");
|
||
else
|
||
builder.AppendLine("Research".Translate() + ": " + requiredResearch.LabelCap + " (" + "Required".Translate() + ")");
|
||
}
|
||
|
||
// === 新增:显示Hediff奖励信息 ===
|
||
if (extraHediffs != null && extraHediffs.Count > 0)
|
||
{
|
||
builder.AppendLine();
|
||
builder.AppendLine("HediffRewards".Translate() + ":");
|
||
|
||
foreach (var hediff in extraHediffs)
|
||
{
|
||
if (hediff != null)
|
||
{
|
||
builder.AppendLine(" - " + hediff.LabelCap);
|
||
}
|
||
}
|
||
|
||
// 显示质量阈值信息
|
||
if (hediffRewards != null && hediffRewards.Count > 0)
|
||
{
|
||
builder.AppendLine();
|
||
builder.AppendLine("QualityThresholds".Translate() + ":");
|
||
foreach (var reward in hediffRewards)
|
||
{
|
||
if (reward.qualityThreshold > 0 && reward.hediffCount > 0)
|
||
{
|
||
string rewardText = $" {reward.qualityThreshold:P0}: ";
|
||
rewardText += "HediffCountReward".Translate(reward.hediffCount);
|
||
builder.AppendLine(rewardText);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return builder.ToString().TrimEndNewlines();
|
||
}
|
||
|
||
// === 新增:获取应给予的Hediff数量 ===
|
||
public int GetHediffCountForQuality(float qualityPercent)
|
||
{
|
||
if (hediffRewards == null || hediffRewards.Count == 0)
|
||
return 0;
|
||
|
||
// 按阈值降序排序,找到第一个满足条件的奖励
|
||
var sortedRewards = new List<QualityHediffReward>(hediffRewards);
|
||
sortedRewards.Sort((a, b) => b.qualityThreshold.CompareTo(a.qualityThreshold));
|
||
|
||
foreach (var reward in sortedRewards)
|
||
{
|
||
if (qualityPercent >= reward.qualityThreshold)
|
||
{
|
||
if (reward.giveAllHediffs)
|
||
{
|
||
return extraHediffs != null ? extraHediffs.Count : 0;
|
||
}
|
||
return reward.hediffCount;
|
||
}
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
// === 新增:获取奖励的Hediff列表 ===
|
||
public List<HediffDef> GetRewardHediffs(float qualityPercent)
|
||
{
|
||
int count = GetHediffCountForQuality(qualityPercent);
|
||
if (count <= 0 || extraHediffs == null || extraHediffs.Count == 0)
|
||
return new List<HediffDef>();
|
||
|
||
// 如果要求给予所有Hediff,返回全部
|
||
var sortedRewards = new List<QualityHediffReward>(hediffRewards);
|
||
sortedRewards.Sort((a, b) => b.qualityThreshold.CompareTo(a.qualityThreshold));
|
||
|
||
foreach (var reward in sortedRewards)
|
||
{
|
||
if (qualityPercent >= reward.qualityThreshold && reward.giveAllHediffs)
|
||
{
|
||
return new List<HediffDef>(extraHediffs);
|
||
}
|
||
}
|
||
|
||
// 否则随机选择指定数量的Hediff(不重复)
|
||
List<HediffDef> result = new List<HediffDef>();
|
||
List<HediffDef> available = new List<HediffDef>(extraHediffs);
|
||
|
||
count = Mathf.Min(count, available.Count);
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
if (available.Count == 0) break;
|
||
|
||
int index = Rand.Range(0, available.Count);
|
||
result.Add(available[index]);
|
||
available.RemoveAt(index);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
// === 新增:获取奖励消息 ===
|
||
public string GetRewardMessage(float qualityPercent)
|
||
{
|
||
if (hediffRewards == null || hediffRewards.Count == 0)
|
||
return null;
|
||
|
||
var sortedRewards = new List<QualityHediffReward>(hediffRewards);
|
||
sortedRewards.Sort((a, b) => b.qualityThreshold.CompareTo(a.qualityThreshold));
|
||
|
||
foreach (var reward in sortedRewards)
|
||
{
|
||
if (qualityPercent >= reward.qualityThreshold && !string.IsNullOrEmpty(reward.messageKey))
|
||
{
|
||
return reward.messageKey.Translate();
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// 组件属性定义
|
||
public class CompProperties_IncubatorData : CompProperties
|
||
{
|
||
public List<IncubationConfig> incubationConfigs = new List<IncubationConfig>();
|
||
|
||
// 默认选择索引
|
||
public int defaultIndex = 0;
|
||
|
||
// Gizmo相关配置
|
||
public string buttonLabel = "IncubatorButtonLabel"; // 按钮标签翻译键
|
||
public string buttonDesc = "IncubatorButtonDesc"; // 按钮描述翻译键
|
||
public string menuTitle = "IncubatorMenuTitle"; // 菜单标题翻译键
|
||
public string defaultIconPath = "UI/Commands/Default";
|
||
|
||
// === 新增:全局Hediff奖励(所有配置共享)===
|
||
public List<HediffDef> globalExtraHediffs = new List<HediffDef>();
|
||
public List<QualityHediffReward> globalHediffRewards = new List<QualityHediffReward>();
|
||
|
||
public CompProperties_IncubatorData()
|
||
{
|
||
compClass = typeof(CompIncubatorData);
|
||
}
|
||
|
||
public override void ResolveReferences(ThingDef parentDef)
|
||
{
|
||
base.ResolveReferences(parentDef);
|
||
if (incubationConfigs == null)
|
||
incubationConfigs = new List<IncubationConfig>();
|
||
if (globalExtraHediffs == null)
|
||
globalExtraHediffs = new List<HediffDef>();
|
||
if (globalHediffRewards == null)
|
||
globalHediffRewards = CreateDefaultGlobalRewards();
|
||
}
|
||
|
||
// 创建默认的全局奖励配置
|
||
private List<QualityHediffReward> CreateDefaultGlobalRewards()
|
||
{
|
||
return new List<QualityHediffReward>
|
||
{
|
||
new QualityHediffReward(0.30f, 1, false, "ARA_QualityReward_1Hediff"),
|
||
new QualityHediffReward(0.50f, 2, false, "ARA_QualityReward_2Hediff"),
|
||
new QualityHediffReward(0.85f, 3, false, "ARA_QualityReward_3Hediff"),
|
||
new QualityHediffReward(0.99f, 4, false, "ARA_QualityReward_4Hediff")
|
||
};
|
||
}
|
||
|
||
// === 新增:配置后处理,确保每个配置都有Hediff奖励 ===
|
||
public override void PostLoadSpecial(ThingDef parent)
|
||
{
|
||
base.PostLoadSpecial(parent);
|
||
|
||
// 确保每个配置都有默认的奖励配置
|
||
foreach (var config in incubationConfigs)
|
||
{
|
||
if (config.hediffRewards == null || config.hediffRewards.Count == 0)
|
||
{
|
||
config.hediffRewards = new List<QualityHediffReward>
|
||
{
|
||
new QualityHediffReward(0.30f, 1, false, "ARA_QualityReward_1Hediff"),
|
||
new QualityHediffReward(0.50f, 2, false, "ARA_QualityReward_2Hediff"),
|
||
new QualityHediffReward(0.85f, 3, false, "ARA_QualityReward_3Hediff"),
|
||
new QualityHediffReward(0.99f, 4, false, "ARA_QualityReward_4Hediff")
|
||
};
|
||
}
|
||
|
||
// 合并全局Hediff列表
|
||
if (globalExtraHediffs != null && globalExtraHediffs.Count > 0)
|
||
{
|
||
if (config.extraHediffs == null)
|
||
config.extraHediffs = new List<HediffDef>();
|
||
|
||
foreach (var hediff in globalExtraHediffs)
|
||
{
|
||
if (!config.extraHediffs.Contains(hediff))
|
||
{
|
||
config.extraHediffs.Add(hediff);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 如果配置没有自己的奖励规则,使用全局规则
|
||
if (globalHediffRewards != null && globalHediffRewards.Count > 0)
|
||
{
|
||
if (config.hediffRewards == null || config.hediffRewards.Count == 0)
|
||
{
|
||
config.hediffRewards = new List<QualityHediffReward>(globalHediffRewards);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 数据存储组件(仅数据,无Gizmo)
|
||
public class CompIncubatorData : ThingComp
|
||
{
|
||
private CompProperties_IncubatorData Props => (CompProperties_IncubatorData)props;
|
||
|
||
// 当前选择的配置索引
|
||
private int selectedIndex = -1;
|
||
|
||
// 公开获取孵化配置列表的方法
|
||
public List<IncubationConfig> IncubationConfigs => Props?.incubationConfigs ?? new List<IncubationConfig>();
|
||
|
||
// 获取当前选择的配置
|
||
public IncubationConfig SelectedConfig
|
||
{
|
||
get
|
||
{
|
||
var configs = IncubationConfigs;
|
||
if (configs.Count == 0) return null;
|
||
|
||
// 如果未选择,返回null(和孵化池一样的行为)
|
||
if (selectedIndex == -1)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
if (selectedIndex < 0 || selectedIndex >= configs.Count)
|
||
return null;
|
||
|
||
return configs[selectedIndex];
|
||
}
|
||
}
|
||
|
||
// 获取当前选择的PawnKind
|
||
public PawnKindDef SelectedPawnKind => SelectedConfig?.pawnKind;
|
||
|
||
// 切换到特定索引(-1 表示清除选择)
|
||
public void SwitchToConfig(int index)
|
||
{
|
||
if (index == -1)
|
||
{
|
||
selectedIndex = -1; // 清除选择
|
||
}
|
||
else if (index >= 0 && index < IncubationConfigs.Count)
|
||
{
|
||
selectedIndex = index;
|
||
}
|
||
}
|
||
|
||
// 检查配置是否可用(研究是否完成)
|
||
public bool IsConfigAvailable(int index)
|
||
{
|
||
if (index < 0 || index >= IncubationConfigs.Count)
|
||
return false;
|
||
|
||
var config = IncubationConfigs[index];
|
||
return config?.IsResearchComplete ?? false;
|
||
}
|
||
|
||
// 获取配置索引
|
||
public int GetSelectedIndex()
|
||
{
|
||
return selectedIndex;
|
||
}
|
||
|
||
// 显示目标选择菜单
|
||
public void ShowFloatMenu()
|
||
{
|
||
var configs = IncubationConfigs;
|
||
if (configs == null || configs.Count == 0) return;
|
||
|
||
List<FloatMenuOption> options = new List<FloatMenuOption>();
|
||
|
||
for (int i = 0; i < configs.Count; i++)
|
||
{
|
||
var cfg = configs[i];
|
||
int index = i;
|
||
|
||
string label = cfg.pawnKind.LabelCap;
|
||
if (cfg.requiredResearch != null && !cfg.requiredResearch.IsFinished)
|
||
{
|
||
label += " (" + "ARA_Menu_RequiresResearch".Translate(cfg.requiredResearch.LabelCap) + ")";
|
||
options.Add(new FloatMenuOption(label, null));
|
||
}
|
||
else
|
||
{
|
||
label += " (" + "ARA_Menu_Days".Translate(cfg.daysRequired.ToString("F1")) + ")";
|
||
options.Add(new FloatMenuOption(label, () => SwitchToConfig(index)));
|
||
}
|
||
}
|
||
|
||
if (options.Count > 0)
|
||
Find.WindowStack.Add(new FloatMenu(options, "ARA_Menu_SelectIncubationTarget".Translate()));
|
||
}
|
||
|
||
// 存档加载
|
||
public override void PostExposeData()
|
||
{
|
||
base.PostExposeData();
|
||
Scribe_Values.Look(ref selectedIndex, "selectedIndex", -1);
|
||
|
||
if (Scribe.mode == LoadSaveMode.PostLoadInit)
|
||
{
|
||
// 验证索引有效性
|
||
if (selectedIndex >= IncubationConfigs.Count)
|
||
selectedIndex = Mathf.Clamp(Props.defaultIndex, 0, IncubationConfigs.Count - 1);
|
||
}
|
||
}
|
||
|
||
// 在建筑信息中显示额外信息
|
||
public override string CompInspectStringExtra()
|
||
{
|
||
var current = SelectedConfig;
|
||
if (current != null)
|
||
{
|
||
string status = "ARA_Status_Target".Translate(current.pawnKind.LabelCap);
|
||
|
||
if (current.requiredResearch != null && !current.requiredResearch.IsFinished)
|
||
{
|
||
status += " (" + "ARA_Menu_RequiresResearch".Translate(current.requiredResearch.LabelCap) + ")";
|
||
}
|
||
|
||
return status;
|
||
}
|
||
return base.CompInspectStringExtra();
|
||
}
|
||
|
||
// === 新增:获取当前配置的Hediff奖励信息 ===
|
||
public string GetHediffRewardDescription()
|
||
{
|
||
var config = SelectedConfig;
|
||
if (config == null || config.extraHediffs == null || config.extraHediffs.Count == 0)
|
||
return null;
|
||
|
||
var builder = new StringBuilder();
|
||
builder.AppendLine("HediffRewards".Translate() + ":");
|
||
|
||
foreach (var hediff in config.extraHediffs)
|
||
{
|
||
if (hediff != null)
|
||
{
|
||
builder.AppendLine(" - " + hediff.LabelCap);
|
||
}
|
||
}
|
||
|
||
if (config.hediffRewards != null && config.hediffRewards.Count > 0)
|
||
{
|
||
builder.AppendLine();
|
||
builder.AppendLine("QualityThresholds".Translate() + ":");
|
||
foreach (var reward in config.hediffRewards)
|
||
{
|
||
if (reward.qualityThreshold > 0 && reward.hediffCount > 0)
|
||
{
|
||
string rewardText = $" {reward.qualityThreshold:P0}: ";
|
||
if (reward.giveAllHediffs)
|
||
{
|
||
rewardText += "AllHediffsReward".Translate();
|
||
}
|
||
else
|
||
{
|
||
rewardText += "HediffCountReward".Translate(reward.hediffCount);
|
||
}
|
||
builder.AppendLine(rewardText);
|
||
}
|
||
}
|
||
}
|
||
|
||
return builder.ToString().TrimEndNewlines();
|
||
}
|
||
}
|
||
}
|