Files
ArachnaeSwarm/Source/ArachnaeSwarm/Needs/Need_HoneyProduction.cs
2025-12-19 12:00:27 +08:00

195 lines
5.9 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 UnityEngine;
using Verse;
namespace ArachnaeSwarm
{
public class Need_HoneyProduction : Need
{
// 转化率:多少食物流失转换为蜂蜜
public float ConversionRate = 5f;
// 基础流失速率(与食物需要对应)
private const float BaseHoneyGainPerTick = 2.6666667E-05f;
// 用于存储对食物需要的引用
private Need_Food cachedFoodNeed;
// 当前类别
private HoneyProductionCategory curCategoryInt = HoneyProductionCategory.Empty;
// 最大容量 - 跟随食物需求的最大容量
public override float MaxLevel => FoodNeed?.MaxLevel ?? 1f;
// 当前类别
public HoneyProductionCategory CurCategory => curCategoryInt;
// 是否满仓
public bool IsFull => CurCategory == HoneyProductionCategory.Full;
// 是否为空
public bool IsEmpty => CurCategory == HoneyProductionCategory.Empty;
// 获取食物需要的引用
private Need_Food FoodNeed
{
get
{
if (cachedFoodNeed == null)
{
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 NeedInterval()
{
// 检查是否需要冻结
if (IsFrozen)
{
return;
}
// 获取食物需要的流失速率
float foodFallRate = GetFoodFallRate();
// 蜜罐的增长速率 = 食物流失速率 × 转化率
float honeyGainRate = foodFallRate * ConversionRate;
// 应用间隔
CurLevel += honeyGainRate * 150; // 150 ticks间隔
// 确保不超过最大容量
if (CurLevel > MaxLevel)
CurLevel = MaxLevel;
// 更新类别
UpdateCategory();
}
// 获取食物流失速率
private float GetFoodFallRate()
{
if (FoodNeed == null)
{
// 如果没有食物需要,使用默认值
return BaseHoneyGainPerTick;
}
// 获取当前食物类别对应的流失速率
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 string GetCategoryLabel()
{
switch (curCategoryInt)
{
case HoneyProductionCategory.Full:
return "ARA_HoneyProduction.Full".Translate();
case HoneyProductionCategory.High:
return "ARA_HoneyProduction.High".Translate();
case HoneyProductionCategory.Medium:
return "ARA_HoneyProduction.Medium".Translate();
case HoneyProductionCategory.Low:
return "ARA_HoneyProduction.Low".Translate();
case HoneyProductionCategory.Empty:
return "ARA_HoneyProduction.Empty".Translate();
default:
return "Unknown";
}
}
// 是否冻结
protected override bool IsFrozen
{
get
{
// 如果基础条件冻结,则蜜罐生产也冻结
if (base.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%
}
}