This commit is contained in:
2025-12-15 17:29:07 +08:00
parent 675ac8b298
commit 15404222fd
13 changed files with 678 additions and 392 deletions

View File

@@ -0,0 +1,264 @@
using RimWorld;
using UnityEngine;
using Verse;
namespace ArachnaeSwarm
{
public class Need_HoneyProduction : Need
{
// 基础流失速率(与食物需要对应)
private const float BaseHoneyGainPerTick = 2.6666667E-05f * 0.5f; // 食物流失速率的50%
// 用于存储对食物需要的引用
private Need_Food cachedFoodNeed;
// 当前类别
private HoneyProductionCategory curCategoryInt = HoneyProductionCategory.Empty;
// 上次满的时间点
private int lastFullTick = -99999;
// 蜜罐的最大容量(可能需要调整)
public override float MaxLevel => 1f;
// 当前类别
public HoneyProductionCategory CurCategory => curCategoryInt;
// 是否满仓
public bool IsFull => CurCategory == HoneyProductionCategory.Full;
// 是否为空
public bool IsEmpty => CurCategory == HoneyProductionCategory.Empty;
// 当前类别对应的生产效率
public float ProductionEfficiency
{
get
{
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 Need_Food FoodNeed
{
get
{
if (cachedFoodNeed == null || cachedFoodNeed.pawn != pawn)
{
cachedFoodNeed = pawn.needs?.TryGetNeed<Need_Food>();
}
return cachedFoodNeed;
}
}
public Need_HoneyProduction(Pawn newPawn) : base(newPawn)
{
// 初始化时设置为空
curLevelInt = 0f;
threshPercents = new System.Collections.Generic.List<float>
{
0.25f, // Low -> Medium
0.5f, // Medium -> High
0.75f // High -> Full
};
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref lastFullTick, "lastFullTick", -99999);
}
public override void NeedInterval()
{
base.NeedInterval();
// 检查是否需要冻结(与食物需要类似的条件)
if (IsFrozen)
{
return;
}
// 获取食物需要的流失速率
float foodFallRate = GetFoodFallRate();
// 蜜罐的增长速率是食物流失速率的50%
float honeyGainRate = foodFallRate * 0.5f;
// 应用150 ticks的间隔
CurLevel += honeyGainRate * 150f;
// 更新类别
UpdateCategory();
// 记录满的时间
if (IsFull)
{
lastFullTick = Find.TickManager.TicksGame;
}
}
// 获取食物流失速率
private float GetFoodFallRate()
{
if (FoodNeed == null)
{
// 如果没有食物需要,使用默认值
return BaseHoneyGainPerTick / 0.5f; // 反向计算基础食物流失速率
}
// 获取当前食物类别对应的流失速率
return FoodNeed.FoodFallPerTick;
}
// 更新类别
private void UpdateCategory()
{
float percentage = CurLevelPercentage;
if (percentage >= 0.75f)
curCategoryInt = HoneyProductionCategory.Full;
else if (percentage >= 0.5f)
curCategoryInt = HoneyProductionCategory.High;
else if (percentage >= 0.25f)
curCategoryInt = HoneyProductionCategory.Medium;
else if (percentage > 0f)
curCategoryInt = HoneyProductionCategory.Low;
else
curCategoryInt = HoneyProductionCategory.Empty;
}
// 提取蜂蜜(减少蜜罐存量)
public void ExtractHoney(float amount)
{
CurLevel -= amount;
if (CurLevel < 0f)
CurLevel = 0f;
UpdateCategory();
}
// 强制清空蜜罐
public void EmptyHoney()
{
CurLevel = 0f;
curCategoryInt = HoneyProductionCategory.Empty;
}
// 重置到初始状态
public override void SetInitialLevel()
{
// 初始为空
CurLevel = 0f;
curCategoryInt = HoneyProductionCategory.Empty;
}
// 获取提示字符串
public override string GetTipString()
{
string text = (LabelCap + ": " + CurLevelPercentage.ToStringPercent()).Colorize(ColoredText.TipSectionTitleColor);
text += "\n" + def.description;
text += $"\n\n{"AS.HoneyProduction.CurrentLevel".Translate()}: {CurLevel:0.##} / {MaxLevel:0.##}";
text += $"\n{"AS.HoneyProduction.Category".Translate()}: {GetCategoryLabel().CapitalizeFirst()}";
text += $"\n{"AS.HoneyProduction.Efficiency".Translate()}: {ProductionEfficiency.ToStringPercent()}";
text += $"\n{"AS.HoneyProduction.FoodDrainRate".Translate()}: {GetFoodFallRate():0.#####}/tick";
text += $"\n{"AS.HoneyProduction.HoneyGainRate".Translate()}: {(GetFoodFallRate() * 0.5f):0.#####}/tick";
if (IsFull)
{
text += $"\n\n{"AS.HoneyProduction.FullWarning".Translate()}";
}
return text;
}
// 获取类别标签
public string GetCategoryLabel()
{
switch (curCategoryInt)
{
case HoneyProductionCategory.Full:
return "AS.HoneyProduction.Full".Translate();
case HoneyProductionCategory.High:
return "AS.HoneyProduction.High".Translate();
case HoneyProductionCategory.Medium:
return "AS.HoneyProduction.Medium".Translate();
case HoneyProductionCategory.Low:
return "AS.HoneyProduction.Low".Translate();
case HoneyProductionCategory.Empty:
return "AS.HoneyProduction.Empty".Translate();
default:
return "Unknown";
}
}
// 在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 (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
{
get
{
// 如果基础条件冻结,则蜜罐生产也冻结
if (base.IsFrozen)
return true;
// 如果没有食物需要,或者食物需要被冻结,则蜜罐生产也冻结
if (FoodNeed == null || FoodNeed.IsFrozen)
return true;
// 如果生物死亡,则冻结
if (pawn.Dead)
return true;
return false;
}
}
// GUI变化箭头总是显示增长
public override int GUIChangeArrow => 1;
// 调试调整百分比
protected override void OffsetDebugPercent(float offsetPercent)
{
base.OffsetDebugPercent(offsetPercent);
UpdateCategory();
}
}
// 蜜罐生产类别
public enum HoneyProductionCategory
{
Empty, // 空仓
Low, // 低存量0-25%
Medium, // 中存量25-50%
High, // 高存量50-75%
Full // 满仓75-100%
}
}