This commit is contained in:
Tourswen
2025-12-18 00:44:50 +08:00
parent c686546abb
commit 3f73c7024f
50 changed files with 2258 additions and 1002 deletions

View File

@@ -1,52 +1,16 @@
// File: Need_HoneyProduction.cs
using RimWorld;
using UnityEngine;
using Verse;
namespace ArachnaeSwarm
{
public class HoneyProductionExtension : DefModExtension
{
// 蜜罐生产的基础转化率(相对于食物流失的百分比)
public float baseConversionRate = 0.5f;
// 最大蜜罐容量(可选,覆盖默认值)
public float maxHoneyCapacity = 1f;
// 是否启用蜜罐生产
public bool enableHoneyProduction = true;
// 生产速率乘数(影响生产速度)
public float productionSpeedFactor = 1f;
// 蜜罐类别对应的生产效率(可选,覆盖默认值)
public float fullProductionEfficiency = 1.5f;
public float highProductionEfficiency = 1.2f;
public float mediumProductionEfficiency = 1f;
public float lowProductionEfficiency = 0.5f;
public float emptyProductionEfficiency = 0f;
// 生产间隔ticks可选覆盖默认值
public int productionInterval = 150;
public static HoneyProductionExtension Get(Pawn pawn)
{
if (pawn?.def?.GetModExtension<HoneyProductionExtension>() is HoneyProductionExtension ext)
return ext;
return null;
}
// 获取转化率
public float GetConversionRate()
{
return baseConversionRate * productionSpeedFactor;
}
}
public class Need_HoneyProduction : Need
{
// 转化率:多少食物流失转换为蜂蜜
public float ConversionRate = 10f;
// 基础流失速率(与食物需要对应)
private const float BaseHoneyGainPerTick = 2.6666667E-05f * 0.5f; // 食物流失速率的50%作为默认值
private const float BaseHoneyGainPerTick = 2.6666667E-05f;
// 用于存储对食物需要的引用
private Need_Food cachedFoodNeed;
@@ -54,25 +18,8 @@ namespace ArachnaeSwarm
// 当前类别
private HoneyProductionCategory curCategoryInt = HoneyProductionCategory.Empty;
// 上次满的时间点
private int lastFullTick = -99999;
// 缓存的ModExtension
private HoneyProductionExtension cachedExtension;
// 蜜罐的最大容量 - 优先使用ModExtension的值
public override float MaxLevel
{
get
{
var ext = GetExtension();
if (ext != null && ext.maxHoneyCapacity > 0)
{
return ext.maxHoneyCapacity;
}
return FoodNeed?.MaxLevel ?? 1f;
}
}
// 最大容量 - 跟随食物需求的最大容量
public override float MaxLevel => FoodNeed?.MaxLevel ?? 1f;
// 当前类别
public HoneyProductionCategory CurCategory => curCategoryInt;
@@ -83,72 +30,6 @@ namespace ArachnaeSwarm
// 是否为空
public bool IsEmpty => CurCategory == HoneyProductionCategory.Empty;
// 当前类别对应的生产效率
public float ProductionEfficiency
{
get
{
var ext = GetExtension();
// 如果有扩展定义,使用扩展的值
if (ext != null)
{
switch (curCategoryInt)
{
case HoneyProductionCategory.Full:
return ext.fullProductionEfficiency;
case HoneyProductionCategory.High:
return ext.highProductionEfficiency;
case HoneyProductionCategory.Medium:
return ext.mediumProductionEfficiency;
case HoneyProductionCategory.Low:
return ext.lowProductionEfficiency;
case HoneyProductionCategory.Empty:
return ext.emptyProductionEfficiency;
default:
return 0f;
}
}
// 默认值
switch (curCategoryInt)
{
case HoneyProductionCategory.Full:
return 1.0f;
case HoneyProductionCategory.High:
return 0.8f;
case HoneyProductionCategory.Medium:
return 0.5f;
case HoneyProductionCategory.Low:
return 0.3f;
case HoneyProductionCategory.Empty:
return 0f;
default:
return 0f;
}
}
}
// 获取扩展
private HoneyProductionExtension GetExtension()
{
if (cachedExtension == null && pawn != null)
{
cachedExtension = pawn.def?.GetModExtension<HoneyProductionExtension>();
}
return cachedExtension;
}
// 是否启用蜜罐生产
private bool IsEnabled
{
get
{
var ext = GetExtension();
return ext == null || ext.enableHoneyProduction; // 默认启用
}
}
// 获取食物需要的引用
private Need_Food FoodNeed
{
@@ -174,19 +55,9 @@ namespace ArachnaeSwarm
};
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref lastFullTick, "lastFullTick", -99999);
}
public override void NeedInterval()
{
// 如果不启用,直接返回
if (!IsEnabled)
return;
// 检查是否需要冻结(与食物需要类似的条件)
// 检查是否需要冻结
if (IsFrozen)
{
return;
@@ -195,17 +66,11 @@ namespace ArachnaeSwarm
// 获取食物需要的流失速率
float foodFallRate = GetFoodFallRate();
// 获取转化率从ModExtension或使用默认值
float conversionRate = GetExtension()?.GetConversionRate() ?? 0.5f;
// 蜜罐的增长速率 = 食物流失速率 × 转化率
float honeyGainRate = foodFallRate * conversionRate;
// 获取生产间隔
int interval = GetExtension()?.productionInterval ?? 150;
float honeyGainRate = foodFallRate * ConversionRate;
// 应用间隔
CurLevel += honeyGainRate * interval;
CurLevel += honeyGainRate * 150; // 150 ticks间隔
// 确保不超过最大容量
if (CurLevel > MaxLevel)
@@ -213,12 +78,6 @@ namespace ArachnaeSwarm
// 更新类别
UpdateCategory();
// 记录满的时间
if (IsFull)
{
lastFullTick = Find.TickManager.TicksGame;
}
}
// 获取食物流失速率
@@ -227,7 +86,7 @@ namespace ArachnaeSwarm
if (FoodNeed == null)
{
// 如果没有食物需要,使用默认值
return BaseHoneyGainPerTick / 0.5f; // 反向计算基础食物流失速率
return BaseHoneyGainPerTick;
}
// 获取当前食物类别对应的流失速率
@@ -275,49 +134,6 @@ namespace ArachnaeSwarm
curCategoryInt = HoneyProductionCategory.Empty;
}
// 获取提示字符串
public override string GetTipString()
{
string text = (LabelCap + ": " + CurLevelPercentage.ToStringPercent()).Colorize(ColoredText.TipSectionTitleColor);
text += "\n" + def.description;
text += $"\n{"ARA_HoneyProduction.Efficiency".Translate()} {ProductionEfficiency.ToStringPercent()}";
// 获取每tick的速率
float foodFallPerTick = GetFoodFallRate();
float conversionRate = GetExtension()?.GetConversionRate() ?? 0.5f;
float honeyGainPerTick = foodFallPerTick * conversionRate;
// 转换为每秒1秒 = 60tick
float foodFallPerSecond = foodFallPerTick * 60f;
float honeyGainPerSecond = honeyGainPerTick * 60f;
text += $"\n{"ARA_HoneyProduction.FoodDrainRate".Translate()}: {foodFallPerSecond:0.#####}/ {"LetterSecond".Translate()}";
text += $"\n{"ARA_HoneyProduction.HoneyGainRate".Translate()}: {honeyGainPerSecond:0.#####}/ {"LetterSecond".Translate()}";
// 显示转化率信息
if (GetExtension() != null)
{
text += $"\n{"ARA_HoneyProduction.ConversionRate".Translate()}: {conversionRate:P1}";
if (GetExtension().productionSpeedFactor != 1f)
{
text += $"\n{"ARA_HoneyProduction.SpeedFactor".Translate()}: {GetExtension().productionSpeedFactor:F2}";
}
}
if (IsFull)
{
text += $"\n\n{"ARA_HoneyProduction.FullWarning".Translate()}";
}
// 显示是否启用
if (!IsEnabled)
{
text += $"\n\n<color=orange>{"ARA_HoneyProduction.Disabled".Translate()}</color>";
}
return text;
}
// 获取类别标签
public string GetCategoryLabel()
{
@@ -338,24 +154,6 @@ namespace ArachnaeSwarm
}
}
// 在UI上绘制
public override void DrawOnGUI(Rect rect, int maxThresholdMarkers = int.MaxValue, float customMargin = -1f, bool drawArrows = true, bool doTooltip = true, Rect? rectForTooltip = null, bool drawLabel = true)
{
// 如果不启用,不显示
if (!IsEnabled)
return;
if (threshPercents == null)
{
threshPercents = new System.Collections.Generic.List<float>
{
0.25f, 0.5f, 0.75f
};
}
base.DrawOnGUI(rect, maxThresholdMarkers, customMargin, false, doTooltip, rectForTooltip, drawLabel);
}
// 是否冻结
protected override bool IsFrozen
{
@@ -374,7 +172,7 @@ namespace ArachnaeSwarm
}
// GUI变化箭头总是显示增长
public override int GUIChangeArrow => IsEnabled ? 1 : 0;
public override int GUIChangeArrow => 1;
// 调试调整百分比
protected override void OffsetDebugPercent(float offsetPercent)