各种更新
This commit is contained in:
@@ -11,6 +11,12 @@ namespace WulaFallenEmpire
|
||||
public int aircraftCount = 1; // 起飞后提供的战机数量
|
||||
public ThingDef skyfallerLeaving; // 起飞时的天空坠落者效果
|
||||
|
||||
// 新增:自动发射配置
|
||||
public bool autoLaunchEnabled = false; // 是否启用自动发射
|
||||
public int autoLaunchDelayTicks = 600; // 自动发射延迟(ticks,默认10秒)
|
||||
public bool autoLaunchOnConstruction = true; // 建造完成后自动发射
|
||||
public bool autoLaunchOnPowerOn = false; // 通电时自动发射
|
||||
|
||||
public CompProperties_AircraftHangar()
|
||||
{
|
||||
compClass = typeof(CompAircraftHangar);
|
||||
@@ -21,6 +27,86 @@ namespace WulaFallenEmpire
|
||||
{
|
||||
public CompProperties_AircraftHangar Props => (CompProperties_AircraftHangar)props;
|
||||
|
||||
// 新增:自动发射状态
|
||||
private bool autoLaunchScheduled = false;
|
||||
private int autoLaunchTick = -1;
|
||||
private bool hasLaunched = false;
|
||||
|
||||
public override void PostSpawnSetup(bool respawningAfterLoad)
|
||||
{
|
||||
base.PostSpawnSetup(respawningAfterLoad);
|
||||
|
||||
if (!respawningAfterLoad && Props.autoLaunchEnabled && Props.autoLaunchOnConstruction)
|
||||
{
|
||||
ScheduleAutoLaunch();
|
||||
}
|
||||
}
|
||||
|
||||
public override void PostExposeData()
|
||||
{
|
||||
base.PostExposeData();
|
||||
Scribe_Values.Look(ref autoLaunchScheduled, "autoLaunchScheduled", false);
|
||||
Scribe_Values.Look(ref autoLaunchTick, "autoLaunchTick", -1);
|
||||
Scribe_Values.Look(ref hasLaunched, "hasLaunched", false);
|
||||
}
|
||||
|
||||
public override void CompTick()
|
||||
{
|
||||
base.CompTick();
|
||||
|
||||
// 处理自动发射
|
||||
if (Props.autoLaunchEnabled && !hasLaunched)
|
||||
{
|
||||
HandleAutoLaunch();
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:自动发射处理
|
||||
private void HandleAutoLaunch()
|
||||
{
|
||||
// 检查预定发射
|
||||
if (autoLaunchScheduled && Find.TickManager.TicksGame >= autoLaunchTick)
|
||||
{
|
||||
LaunchAircraft();
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查通电自动发射
|
||||
if (Props.autoLaunchOnPowerOn && IsPoweredOn() && !autoLaunchScheduled)
|
||||
{
|
||||
ScheduleAutoLaunch();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:检查电力状态
|
||||
private bool IsPoweredOn()
|
||||
{
|
||||
var powerComp = parent.GetComp<CompPowerTrader>();
|
||||
return powerComp != null && powerComp.PowerOn;
|
||||
}
|
||||
|
||||
// 新增:预定自动发射
|
||||
private void ScheduleAutoLaunch()
|
||||
{
|
||||
if (hasLaunched || autoLaunchScheduled)
|
||||
return;
|
||||
|
||||
autoLaunchScheduled = true;
|
||||
autoLaunchTick = Find.TickManager.TicksGame + Props.autoLaunchDelayTicks;
|
||||
|
||||
Messages.Message("AircraftAutoLaunchScheduled".Translate(Props.aircraftDef.LabelCap, (Props.autoLaunchDelayTicks / 60f).ToString("F1")), parent, MessageTypeDefOf.NeutralEvent);
|
||||
}
|
||||
|
||||
// 新增:强制立即发射(用于调试或其他系统调用)
|
||||
public void ForceLaunch()
|
||||
{
|
||||
if (!hasLaunched)
|
||||
{
|
||||
LaunchAircraft();
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<Gizmo> CompGetGizmosExtra()
|
||||
{
|
||||
foreach (Gizmo gizmo in base.CompGetGizmosExtra())
|
||||
@@ -28,7 +114,11 @@ namespace WulaFallenEmpire
|
||||
yield return gizmo;
|
||||
}
|
||||
|
||||
// 起飞命令
|
||||
// 如果已经发射,不显示任何按钮
|
||||
if (hasLaunched)
|
||||
yield break;
|
||||
|
||||
// 手动发射命令
|
||||
Command_Action launchCommand = new Command_Action
|
||||
{
|
||||
defaultLabel = "LaunchAircraft".Translate(),
|
||||
@@ -46,8 +136,28 @@ namespace WulaFallenEmpire
|
||||
yield return launchCommand;
|
||||
}
|
||||
|
||||
// 新增:切换自动发射状态
|
||||
private void ToggleAutoLaunch()
|
||||
{
|
||||
if (autoLaunchScheduled)
|
||||
{
|
||||
// 取消预定发射
|
||||
autoLaunchScheduled = false;
|
||||
autoLaunchTick = -1;
|
||||
Messages.Message("AutoLaunchCancelled".Translate(), parent, MessageTypeDefOf.NeutralEvent);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 预定发射
|
||||
ScheduleAutoLaunch();
|
||||
}
|
||||
}
|
||||
|
||||
private void LaunchAircraft()
|
||||
{
|
||||
if (hasLaunched)
|
||||
return;
|
||||
|
||||
// 获取全局战机管理器
|
||||
WorldComponent_AircraftManager aircraftManager = Find.World.GetComponent<WorldComponent_AircraftManager>();
|
||||
|
||||
@@ -61,7 +171,7 @@ namespace WulaFallenEmpire
|
||||
aircraftManager.AddAircraft(Props.aircraftDef, Props.aircraftCount, parent.Faction);
|
||||
|
||||
// 显示消息
|
||||
Messages.Message("AircraftLaunched".Translate(Props.aircraftCount, Props.aircraftDef.LabelCap), MessageTypeDefOf.PositiveEvent);
|
||||
Messages.Message("AircraftLaunched".Translate(Props.aircraftCount, Props.aircraftDef.LabelCap), parent, MessageTypeDefOf.PositiveEvent);
|
||||
|
||||
// 创建起飞效果(仅视觉效果)
|
||||
if (Props.skyfallerLeaving != null)
|
||||
@@ -73,6 +183,9 @@ namespace WulaFallenEmpire
|
||||
// 如果没有定义 Skyfaller,直接销毁建筑
|
||||
parent.Destroy();
|
||||
}
|
||||
|
||||
hasLaunched = true;
|
||||
autoLaunchScheduled = false;
|
||||
}
|
||||
|
||||
private void CreateTakeoffEffect()
|
||||
@@ -112,10 +225,30 @@ namespace WulaFallenEmpire
|
||||
}
|
||||
}
|
||||
|
||||
public override void PostExposeData()
|
||||
// 新增:检查是否已经发射
|
||||
public bool HasLaunched => hasLaunched;
|
||||
|
||||
// 新增:获取自动发射状态信息(用于检查字符串)
|
||||
public override string CompInspectStringExtra()
|
||||
{
|
||||
base.PostExposeData();
|
||||
// 不需要保存状态,因为建筑起飞后就销毁了
|
||||
if (hasLaunched)
|
||||
return "AircraftStatusLaunched".Translate();
|
||||
|
||||
if (Props.autoLaunchEnabled)
|
||||
{
|
||||
if (autoLaunchScheduled)
|
||||
{
|
||||
int ticksRemaining = autoLaunchTick - Find.TickManager.TicksGame;
|
||||
float secondsRemaining = ticksRemaining / 60f;
|
||||
return "AutoLaunchScheduled".Translate(secondsRemaining.ToString("F1"));
|
||||
}
|
||||
else
|
||||
{
|
||||
return "AutoLaunchReady".Translate();
|
||||
}
|
||||
}
|
||||
|
||||
return base.CompInspectStringExtra();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,243 +0,0 @@
|
||||
using RimWorld;
|
||||
using Verse;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WulaFallenEmpire
|
||||
{
|
||||
public class CompProperties_AutoLaunchHangar : CompProperties
|
||||
{
|
||||
public ThingDef aircraftDef; // 对应的战机定义
|
||||
public int aircraftCount = 1; // 起飞后提供的战机数量
|
||||
public ThingDef skyfallerLeaving; // 起飞时的天空坠落者效果
|
||||
public int launchDelayTicks = 60; // 延迟启动的ticks(默认1秒)
|
||||
public bool requirePower = true; // 是否需要电力才能启动
|
||||
|
||||
public CompProperties_AutoLaunchHangar()
|
||||
{
|
||||
compClass = typeof(CompAutoLaunchHangar);
|
||||
}
|
||||
}
|
||||
|
||||
public class CompAutoLaunchHangar : ThingComp
|
||||
{
|
||||
public CompProperties_AutoLaunchHangar Props => (CompProperties_AutoLaunchHangar)props;
|
||||
private bool hasLaunched = false;
|
||||
private int spawnTick = -1;
|
||||
|
||||
private CompPowerTrader powerComp;
|
||||
private CompRefuelable refuelableComp;
|
||||
|
||||
public override void PostSpawnSetup(bool respawningAfterLoad)
|
||||
{
|
||||
base.PostSpawnSetup(respawningAfterLoad);
|
||||
|
||||
if (!respawningAfterLoad)
|
||||
{
|
||||
// 记录生成时间
|
||||
spawnTick = Find.TickManager.TicksAbs;
|
||||
hasLaunched = false;
|
||||
|
||||
// 缓存其他组件
|
||||
powerComp = parent.GetComp<CompPowerTrader>();
|
||||
refuelableComp = parent.GetComp<CompRefuelable>();
|
||||
|
||||
Log.Message($"AutoLaunchHangar spawned at tick {spawnTick}, will launch in {Props.launchDelayTicks} ticks");
|
||||
}
|
||||
}
|
||||
|
||||
public override void PostExposeData()
|
||||
{
|
||||
base.PostExposeData();
|
||||
Scribe_Values.Look(ref hasLaunched, "hasLaunched", false);
|
||||
Scribe_Values.Look(ref spawnTick, "spawnTick", -1);
|
||||
}
|
||||
|
||||
public override void CompTick()
|
||||
{
|
||||
base.CompTick();
|
||||
|
||||
if (hasLaunched || spawnTick == -1)
|
||||
return;
|
||||
|
||||
int currentTick = Find.TickManager.TicksAbs;
|
||||
if (currentTick - spawnTick >= Props.launchDelayTicks)
|
||||
{
|
||||
if (CanAutoLaunch())
|
||||
{
|
||||
AutoLaunchAircraft();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanAutoLaunch()
|
||||
{
|
||||
// 检查建筑是否完好
|
||||
if (parent.HitPoints <= 0)
|
||||
{
|
||||
Log.Message("AutoLaunch: Hangar is damaged, cannot launch");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查电力需求
|
||||
if (Props.requirePower && powerComp != null && !powerComp.PowerOn)
|
||||
{
|
||||
Log.Message("AutoLaunch: No power, cannot launch");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查燃料需求
|
||||
if (refuelableComp != null && !refuelableComp.HasFuel)
|
||||
{
|
||||
Log.Message("AutoLaunch: No fuel, cannot launch");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查地图是否有效
|
||||
if (parent.Map == null)
|
||||
{
|
||||
Log.Message("AutoLaunch: Map is null, cannot launch");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void AutoLaunchAircraft()
|
||||
{
|
||||
Log.Message($"AutoLaunch: Starting aircraft launch sequence for {parent.Label}");
|
||||
|
||||
// 获取全局战机管理器
|
||||
WorldComponent_AircraftManager aircraftManager = Find.World.GetComponent<WorldComponent_AircraftManager>();
|
||||
|
||||
if (aircraftManager == null)
|
||||
{
|
||||
Log.Error("AutoLaunch: AircraftManager not found");
|
||||
hasLaunched = true;
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 立即向全局管理器注册战机
|
||||
aircraftManager.AddAircraft(Props.aircraftDef, Props.aircraftCount, parent.Faction);
|
||||
|
||||
// 显示消息
|
||||
Messages.Message("AircraftAutoLaunched".Translate(Props.aircraftCount, Props.aircraftDef.LabelCap),
|
||||
parent, MessageTypeDefOf.PositiveEvent);
|
||||
|
||||
Log.Message($"AutoLaunch: Successfully added {Props.aircraftCount} {Props.aircraftDef.LabelCap} to global manager");
|
||||
|
||||
// 创建起飞效果
|
||||
if (Props.skyfallerLeaving != null)
|
||||
{
|
||||
CreateAutoTakeoffEffect();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有定义 Skyfaller,直接销毁建筑
|
||||
parent.Destroy();
|
||||
}
|
||||
|
||||
hasLaunched = true;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.Error($"AutoLaunch error: {ex}");
|
||||
hasLaunched = true; // 标记为已启动,避免重复尝试
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateAutoTakeoffEffect()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 创建起飞效果
|
||||
Thing chemfuel = ThingMaker.MakeThing(ThingDefOf.Chemfuel);
|
||||
chemfuel.stackCount = 1;
|
||||
|
||||
Skyfaller skyfaller = SkyfallerMaker.MakeSkyfaller(Props.skyfallerLeaving, chemfuel);
|
||||
|
||||
IntVec3 takeoffPos = parent.Position;
|
||||
|
||||
if (parent.Map == null)
|
||||
{
|
||||
Log.Error("AutoLaunch: Map is null during takeoff effect creation");
|
||||
parent.Destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成 Skyfaller
|
||||
GenSpawn.Spawn(skyfaller, takeoffPos, parent.Map);
|
||||
|
||||
Log.Message($"AutoLaunch: Takeoff effect created at {takeoffPos}");
|
||||
|
||||
// 销毁原建筑
|
||||
parent.Destroy(DestroyMode.Vanish);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.Error($"AutoLaunch takeoff effect error: {ex}");
|
||||
// 如果Skyfaller创建失败,直接销毁建筑
|
||||
parent.Destroy(DestroyMode.Vanish);
|
||||
}
|
||||
}
|
||||
|
||||
// 可选:提供手动触发的Gizmo(如果自动触发失败)
|
||||
public override IEnumerable<Gizmo> CompGetGizmosExtra()
|
||||
{
|
||||
if (!hasLaunched)
|
||||
{
|
||||
Command_Action manualLaunch = new Command_Action
|
||||
{
|
||||
defaultLabel = "ManualLaunchAircraft".Translate(),
|
||||
defaultDesc = "ManualLaunchAircraftDesc".Translate(),
|
||||
icon = TexCommand.Attack,
|
||||
action = () =>
|
||||
{
|
||||
if (CanAutoLaunch())
|
||||
{
|
||||
AutoLaunchAircraft();
|
||||
}
|
||||
else
|
||||
{
|
||||
Messages.Message("CannotManualLaunch".Translate(), MessageTypeDefOf.RejectInput);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 禁用条件检查
|
||||
if (parent.HitPoints <= 0)
|
||||
{
|
||||
manualLaunch.Disable("HangarDamaged".Translate());
|
||||
}
|
||||
else if (Props.requirePower && powerComp != null && !powerComp.PowerOn)
|
||||
{
|
||||
manualLaunch.Disable("NoPower".Translate());
|
||||
}
|
||||
else if (refuelableComp != null && !refuelableComp.HasFuel)
|
||||
{
|
||||
manualLaunch.Disable("NoFuel".Translate());
|
||||
}
|
||||
|
||||
yield return manualLaunch;
|
||||
}
|
||||
}
|
||||
|
||||
public override string CompInspectStringExtra()
|
||||
{
|
||||
if (!hasLaunched)
|
||||
{
|
||||
int remainingTicks = Props.launchDelayTicks - (Find.TickManager.TicksAbs - spawnTick);
|
||||
if (remainingTicks > 0)
|
||||
{
|
||||
return "AutoLaunchIn".Translate(remainingTicks.ToStringTicksToPeriod());
|
||||
}
|
||||
else
|
||||
{
|
||||
return "AutoLaunchReady".Translate();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user