250 lines
8.5 KiB
C#
250 lines
8.5 KiB
C#
using RimWorld;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
using Verse;
|
|
|
|
namespace ArachnaeSwarm
|
|
{
|
|
public class Building_Morphable : Building
|
|
{
|
|
private CompMorphable compMorphable;
|
|
private CompRefuelableNutrition compRefuelable;
|
|
private Effecter researchEffecter;
|
|
|
|
public float virtualRest; // Public for external access
|
|
private bool forceSleep;
|
|
|
|
public float VirtualRestMax => 1.0f;
|
|
|
|
public override void ExposeData()
|
|
{
|
|
base.ExposeData();
|
|
Scribe_Values.Look(ref virtualRest, "virtualRest", 1f);
|
|
Scribe_Values.Look(ref forceSleep, "forceSleep", false);
|
|
}
|
|
|
|
public override void SpawnSetup(Map map, bool respawningAfterLoad)
|
|
{
|
|
base.SpawnSetup(map, respawningAfterLoad);
|
|
this.compMorphable = GetComp<CompMorphable>();
|
|
this.compRefuelable = GetComp<CompRefuelableNutrition>();
|
|
}
|
|
|
|
protected override void Tick()
|
|
{
|
|
base.Tick();
|
|
|
|
if (compMorphable?.StoredPawn == null)
|
|
{
|
|
StopResearchEffect();
|
|
return;
|
|
}
|
|
|
|
Pawn pawn = compMorphable.StoredPawn;
|
|
|
|
pawn.needs?.NeedsTrackerTickInterval(1);
|
|
|
|
var needs = pawn.needs;
|
|
if (needs == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// --- 饮食逻辑 ---
|
|
if (needs.food != null && compRefuelable != null)
|
|
{
|
|
if (compRefuelable.HasFuel)
|
|
{
|
|
needs.food.CurLevel += needs.food.FoodFallPerTick;
|
|
compRefuelable.ConsumeFuel(needs.food.FoodFallPerTick);
|
|
}
|
|
else
|
|
{
|
|
Messages.Message("PawnTransformer_OutOfFuel".Translate(pawn.Named("PAWN")), pawn, MessageTypeDefOf.NegativeEvent);
|
|
this.Destroy(DestroyMode.Vanish);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// --- 休息与研究逻辑 ---
|
|
if (needs.rest != null)
|
|
{
|
|
if (needs.rest.CurLevel <= 0)
|
|
{
|
|
forceSleep = true;
|
|
}
|
|
if (forceSleep && needs.rest.CurLevel >= needs.rest.MaxLevel)
|
|
{
|
|
forceSleep = false;
|
|
}
|
|
}
|
|
|
|
TimeAssignmentDef assignment = pawn.timetable?.CurrentAssignment ?? TimeAssignmentDefOf.Anything;
|
|
|
|
if (forceSleep || (assignment != TimeAssignmentDefOf.Work && assignment != TimeAssignmentDefOf.Anything))
|
|
{
|
|
// 休眠期
|
|
if (needs.rest != null)
|
|
{
|
|
needs.rest.CurLevel += Need_Rest.BaseRestGainPerTick * 2f;
|
|
}
|
|
StopResearchEffect();
|
|
}
|
|
else // 工作时间
|
|
{
|
|
ResearchProjectDef currentProj = Find.ResearchManager.GetProject();
|
|
if (currentProj != null)
|
|
{
|
|
float researchSpeed = pawn.GetStatValue(StatDefOf.ResearchSpeed);
|
|
researchSpeed *= this.GetStatValue(StatDefOf.ResearchSpeedFactor);
|
|
Find.ResearchManager.ResearchPerformed(researchSpeed, pawn);
|
|
pawn.skills.Learn(SkillDefOf.Intellectual, 0.1f, false);
|
|
StartResearchEffect();
|
|
}
|
|
else
|
|
{
|
|
StopResearchEffect();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void StartResearchEffect()
|
|
{
|
|
if (researchEffecter == null)
|
|
{
|
|
researchEffecter = EffecterDefOf.Research.Spawn();
|
|
}
|
|
researchEffecter.EffectTick(this, TargetInfo.Invalid);
|
|
}
|
|
|
|
private void StopResearchEffect()
|
|
{
|
|
if (researchEffecter != null)
|
|
{
|
|
researchEffecter.Cleanup();
|
|
researchEffecter = null;
|
|
}
|
|
}
|
|
|
|
|
|
public override string Label
|
|
{
|
|
get
|
|
{
|
|
if (compMorphable?.StoredPawn != null)
|
|
{
|
|
return $"{base.Label} ({compMorphable.StoredPawn.LabelShort})";
|
|
}
|
|
return base.Label;
|
|
}
|
|
}
|
|
|
|
public override string GetInspectString()
|
|
{
|
|
List<string> inspectStrings = new List<string>();
|
|
string baseString = base.GetInspectString();
|
|
if (!baseString.NullOrEmpty())
|
|
{
|
|
inspectStrings.Add(baseString);
|
|
}
|
|
|
|
if (compMorphable?.StoredPawn != null)
|
|
{
|
|
Pawn pawn = compMorphable.StoredPawn;
|
|
|
|
SkillRecord intellectualSkill = pawn.skills?.GetSkill(SkillDefOf.Intellectual);
|
|
if (intellectualSkill != null)
|
|
{
|
|
inspectStrings.Add($"{SkillDefOf.Intellectual.LabelCap}: {intellectualSkill.Level} ({intellectualSkill.XpProgressPercent:P0})");
|
|
}
|
|
|
|
Need_Rest restNeed = pawn.needs?.rest;
|
|
if (restNeed != null)
|
|
{
|
|
inspectStrings.Add($"{restNeed.LabelCap}: {restNeed.CurLevelPercentage:P0}");
|
|
}
|
|
|
|
TimeAssignmentDef assignment = pawn.timetable?.CurrentAssignment ?? TimeAssignmentDefOf.Anything;
|
|
bool isWorkingTime = !forceSleep && (assignment == TimeAssignmentDefOf.Work || assignment == TimeAssignmentDefOf.Anything);
|
|
|
|
string activity;
|
|
if (isWorkingTime)
|
|
{
|
|
ResearchProjectDef currentProj = Find.ResearchManager.GetProject();
|
|
if (currentProj != null)
|
|
{
|
|
activity = "Activity".Translate() + ": " + "Researching".Translate() + $" ({currentProj.LabelCap})";
|
|
}
|
|
else
|
|
{
|
|
activity = "Activity".Translate() + ": " + "Idle".Translate();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
activity = "Activity".Translate() + ": " + "Sleeping".Translate();
|
|
}
|
|
inspectStrings.Add(activity);
|
|
}
|
|
|
|
return string.Join("\n", inspectStrings);
|
|
}
|
|
|
|
public override void PostApplyDamage(DamageInfo dinfo, float totalDamageDealt)
|
|
{
|
|
base.PostApplyDamage(dinfo, totalDamageDealt);
|
|
if (dinfo.Amount <= 0 || this.Destroyed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (compMorphable?.StoredPawn != null)
|
|
{
|
|
ForceRevert(dinfo);
|
|
}
|
|
}
|
|
|
|
private void ForceRevert(DamageInfo dinfo)
|
|
{
|
|
Pawn pawn = compMorphable.StoredPawn;
|
|
Map map = this.Map;
|
|
IntVec3 position = this.Position;
|
|
|
|
compMorphable.SetStoredPawn(null);
|
|
|
|
float damageProportion = dinfo.Amount / this.def.statBases.GetStatValueFromList(StatDefOf.MaxHitPoints, 1f);
|
|
float pawnDamage = pawn.MaxHitPoints * damageProportion;
|
|
DamageInfo pawnDinfo = new DamageInfo(dinfo.Def, pawnDamage, dinfo.ArmorPenetrationInt, dinfo.Angle, dinfo.Instigator, null, dinfo.Weapon, dinfo.Category, dinfo.IntendedTarget);
|
|
|
|
this.Destroy(DestroyMode.Vanish);
|
|
|
|
GenSpawn.Spawn(pawn, position, map, WipeMode.Vanish);
|
|
PawnComponentsUtility.AddComponentsForSpawn(pawn);
|
|
pawn.TakeDamage(pawnDinfo);
|
|
|
|
Messages.Message("PawnTransformer_ForcedRevert".Translate(pawn.Named("PAWN")), pawn, MessageTypeDefOf.NegativeEvent);
|
|
}
|
|
|
|
public override void Destroy(DestroyMode mode)
|
|
{
|
|
if (researchEffecter != null)
|
|
{
|
|
researchEffecter.Cleanup();
|
|
researchEffecter = null;
|
|
}
|
|
|
|
if (this.Spawned && compMorphable != null && compMorphable.StoredPawn != null)
|
|
{
|
|
Pawn pawn = compMorphable.StoredPawn;
|
|
GenSpawn.Spawn(pawn, this.Position, this.Map, WipeMode.Vanish);
|
|
PawnComponentsUtility.AddComponentsForSpawn(pawn);
|
|
|
|
if (mode == DestroyMode.KillFinalize)
|
|
{
|
|
Messages.Message("PawnTransformer_BuildingDestroyed".Translate(pawn.Named("PAWN"), this.Named("BUILDING")), pawn, MessageTypeDefOf.NegativeEvent);
|
|
}
|
|
}
|
|
base.Destroy(mode);
|
|
}
|
|
}
|
|
} |