This commit is contained in:
2025-09-22 15:40:32 +08:00
parent 0ac7be78ad
commit db7e4393bc
12 changed files with 1701 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<ThingDef ParentName="BuildingBase">
<defName>CatastropheMissileSilo</defName>
<label>天灾导弹发射井</label>
<description>一个多功能导弹发射平台。它装备的武器系统既可以作为自动炮塔进行本地防御,也可以在操作员的指引下,将“天灾”级巡航导弹发射到全球任何一个角落。</description>
<thingClass>ArachnaeSwarm.Building_CatastropheMissileSilo</thingClass>
<graphicData>
<texPath>Things/Building/Security/TurretMortar_Base</texPath>
<graphicClass>Graphic_Single</graphicClass>
<drawSize>(6,6)</drawSize>
</graphicData>
<size>(2,2)</size>
<altitudeLayer>Building</altitudeLayer>
<passability>PassThroughOnly</passability>
<pathCost>50</pathCost>
<fillPercent>0.5</fillPercent>
<stealable>false</stealable>
<statBases>
<MaxHitPoints>500</MaxHitPoints>
<WorkToBuild>12000</WorkToBuild>
<Mass>800</Mass>
<Beauty>-20</Beauty>
</statBases>
<tickerType>Normal</tickerType>
<comps>
<li Class="CompProperties_Power">
<compClass>CompPowerTrader</compClass>
<basePowerConsumption>500</basePowerConsumption>
</li>
<li Class="CompProperties_Refuelable">
<fuelLabel>导弹</fuelLabel>
<fuelGizmoLabel>导弹</fuelGizmoLabel>
<outOfFuelMessage>缺少导弹</outOfFuelMessage>
<fuelFilter>
<thingDefs>
<li>ComponentSpacer</li>
</thingDefs>
</fuelFilter>
<fuelCapacity>1</fuelCapacity>
<initialFuelPercent>0</initialFuelPercent>
<autoRefuelPercent>1</autoRefuelPercent>
<showFuelGizmo>true</showFuelGizmo>
<consumeFuelOnlyWhenUsed>true</consumeFuelOnlyWhenUsed>
</li>
<li Class="CompProperties_Forbiddable"/>
<li Class="CompProperties_Breakdownable"/>
<li Class="ArachnaeSwarm.CompProperties_ForceTargetable" />
</comps>
<building>
<turretGunDef>CatastropheMissile_Weapon</turretGunDef>
<turretBurstCooldownTime>5.0</turretBurstCooldownTime>
<buildingTags>
<li>Artillery</li>
</buildingTags>
</building>
<designationCategory>Security</designationCategory>
<constructionSkillPrerequisite>8</constructionSkillPrerequisite>
<researchPrerequisites>
<li>ShipbuildingBasics</li>
</researchPrerequisites>
</ThingDef>
</Defs>

View File

@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<ThingDef ParentName="BaseBullet">
<defName>Projectile_CatastropheMissile</defName>
<label>“天灾”巡航导弹</label>
<thingClass>ArachnaeSwarm.Projectile_CruiseMissile</thingClass>
<graphicData>
<texPath>Wula/Projectile/WULA_Loitering_Munition</texPath>
<graphicClass>Graphic_Single</graphicClass>
</graphicData>
<projectile>
<damageDef>Bomb</damageDef>
<damageAmountBase>200</damageAmountBase>
<speed>30</speed>
<explosionRadius>5.9</explosionRadius>
<soundExplode>MortarBomb_Explode</soundExplode>
</projectile>
<modExtensions>
<li Class="ArachnaeSwarm.CruiseMissileProperties">
<customDamageDef>Bomb</customDamageDef>
<customDamageAmount>150</customDamageAmount>
<customExplosionRadius>5.9</customExplosionRadius>
<customSoundExplode>MortarBomb_Explode</customSoundExplode>
<useSubExplosions>true</useSubExplosions>
<subExplosionCount>8</subExplosionCount>
<subExplosionRadius>2.9</subExplosionRadius>
<subExplosionDamage>50</subExplosionDamage>
<subExplosionSpread>15</subExplosionSpread>
<subDamageDef>Bomb</subDamageDef>
<subSoundExplode>Mortar_Explode</subSoundExplode>
<bezierArcHeightFactor>0.05</bezierArcHeightFactor>
<bezierMinArcHeight>5</bezierMinArcHeight>
<bezierMaxArcHeight>20</bezierMaxArcHeight>
<bezierHorizontalOffsetFactor>0.1</bezierHorizontalOffsetFactor>
<bezierSideOffsetFactor>0.2</bezierSideOffsetFactor>
<bezierRandomOffsetScale>0.5</bezierRandomOffsetScale>
</li>
</modExtensions>
</ThingDef>
<ThingDef ParentName="BaseWeapon">
<defName>CatastropheMissile_Weapon</defName>
<label>天灾导弹武器系统</label>
<description>天灾导弹的发射系统。</description>
<techLevel>Spacer</techLevel>
<graphicData>
<texPath>Things/Building/Security/TurretMortar_Top</texPath>
<graphicClass>Graphic_Single</graphicClass>
</graphicData>
<statBases>
<RangedWeapon_Cooldown>5.0</RangedWeapon_Cooldown>
<Mass>50</Mass>
</statBases>
<verbs>
<li>
<verbClass>Verb_Shoot</verbClass>
<forceNormalTimeSpeed>false</forceNormalTimeSpeed>
<warmupTime>4.0</warmupTime>
<forcedMissRadius>9</forcedMissRadius>
<forcedMissRadiusClassicMortars>13</forcedMissRadiusClassicMortars>
<isMortar>true</isMortar>
<requireLineOfSight>false</requireLineOfSight>
<minRange>29.9</minRange>
<range>500</range>
<burstShotCount>1</burstShotCount>
<soundCast>Mortar_LaunchA</soundCast>
<muzzleFlashScale>16</muzzleFlashScale>
<consumeFuelPerShot>1</consumeFuelPerShot>
<targetParams>
<canTargetLocations>true</canTargetLocations>
</targetParams>
</li>
</verbs>
<verbs>
<li>
<verbClass>ArachnaeSwarm.Verb_LaunchCatastropheMissile</verbClass>
<hasStandardCommand>true</hasStandardCommand>
<defaultProjectile>Projectile_CatastropheMissile</defaultProjectile> <!-- Placeholder local projectile -->
<warmupTime>3.0</warmupTime>
<forcedMissRadius>1</forcedMissRadius>
<isMortar>true</isMortar>
<requireLineOfSight>false</requireLineOfSight>
<minRange>29.9</minRange>
<burstShotCount>1</burstShotCount>
<range>500</range>
<soundCast>Shot_Autocannon</soundCast>
<muzzleFlashScale>16</muzzleFlashScale>
<targetParams>
<canTargetLocations>true</canTargetLocations>
</targetParams>
</li>
</verbs>
</ThingDef>
</Defs>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<WorldObjectDef>
<defName>CatastropheMissile_Flying</defName>
<label>巡航导弹</label>
<worldObjectClass>ArachnaeSwarm.WorldObject_CatastropheMissile</worldObjectClass>
<texture>World/WorldObjects/Caravan</texture> <!-- Placeholder texture -->
<expandingIcon>true</expandingIcon>
<useDynamicDrawer>true</useDynamicDrawer>
</WorldObjectDef>
</Defs>

View File

@@ -0,0 +1,468 @@
# 天灾导弹防御塔 - C# 实现计划
本文档包含构建“天灾导弹防御塔”所需的全部C#类的源代码。这些代码基于 `Rimatomics` 的跨地图打击框架,并与我们自己的 `Projectile_CruiseMissile` 弹头相结合。
---
## 1. 建筑类: `Building_CatastropheMissileSilo.cs`
**路径**: `Source/ArachnaeSwarm/Buildings/Building_CatastropheMissileSilo.cs`
**功能**: 这是导弹发射井的建筑本体,负责处理玩家的瞄准指令。它几乎是 `Building_Railgun` 的翻版,但进行了一些重命名和命名空间调整。
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
using Verse.Sound;
namespace ArachnaeSwarm
{
[StaticConstructorOnStartup]
public class Building_CatastropheMissileSilo : Building
{
public CompPowerTrader powerComp;
public CompRefuelable refuelableComp;
public Verb_LaunchCatastropheMissile verb;
private GlobalTargetInfo longTargetInt;
private List<Building_CatastropheMissileSilo> selectedSilos;
public static readonly Texture2D FireMissionTex = ContentFinder<Texture2D>.Get("UI/Commands/Attack", true);
public override void SpawnSetup(Map map, bool respawningAfterLoad)
{
base.SpawnSetup(map, respawningAfterLoad);
this.powerComp = base.GetComp<CompPowerTrader>();
this.refuelableComp = base.GetComp<CompRefuelable>();
// This is a placeholder, verb will be initialized from the weapon ThingDef
this.verb = (Verb_LaunchCatastropheMissile)Activator.CreateInstance(typeof(Verb_LaunchCatastropheMissile));
this.verb.caster = this;
}
public override void ExposeData()
{
base.ExposeData();
Scribe_TargetInfo.Look(ref this.longTargetInt, "longTargetInt");
}
public override IEnumerable<Gizmo> GetGizmos()
{
foreach (Gizmo c in base.GetGizmos())
{
yield return c;
}
Command_Action launch = new Command_Action
{
defaultLabel = "CommandFireMission".Translate(),
defaultDesc = "CommandFireMissionDesc".Translate(),
icon = FireMissionTex,
action = new Action(this.StartChoosingDestination)
};
if (!CanFire())
{
launch.Disable("CannotFire".Translate() + ": " + GetDisabledReason());
}
yield return launch;
}
private bool CanFire()
{
return powerComp.PowerOn && refuelableComp.HasFuel;
}
private string GetDisabledReason()
{
if (!powerComp.PowerOn) return "NoPower".Translate().CapitalizeFirst();
if (!refuelableComp.HasFuel) return "NoFuel".Translate().CapitalizeFirst();
return "Unknown";
}
private void StartChoosingDestination()
{
this.selectedSilos = Find.Selector.SelectedObjects.OfType<Building_CatastropheMissileSilo>().ToList();
CameraJumper.TryJump(CameraJumper.GetWorldTarget(this), CameraJumper.MovementMode.Pan);
Find.WorldSelector.ClearSelection();
Find.WorldTargeter.BeginTargeting(
new Func<GlobalTargetInfo, bool>(this.ChoseWorldTarget),
true, // Can target self
FireMissionTex,
true,
() => GenDraw.DrawWorldRadiusRing(this.Map.Tile, this.MaxWorldRange),
(GlobalTargetInfo t) => "Select target",
null, null, true);
}
private bool ChoseWorldTarget(GlobalTargetInfo target)
{
if (!target.IsValid)
{
Messages.Message("MessageTargetInvalid".Translate(), MessageTypeDefOf.RejectInput, true);
return false;
}
int distance = Find.WorldGrid.TraversalDistanceBetween(this.Map.Tile, target.Tile, true, int.MaxValue);
if (distance > this.MaxWorldRange)
{
Messages.Message("MessageTargetBeyondMaximumRange".Translate(), this, MessageTypeDefOf.RejectInput, true);
return false;
}
MapParent mapParent = target.WorldObject as MapParent;
if (mapParent != null && mapParent.HasMap)
{
Map targetMap = mapParent.Map;
var originalMap = base.Map;
Action onFinished = () => {
if (Current.Game.CurrentMap != originalMap) Current.Game.CurrentMap = originalMap;
};
Current.Game.CurrentMap = targetMap;
Find.Targeter.BeginTargeting(
new TargetingParameters { canTargetLocations = true },
(LocalTargetInfo localTarget) =>
{
foreach (var silo in this.selectedSilos)
{
silo.FireMission(targetMap.Tile, localTarget, targetMap.uniqueID);
}
},
null, onFinished, FireMissionTex, true);
return true;
}
else // For non-map targets (caravans, sites)
{
foreach (var silo in this.selectedSilos)
{
silo.FireMission(target.Tile, new LocalTargetInfo(target.Cell), -1);
}
return true;
}
}
public void FireMission(int tile, LocalTargetInfo targ, int mapId)
{
if (!targ.IsValid)
{
this.longTargetInt = GlobalTargetInfo.Invalid;
return;
}
Map targetMap = (mapId != -1) ? Find.Maps.FirstOrDefault(m => m.uniqueID == mapId) : null;
this.longTargetInt = new GlobalTargetInfo(targ.Cell, targetMap);
this.verb.verbProps.defaultProjectile.GetModExtension<CruiseMissileProperties>(); // Ensure verb has properties.
this.verb.TryStartCastOn(this.longTargetInt);
}
public int MaxWorldRange => 99999; // Effectively global range
public GlobalTargetInfo LongTarget => longTargetInt;
}
}
```
---
## 2. 动作类: `Verb_LaunchCatastropheMissile.cs`
**路径**: `Source/ArachnaeSwarm/Verbs/Verb_LaunchCatastropheMissile.cs`
**功能**: 定义发射动作。它会创建 `WorldObject_CatastropheMissile` 并将其发射到世界地图。
```csharp
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
namespace ArachnaeSwarm
{
public class Verb_LaunchCatastropheMissile : Verb_Shoot
{
public override bool CanHitTargetFrom(IntVec3 root, LocalTargetInfo targ)
{
return true; // Always true for world-map targeting
}
protected override bool TryCastShot()
{
Building_CatastropheMissileSilo silo = this.caster as Building_CatastropheMissileSilo;
if (silo == null || !silo.LongTarget.IsValid) return false;
WorldObject_CatastropheMissile missile = (WorldObject_CatastropheMissile)WorldObjectMaker.MakeWorldObject(
DefDatabase<WorldObjectDef>.GetNamed("CatastropheMissile_Flying")
);
missile.Tile = silo.Map.Tile;
missile.destinationTile = silo.LongTarget.Tile;
missile.destinationCell = silo.LongTarget.Cell;
missile.Projectile = this.verbProps.defaultProjectile;
Find.WorldObjects.Add(missile);
// Consume fuel
silo.refuelableComp.ConsumeFuel(silo.refuelableComp.Props.fuelConsumptionRate);
// Visual/Sound effects at launch site
MoteMaker.MakeStaticMote(silo.TrueCenter(), silo.Map, ThingDefOf.Mote_ExplosionFlash, 10f);
SoundDefOf.RocketLaunch.PlayOneShot(new TargetInfo(silo.Position, silo.Map));
return true;
}
}
}
```
---
## 3. 飞行物类: `WorldObject_CatastropheMissile.cs`
**路径**: `Source/ArachnaeSwarm/World/WorldObject_CatastropheMissile.cs`
**功能**: 模拟导弹在世界地图上的飞行,并在抵达时生成 `Projectile_CruiseMissile`
```csharp
using RimWorld.Planet;
using UnityEngine;
using Verse;
using RimWorld;
namespace ArachnaeSwarm
{
public class WorldObject_CatastropheMissile : WorldObject
{
public int destinationTile = -1;
public IntVec3 destinationCell = IntVec3.Invalid;
public ThingDef Projectile;
private int initialTile = -1;
private float traveledPct;
private const float TravelSpeed = 0.0002f; // Faster than sabot
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref destinationTile, "destinationTile", 0);
Scribe_Values.Look(ref destinationCell, "destinationCell");
Scribe_Defs.Look(ref Projectile, "Projectile");
Scribe_Values.Look(ref initialTile, "initialTile", 0);
Scribe_Values.Look(ref traveledPct, "traveledPct", 0f);
}
public override void PostAdd()
{
base.PostAdd();
this.initialTile = this.Tile;
}
private Vector3 StartPos => Find.WorldGrid.GetTileCenter(this.initialTile);
private Vector3 EndPos => Find.WorldGrid.GetTileCenter(this.destinationTile);
public override Vector3 DrawPos => Vector3.Slerp(StartPos, EndPos, traveledPct);
public override void Tick()
{
base.Tick();
traveledPct += TravelSpeed / GenMath.SphericalDistance(StartPos.normalized, EndPos.normalized);
if (traveledPct >= 1f)
{
Arrived();
}
}
private void Arrived()
{
Map targetMap = Current.Game.FindMap(this.destinationTile);
if (targetMap != null)
{
// Target is a loaded map, spawn the projectile to hit it
IntVec3 entryCell = Find.WorldGrid.GetRotatedPos(new IntVec2(0, 1), targetMap.info.parent.Rotation).ToIntVec3() * (targetMap.Size.x / 2);
entryCell.y = 0;
entryCell.z += targetMap.Size.z / 2;
Projectile_CruiseMissile missile = (Projectile_CruiseMissile)GenSpawn.Spawn(this.Projectile, entryCell, targetMap, WipeMode.Vanish);
missile.Launch(this, this.destinationCell, this.destinationCell, ProjectileHitFlags.IntendedTarget);
}
else
{
// Target is not a loaded map (e.g., caravan, site), do direct damage
GenExplosion.DoExplosion(
this.destinationCell,
null, // No map
this.Projectile.GetModExtension<CruiseMissileProperties>()?.customExplosionRadius ?? 5f,
this.Projectile.GetModExtension<CruiseMissileProperties>()?.customDamageDef ?? DamageDefOf.Bomb,
null, // Launcher
this.Projectile.GetModExtension<CruiseMissileProperties>()?.customDamageAmount ?? 50
);
}
Find.WorldObjects.Remove(this);
}
}
}
---
## 4. XML
XML定义
### 4.1 : `ThingDef_Building_CatastropheMissileSilo.xml`
****: `1.6/Defs/ThingDefs_Buildings/ThingDef_Building_CatastropheMissileSilo.xml`
****: `Building_CatastropheMissileSilo`
```xml
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<ThingDef ParentName="BuildingBase">
<defName>CatastropheMissileSilo</defName>
<label></label>
<description></description>
<thingClass>ArachnaeSwarm.Building_CatastropheMissileSilo</thingClass>
<graphicData>
<texPath>Things/Building/Security/ShipMissileTurret</texPath> <!-- Placeholder texture -->
<graphicClass>Graphic_Single</graphicClass>
<drawSize>(3,3)</drawSize>
</graphicData>
<size>(3,3)</size>
<statBases>
<MaxHitPoints>500</MaxHitPoints>
<WorkToBuild>8000</WorkToBuild>
<Mass>1000</Mass>
<Flammability>0.5</Flammability>
</statBases>
<costList>
<Steel>400</Steel>
<Plasteel>150</Plasteel>
<ComponentIndustrial>10</ComponentIndustrial>
<ComponentSpacer>4</ComponentSpacer>
</costList>
<comps>
<li Class="CompProperties_Power">
<compClass>CompPowerTrader</compClass>
<basePowerConsumption>500</basePowerConsumption>
</li>
<li Class="CompProperties_Refuelable">
<fuelConsumptionRate>10</fuelConsumptionRate> <!-- How much fuel per launch -->
<fuelCapacity>10.0</fuelCapacity>
<fuelFilter>
<thingDefs>
<li>Chemfuel</li>
</thingDefs>
</fuelFilter>
<showFuelGizmo>true</showFuelGizmo>
</li>
</comps>
<verbs>
<!-- This defines the weapon itself -->
<li>
<verbClass>ArachnaeSwarm.Verb_LaunchCatastropheMissile</verbClass>
<hasStandardCommand>false</hasStandardCommand>
<defaultProjectile>Projectile_CatastropheMissile</defaultProjectile>
</li>
</verbs>
<building>
<ai_combatDangerous>true</ai_combatDangerous>
</building>
<researchPrerequisites>
<li>ShipbuildingBasics</li> <!-- Placeholder research -->
</researchPrerequisites>
</ThingDef>
</Defs>
```
### 4.2 武器/投射物定义: `ThingDef_Projectile_CatastropheMissile.xml`
**路径**: `1.6/Defs/ThingDefs_Misc/ThingDef_Projectile_CatastropheMissile.xml`
**功能**: 定义导弹本身 (`Projectile_CatastropheMissile`) 和它作为投射物的行为 (`ThingDef` of `Projectile_CruiseMissile`)。这里是配置集束弹头和弹道参数的地方。
```xml
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<ThingDef ParentName="BaseBullet">
<defName>Projectile_CatastropheMissile</defName>
<label>“天灾”巡航导弹</label>
<thingClass>ArachnaeSwarm.Projectile_CruiseMissile</thingClass>
<graphicData>
<texPath>Things/Projectile/Missile</texPath> <!-- Placeholder texture -->
<graphicClass>Graphic_Single</graphicClass>
</graphicData>
<projectile>
<damageDef>Bomb</damageDef>
<damageAmountBase>200</damageAmountBase>
<speed>10</speed> <!-- This speed is relative to the bezier curve, not linear -->
<explosionRadius>5.9</explosionRadius>
<soundExplode>MortarBomb_Explode</soundExplode>
</projectile>
<modExtensions>
<li Class="ArachnaeSwarm.CruiseMissileProperties">
<!-- Main Explosion -->
<customDamageDef>Bomb</customDamageDef>
<customDamageAmount>150</customDamageAmount>
<customExplosionRadius>5.9</customExplosionRadius>
<customSoundExplode>MortarBomb_Explode</customSoundExplode>
<!-- Sub Explosions (Cluster) -->
<useSubExplosions>true</useSubExplosions>
<subExplosionCount>8</subExplosionCount>
<subExplosionRadius>2.9</subExplosionRadius>
<subExplosionDamage>50</subExplosionDamage>
<subExplosionSpread>15</subExplosionSpread>
<subDamageDef>Bomb</subDamageDef>
<subSoundExplode>Fragment_Explode</subSoundExplode>
<!-- Trajectory Parameters -->
<bezierArcHeightFactor>0.05</bezierArcHeightFactor>
<bezierMinArcHeight>5</bezierMinArcHeight>
<bezierMaxArcHeight>20</bezierMaxArcHeight>
<bezierHorizontalOffsetFactor>0.1</bezierHorizontalOffsetFactor>
<bezierSideOffsetFactor>0.2</bezierSideOffsetFactor>
<bezierRandomOffsetScale>0.5</bezierRandomOffsetScale>
</li>
</modExtensions>
</ThingDef>
</Defs>
```
### 4.3 飞行物定义: `WorldObjectDef_CatastropheMissile.xml`
**路径**: `1.6/Defs/WorldObjectDefs/WorldObjectDef_CatastropheMissile.xml`
**功能**: 定义在世界地图上飞行的那个导弹实体。
```xml
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<WorldObjectDef>
<defName>CatastropheMissile_Flying</defName>
<label>巡航导弹</label>
<worldObjectClass>ArachnaeSwarm.WorldObject_CatastropheMissile</worldObjectClass>
<texture>World/WorldObjects/Missile</texture> <!-- Placeholder texture -->
<expandingIcon>true</expandingIcon>
<canBeTargeted>false</canBeTargeted>
<canHavePlayerEnemyRelation>false</canHavePlayerEnemyRelation>
<useDynamicDrawer>true</useDynamicDrawer>
</WorldObjectDef>
</Defs>
```

View File

@@ -0,0 +1,743 @@
# Rimatomics 电磁炮战略打击系统技术文档
## 1. 引言
本文档旨在详细解析 Rimatomics Mod 中的电磁炮Railgun远程战略打击系统的实现机制。该系统允许玩家对世界地图上的任意已加载的地图进行精确的火力投送是游戏中一种强大的后期战略武器。
我们将通过分析其三个核心 C# 类:`Building_Railgun``Verb_Railgun``WorldObject_Sabot`,来深入理解其功能、设计模式以及类之间的交互。
## 2. 核心组件概览
整个系统由三个主要部分协同工作,各司其职:
* **`Building_Railgun`**: 炮塔建筑本身,是整个系统的用户交互入口和武器平台。它负责处理玩家的瞄准指令,管理武器的属性(如射程、精度),并发起射击流程。
* **`Verb_Railgun`**: 定义了电磁炮的“射击”这一具体动作。它能够智能区分常规的本地射击和需要跨地图飞行的远程打击,并根据不同模式执行相应的逻辑。
* **`WorldObject_Sabot`**: 一个在世界地图上存在的临时对象,用于模拟炮弹在飞向目标过程中的状态。它负责处理跨地图飞行、轨迹计算,并在抵达目标后触发最终的命中效果。
## 3. 类详解
### 3.1 `Building_Railgun`
`Building_Railgun` 是电磁炮塔的建筑类,继承自 `Building_EnergyWeapon`。它是玩家能直接看到并与之交互的实体。
#### 功能概述
此类作为系统的交互枢纽,主要负责以下功能:
* **提供玩家操作界面**:通过 Gizmo操作按钮让玩家启动“火控任务”。
* **处理目标选择流程**:引导玩家从世界地图选择目标区域,再到目标地图内选择精确弹着点。
* **管理武器状态与属性**:存储远程目标信息,并根据已安装的升级动态计算射程、散布等属性。
* **发起射击指令**在获取有效目标后命令自身的武器组件Verb开火。
#### 源代码
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using Multiplayer.API;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
using Verse.Sound;
namespace Rimatomics
{
[StaticConstructorOnStartup]
public class Building_Railgun : Building_EnergyWeapon
{
public override bool TurretBased
{
get
{
return true;
}
}
public override Vector3 TipOffset
{
get
{
Vector3 pos = this.DrawPos;
Vector3 vecOffset = new Vector3(0f, 1f, 5f);
vecOffset = vecOffset.RotatedBy(this.TurretRotation);
return pos + vecOffset;
}
}
public override bool CanFireWhileRoofed()
{
// For remote fire missions, cannot be roofed. For local targets, it's allowed.
if (this.longTargetInt == GlobalTargetInfo.Invalid)
{
return true;
}
return !base.Position.Roofed(base.Map);
}
public float RangeToWorldTarget
{
get
{
return (float)Find.WorldGrid.TraversalDistanceBetween(base.Map.Tile, this.longTarget.Tile, true, int.MaxValue, false);
}
}
public void TryChamberRound()
{
if (this.magazine.NullOrEmpty<ThingDef>())
{
return;
}
CompChangeableProjectile comp = this.gun.TryGetComp<CompChangeableProjectile>();
if (!comp.Loaded)
{
comp.LoadShell(this.magazine.FirstOrDefault<ThingDef>(), 1);
this.magazine.RemoveAt(0);
}
}
public override void MuzzleFlash()
{
Mote flash = (Mote)ThingMaker.MakeThing(Building_Railgun.Mote_RailgunMuzzleFlash, null);
flash.Scale = 5f;
flash.exactRotation = this.TurretRotation;
flash.exactPosition = this.TipOffset;
GenSpawn.Spawn(flash, base.Position, base.Map, WipeMode.Vanish);
Vector3 vecOffset = new Vector3(1f, 1f, -1f).RotatedBy(this.TurretRotation);
FleckMaker.ThrowSmoke(this.DrawPos + vecOffset, base.Map, 1.5f);
Vector3 vecOffset2 = new Vector3(-1f, 1f, -1f).RotatedBy(this.TurretRotation);
FleckMaker.ThrowSmoke(this.DrawPos + vecOffset2, base.Map, 1.5f);
}
public override IEnumerable<Gizmo> GetGizmos()
{
foreach (Gizmo c in base.GetGizmos())
{
yield return c;
}
Command_Action launch = new Command_Action
{
defaultLabel = "critCommandFireMission".Translate(),
defaultDesc = "critCommandFireMissionDesc".Translate(),
icon = Building_Railgun.FireMissionTex,
action = new Action(this.StartChoosingDestination)
};
if (base.Spawned && base.Position.Roofed(base.Map))
{
launch.Disable("CannotFire".Translate() + ": " + "Roofed".Translate().CapitalizeFirst());
}
yield return launch;
}
private void StartChoosingDestination()
{
// Record all selected railguns to fire them simultaneously
this.selectedRailguns = Find.Selector.SelectedObjects.OfType<Building_Railgun>().ToList<Building_Railgun>();
CameraJumper.TryJump(CameraJumper.GetWorldTarget(this), CameraJumper.MovementMode.Pan);
Find.WorldSelector.ClearSelection();
int tile = base.Map.Tile;
Find.WorldTargeter.BeginTargeting(
new Func<GlobalTargetInfo, bool>(this.ChoseWorldTarget), // Callback when target is chosen
false,
Building_Railgun.FireMissionTex,
true,
() => GenDraw.DrawWorldRadiusRing(tile, this.WorldRange, null), // Draws the range ring
null, null, null, false);
}
public static TargetingParameters ForFireMission()
{
// In multiplayer, disallow targeting mobile pawns to prevent desync issues.
if (MP.IsInMultiplayer)
{
return new TargetingParameters
{
canTargetPawns = false,
canTargetBuildings = false,
canTargetLocations = true
};
}
return new TargetingParameters
{
canTargetPawns = true,
canTargetBuildings = true,
canTargetLocations = true
};
}
private bool ChoseWorldTarget(GlobalTargetInfo target)
{
if (!target.IsValid)
{
Messages.Message("MessageRailgunTargetInvalid".Translate(), MessageTypeDefOf.RejectInput, true);
return false;
}
int distance = Find.WorldGrid.TraversalDistanceBetween(base.Map.Tile, target.Tile, true, int.MaxValue, false);
if (distance > this.WorldRange)
{
Messages.Message("MessageTargetBeyondMaximumRange".Translate(), this, MessageTypeDefOf.RejectInput, true);
return false;
}
MapParent mapParent = target.WorldObject as MapParent;
if (mapParent != null && mapParent.HasMap)
{
if (mapParent.Map == base.Map)
{
Messages.Message("MessageRailgunCantTargetMyMap".Translate(), MessageTypeDefOf.RejectInput, true);
return false;
}
// --- Refactored Code Block ---
// This block handles the logic for fine-grained targeting within the destination map.
Map targetMap = mapParent.Map;
var originalMap = base.Map;
// Action to execute when targeting is finished or cancelled.
Action onFinished = () => {
if (Current.Game.CurrentMap != originalMap)
{
Current.Game.CurrentMap = originalMap;
}
};
// Switch to the target map to allow the player to pick a precise location.
Current.Game.CurrentMap = targetMap;
Find.Targeter.BeginTargeting(
Building_Railgun.ForFireMission(),
(LocalTargetInfo localTarget) => // Lambda for when a local target is selected
{
// Assign the fire mission to all selected railguns.
foreach (Building_Railgun railgun in this.selectedRailguns)
{
railgun.FireMission(targetMap.Tile, localTarget, targetMap.uniqueID);
}
},
null, // Action on highlight
onFinished, // Action on finish/cancel
Building_Railgun.FireMissionTex,
true);
return true;
}
else
{
Messages.Message("MessageRailgunNeedsMap".Translate(), MessageTypeDefOf.RejectInput, true);
return false;
}
}
[SyncMethod(SyncContext.None)]
public void FireMission(int tile, LocalTargetInfo targ, int map)
{
if (!targ.IsValid)
{
this.longTargetInt = GlobalTargetInfo.Invalid;
return;
}
GlobalTargetInfo newtarget = targ.ToGlobalTargetInfo(Find.Maps.FirstOrDefault((Map x) => x.uniqueID == map));
int distance = Find.WorldGrid.TraversalDistanceBetween(base.Map.Tile, tile, true, int.MaxValue, false);
if (distance > this.WorldRange)
{
Messages.Message("MessageTargetBeyondMaximumRange".Translate(), this, MessageTypeDefOf.RejectInput, true);
return;
}
if (this.holdFire)
{
Messages.Message("MessageTurretWontFireBecauseHoldFire".Translate(this.def.label), this, MessageTypeDefOf.RejectInput, true);
return;
}
if (this.longTargetInt != newtarget)
{
this.longTargetInt = newtarget;
if (this.burstCooldownTicksLeft <= 0)
{
this.TryStartShootSomething();
}
SoundDefOf.TurretAcquireTarget.PlayOneShot(new TargetInfo(base.Position, base.Map, false));
}
}
public override float PulseSize
{
get
{
float f = base.GunProps.EnergyWep.PulseSizeScaled;
if (this.UG.HasUpgrade(DubDef.MEPS))
{
f *= 1.15f;
}
if (this.UG.HasUpgrade(DubDef.ERS))
{
f *= 0.85f; // Equivalent to f -= 0.15f * f
}
return f;
}
}
public override int WorldRange
{
get
{
// In space (SoS2 compatibility), range is unlimited.
if (base.Map != null && this.space != null && base.Map.Biome == this.space)
{
return 99999;
}
int range = base.GunProps.EnergyWep.WorldRange;
if (this.UG.HasUpgrade(DubDef.TargetingChip))
{
range += 10;
}
if (this.UG.HasUpgrade(DubDef.MEPS))
{
range += 10;
}
return range;
}
}
public int spread
{
get
{
int v = 6;
if (this.UG.HasUpgrade(DubDef.TargetingChip))
{
v -= 3;
}
return v;
}
}
public static readonly Texture2D FireMissionTex = ContentFinder<Texture2D>.Get("Rimatomics/UI/FireMission", true);
public static ThingDef Mote_RailgunMuzzleFlash = ThingDef.Named("Mote_RailgunMuzzleFlash");
private List<Building_Railgun> selectedRailguns;
public BiomeDef space = DefDatabase<BiomeDef>.GetNamed("OuterSpaceBiome", false);
}
}
```
#### 关键方法和属性详解
* **`GetGizmos()`**: 为炮塔添加“火控任务” (Fire Mission) 按钮,这是远程打击流程的起点。如果炮塔被屋顶遮挡,该按钮会被禁用。
* **`StartChoosingDestination()`**: 响应按钮点击,将视角切换到世界地图,并启动一个带射程圈的目标选择器,让玩家选择目标地块。它支持多炮塔同时选择。
* **`ChoseWorldTarget(GlobalTargetInfo target)`**: 当玩家在世界地图上选择目标后的回调函数。它会进行一系列合法性验证(如射程、是否为当前地图等),如果通过,则将视角切换到目标地图,让玩家选择精确落点。
* **`FireMission(int tile, LocalTargetInfo targ, int map)`**: 这是目标选择的最后一步。它将最终的、精确的目标信息包含地块ID和地图内坐标存入 `longTargetInt` 变量,并立即尝试触发射击 (`TryStartShootSomething()`)。该方法支持多人游戏同步。
* **`WorldRange` (get)**: 一个计算属性返回炮塔在世界地图上的最大射程。基础射程定义在XML中并会受到 `TargetingChip``MEPS` 等升级的加成。
* **`spread` (get)**: 计算属性,返回炮弹的散布半径。`TargetingChip` 升级可以减小此值,提高精度。
* **`CanFireWhileRoofed()`**: 重写方法,规定在执行远程打击 (`longTargetInt` 有效) 时,炮塔不能处于屋顶之下。
### 3.2 `Verb_Railgun`
`Verb_Railgun` 继承自 `Verb_RimatomicsVerb`,它定义了电磁炮武器的“射击”这个核心动作。它不处理玩家交互,只负责执行射击逻辑。
#### 功能概述
这个类的主要职责是根据炮塔当前的目标状态(本地目标 vs 远程目标),决定执行哪种射击模式。
* **动态炮弹选择**: 根据 `CompChangeableProjectile` 组件的状态,决定是发射已装填的特殊炮弹还是默认炮弹。
* **射击模式分发**: 检查是否存在远程目标 (`longTarget`),如果存在,则执行“火控任务”流程;否则,执行标准的本地射击。
* **远程打击发起**: 在“火控任务”模式下,负责创建并初始化 `WorldObject_Sabot`,将其发射到世界地图。
* **本地打击后效**: 为本地射击附加额外的效果,如能量消耗、数据收集和屏幕震动。
#### 源代码
```csharp
using System;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
namespace Rimatomics
{
[StaticConstructorOnStartup]
public class Verb_Railgun : Verb_RimatomicsVerb
{
public override ThingDef Projectile
{
get
{
ThingWithComps equipmentSource = base.EquipmentSource;
CompChangeableProjectile comp = (equipmentSource != null) ? equipmentSource.GetComp<CompChangeableProjectile>() : null;
if (comp != null && comp.Loaded)
{
return comp.Projectile;
}
return this.verbProps.defaultProjectile;
}
}
public override bool CanHitTargetFrom(IntVec3 root, LocalTargetInfo targ)
{
// If a long-range target is set, we can "hit" it regardless of local range.
if (base.GetWep.longTarget.IsValid)
{
return true;
}
return base.CanHitTargetFrom(root, targ);
}
protected bool TryCastFireMission()
{
Building_Railgun railgun = this.caster as Building_Railgun;
// Create the flying sabot object on the world map
WorldObject_Sabot sabot = (WorldObject_Sabot)WorldObjectMaker.MakeWorldObject(DefDatabase<WorldObjectDef>.GetNamed("Sabot", true));
sabot.railgun = railgun;
sabot.Tile = railgun.Map.Tile;
sabot.destinationTile = railgun.longTarget.Tile;
sabot.destinationCell = railgun.longTarget.Cell;
sabot.spread = railgun.spread;
sabot.Projectile = this.Projectile;
Find.WorldObjects.Add(sabot);
// Post-launch effects and data gathering
railgun.GatherData("PPCWeapon", 5f);
railgun.GatherData("PPCFireMission", 10f);
railgun.GatherData("PPCRailgun", 10f);
railgun.PrototypeBang(railgun.GunProps.EnergyWep.PrototypeFailureChance);
railgun.MuzzleFlash();
Find.CameraDriver.shaker.SetMinShake(0.1f);
// Spawn a dummy projectile that flies off-map. This is for visual effect only.
Vector3 shellDirection = Vector3.forward.RotatedBy(railgun.TurretRotation);
IntVec3 outcell = (railgun.DrawPos + shellDirection * 500f).ToIntVec3();
Projectile projectile2 = (Projectile)GenSpawn.Spawn(this.Projectile, railgun.Position, this.caster.Map, WipeMode.Vanish);
projectile2.Launch(railgun, railgun.DrawPos, outcell, null, ProjectileHitFlags.None, false, base.EquipmentSource, null);
// Handle shell consumption
CompChangeableProjectile comp = base.EquipmentSource?.GetComp<CompChangeableProjectile>();
if (comp != null)
{
comp.Notify_ProjectileLaunched();
}
railgun.DissipateCharge(railgun.PulseSize);
return true;
}
public override bool TryCastShot()
{
Building_Railgun railgun = this.caster as Building_Railgun;
if (!railgun.top.TargetInSights)
{
return false;
}
if (this.Projectile == null)
{
return false;
}
bool shotResult;
// --- Shooting Mode Dispatch ---
if (railgun.longTarget.IsValid)
{
// Execute long-range fire mission
shotResult = this.TryCastFireMission();
}
else
{
// Execute standard local shot
shotResult = base.TryCastShot();
if (shotResult)
{
railgun.DissipateCharge(railgun.PulseSize);
railgun.GatherData("PPCWeapon", 5f);
railgun.GatherData("PPCRailgun", 10f);
railgun.PrototypeBang(railgun.GunProps.EnergyWep.PrototypeFailureChance);
railgun.MuzzleFlash();
Find.CameraDriver.shaker.SetMinShake(0.1f);
}
}
// Chamber the next round after firing
railgun.TryChamberRound();
return shotResult;
}
}
}
```
#### 关键方法和属性详解
* **`Projectile` (get)**: 这是一个动态属性,用于获取当前应该发射的炮弹类型。它会优先返回 `CompChangeableProjectile` 中已装填的炮弹,如果未装填,则返回在 XML 中定义的默认炮弹。
* **`CanHitTargetFrom(...)`**: 重写了基类方法。当炮塔被赋予了一个远程目标 (`longTarget.IsValid`) 时,该方法直接返回 `true`,绕过了所有常规的射程和视线检查,确保远程打击流程可以启动。
* **`TryCastShot()`**: 这是射击动作的入口点。它的核心逻辑是检查 `railgun.longTarget` 是否有效。
* 如果**有效**,说明是远程打击任务,它便调用 `TryCastFireMission()`
* 如果**无效**,说明是常规的本地瞄准,它就调用基类的 `base.TryCastShot()` 来发射普通炮弹,并附加一系列开火后效。
* **`TryCastFireMission()`**: 这是发起远程打击的核心。它不直接生成命中目标的炮弹,而是:
1. 创建一个 `WorldObject_Sabot` 实例。
2. 将目标地块、精确坐标、炮弹类型、散布等关键信息从 `Building_Railgun` 传递给 `sabot` 对象。
3.`sabot` 添加到世界对象管理器 (`Find.WorldObjects.Add(sabot)`),让其开始在世界地图上“飞行”。
4. 触发炮口闪光、能量消耗、屏幕震动等本地开火效果。
5. 生成一个飞向地图外的“虚拟”炮弹,仅用于视觉表现,它不会造成任何伤害。
### 3.3 `WorldObject_Sabot`
`WorldObject_Sabot` 继承自 `WorldObject`,是远程打击流程中的“飞行”阶段的执行者。它是一个临时的、在世界地图上存在的实体,模拟了炮弹从发射点到目标点的飞行过程。
#### 功能概述
此类完全独立于发射它的炮塔,其核心职责是在世界地图上完成一段旅程,并在抵达终点时触发命中效果。
* **飞行轨迹模拟**: 通过 `DrawPos` 属性和 `Tick` 方法,平滑地计算并更新自身在世界地图上的位置,实现飞行-动画效果。
* **状态持久化**: 通过 `ExposeData` 方法保存所有关键信息(如起点、终点、飞行进度、炮弹类型),确保在游戏存读档后飞行可以继续。
* **命中触发**: 在飞行到达终点后,`Arrived` 方法负责在目标地图上生成真正的 `Projectile`(炮弹),并让其从地图边缘发射,命中最终的精确弹着点。
* **自我销毁**: 完成命中逻辑后,将自身从世界对象管理器中移除。
* **Mod兼容性**: 能够识别并处理 "Save Our Ship 2" Mod 中的轨道飞船,实现地对空、空对地和空对空打击。
#### 源代码
```csharp
using System;
using System.Linq;
using RimWorld.Planet;
using UnityEngine;
using Verse;
namespace Rimatomics
{
public class WorldObject_Sabot : WorldObject
{
private Vector3 Start
{
get
{
Vector3 startPos = Find.WorldGrid.GetTileCenter(this.initialTile);
// SoS2 compatibility: if a ship is at the tile, use its position.
if (HarmonyPatches.SoS)
{
WorldObject ship = Find.World.worldObjects.AllWorldObjects.FirstOrDefault(o =>
(o.def.defName.Equals("ShipOrbiting") || o.def.defName.Equals("SiteSpace")) && o.Tile == this.initialTile);
if (ship != null)
{
startPos = ship.DrawPos;
}
}
return startPos;
}
}
private Vector3 End
{
get
{
Vector3 endPos = Find.WorldGrid.GetTileCenter(this.destinationTile);
// SoS2 compatibility
if (HarmonyPatches.SoS)
{
WorldObject ship = Find.World.worldObjects.AllWorldObjects.FirstOrDefault(o =>
(o.def.defName.Equals("ShipOrbiting") || o.def.defName.Equals("SiteSpace")) && o.Tile == this.destinationTile);
if (ship != null)
{
endPos = ship.DrawPos;
}
}
return endPos;
}
}
public override Vector3 DrawPos
{
get
{
// Slerp for a smooth curve over the planet's surface
return Vector3.Slerp(this.Start, this.End, this.traveledPct);
}
}
private float TraveledPctStepPerTick
{
get
{
Vector3 start = this.Start;
Vector3 end = this.End;
if (start == end) return 1f;
float distance = GenMath.SphericalDistance(start.normalized, end.normalized);
if (distance == 0f) return 1f;
// Travel speed is constant
return TravelSpeed / distance;
}
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look<int>(ref this.destinationTile, "destinationTile", 0, false);
Scribe_Values.Look<IntVec3>(ref this.destinationCell, "destinationCell", default(IntVec3), false);
Scribe_Values.Look<bool>(ref this.arrived, "arrived", false, false);
Scribe_Values.Look<int>(ref this.initialTile, "initialTile", 0, false);
Scribe_Values.Look<float>(ref this.traveledPct, "traveledPct", 0f, false);
Scribe_Defs.Look<ThingDef>(ref this.Projectile, "Projectile");
Scribe_References.Look<Thing>(ref this.railgun, "railgun");
Scribe_Values.Look<int>(ref this.spread, "spread", 1, false);
}
public override void PostAdd()
{
base.PostAdd();
this.initialTile = base.Tile;
}
public override void Tick()
{
base.Tick();
this.traveledPct += this.TraveledPctStepPerTick;
if (this.traveledPct >= 1f)
{
this.traveledPct = 1f;
this.Arrived();
}
}
private void Arrived()
{
if (this.arrived) return;
this.arrived = true;
Map map = Current.Game.FindMap(this.destinationTile);
if (map != null)
{
// Spawn the projectile at the edge of the map
IntVec3 entryCell = new IntVec3(CellRect.WholeMap(map).Width / 2, 0, CellRect.WholeMap(map).maxZ);
Projectile projectile = (Projectile)GenSpawn.Spawn(this.Projectile, entryCell, map, WipeMode.Vanish);
// Find a random cell near the target destination within the spread radius
IntVec3 finalDestination;
CellFinder.TryFindRandomCellNear(this.destinationCell, map, this.spread, null, out finalDestination, -1);
// Launch the projectile to the final destination
projectile.Launch(this.railgun, finalDestination, finalDestination, ProjectileHitFlags.IntendedTarget, false, null);
}
// Remove self from the world
Find.WorldObjects.Remove(this);
}
private const float TravelSpeed = 0.0001f;
private bool arrived;
public IntVec3 destinationCell = IntVec3.Invalid;
public int destinationTile = -1;
private int initialTile = -1;
public ThingDef Projectile;
public Thing railgun;
public int spread = 1;
private float traveledPct;
}
}
```
#### 关键方法和属性详解
* **`Start` / `End` (get)**: 这两个属性负责计算飞行的起点和终点坐标。它们通过检查 `HarmonyPatches.SoS` 来判断是否加载了 "Save Our Ship 2" Mod如果加载了则会尝试获取轨道上飞船的位置作为起点/终点从而实现与该Mod的无缝兼容。
* **`DrawPos` (get)**: 重写属性,用于在世界地图上渲染该对象。它使用 `Vector3.Slerp`(球面线性插值)在起点和终点之间进行插值,根据 `traveledPct`(已飞行百分比)计算出当前帧应该在的位置,从而形成一条平滑的弧形飞行轨迹。
* **`Tick()`**: 游戏引擎为每个世界对象调用的更新方法。它在每一帧增加 `traveledPct` 来推进飞行进度。当 `traveledPct` 达到1时调用 `Arrived()`
* **`Arrived()`**: 这是飞行结束、触发命中的核心方法。它首先检查目标地块是否存在一个已加载的地图。如果存在,它会在该地图的边缘生成 `Projectile` 实体,并根据 `spread`(散布)在目标点附近随机一个最终落点,然后调用炮弹的 `Launch()` 方法完成最后的攻击。无论地图是否存在,它最终都会将自己从世界上移除。
* **`ExposeData()`**: 保证了该飞行物体的所有状态(起点、终点、进度、炮弹类型等)都能被正确地保存和加载。
## 4. 系统工作流程
现在我们将三个核心组件联系起来,详细描述从玩家点击按钮到炮弹命中的完整工作流程。
### 4.1 交互时序图
下面的 Mermaid 时序图直观地展示了不同对象之间的交互顺序。
```mermaid
sequenceDiagram
participant Player as 玩家
participant Railgun as Building_Railgun
participant Verb as Verb_Railgun
participant Sabot as WorldObject_Sabot
participant Projectile as 最终炮弹
Player->>Railgun: 点击 "Fire Mission" 按钮
activate Railgun
Railgun->>Player: 显示世界地图和射程圈
Player->>Railgun: 1. 选择世界目标 (地块)
Player->>Railgun: 2. 选择地图内目标 (坐标)
Railgun->>Railgun: 调用 FireMission() 设置 longTarget
Railgun->>Verb: 调用 TryStartShootSomething()
deactivate Railgun
activate Verb
Verb->>Verb: TryCastShot() 检测到 longTarget
Verb->>Verb: 调用 TryCastFireMission()
Verb->>Sabot: new WorldObject_Sabot()
activate Sabot
Verb->>Sabot: 传递目标信息、炮弹类型等
Verb-->>Player: 触发炮口闪光和声音
deactivate Verb
loop 飞行过程 (每Tick)
Sabot->>Sabot: 更新 traveledPct (飞行进度)
end
Sabot->>Sabot: Arrived() - 到达目标
Sabot->>Projectile: GenSpawn.Spawn(炮弹)
activate Projectile
Sabot->>Projectile: Launch() - 飞向最终落点
Projectile-->>Player: 爆炸和伤害效果
deactivate Projectile
Sabot->>Sabot: Find.WorldObjects.Remove(this)
deactivate Sabot
```
### 4.2 步骤分解
1. **启动与目标选择 (玩家 -> `Building_Railgun`)**
* 玩家选中一门或多门 `Building_Railgun` 并点击其 "Fire Mission" Gizmo。
* `GetGizmos` 触发 `StartChoosingDestination` 方法,视角切换至世界地图,并显示最大射程圈。
* 玩家在世界地图上选择一个目标地块。
* `ChoseWorldTarget` 方法被回调。在通过一系列验证后,视角切换到目标地图。
* 玩家在目标地图上选择一个精确的弹着点。
2. **下达指令 (`Building_Railgun` -> `Verb_Railgun`)**
* 当精确弹着点被选定后,`ChoseWorldTarget` 方法会为所有被选中的炮塔调用 `FireMission` 方法。
* `FireMission` 将包含地块和坐标的 `GlobalTargetInfo` 存入炮塔的 `longTarget` 变量中。
* `FireMission` 随即调用 `TryStartShootSomething()`这会启动炮塔的射击冷却计时器并最终触发其Verb组件。
3. **发射与创建飞行物 (`Verb_Railgun` -> `WorldObject_Sabot`)**
* 炮塔的 `Verb_Railgun` 组件的 `TryCastShot` 方法被调用。
* `TryCastShot` 检测到 `longTarget` 是有效的,因此它不会执行常规射击,而是调用 `TryCastFireMission`
* `TryCastFireMission` 创建一个 `WorldObject_Sabot` 的实例,并将目标信息、炮弹定义、散布等关键数据从 `Building_Railgun` 复制到这个新实例中。
* `WorldObject_Sabot` 被添加到 `Find.WorldObjects` 管理器中,正式开始其生命周期。
* 同时,`Verb_Railgun` 在本地触发开火的视觉和听觉效果。
4. **跨地图飞行 (`WorldObject_Sabot`)**
* `WorldObject_Sabot` 作为一个独立的世界对象,其 `Tick` 方法被游戏引擎在每一帧调用。
* `Tick` 方法不断更新 `traveledPct` 属性,模拟飞行进度。
*`DrawPos` 属性根据 `traveledPct` 在世界地图上平滑地渲染出飞行轨迹。
5. **抵达与命中 (`WorldObject_Sabot` -> `Projectile`)**
*`traveledPct` 达到100%时,`Arrived` 方法被调用。
* `Arrived` 检查目标地块的地图是否已加载。
* 如果地图已加载,它会在地图边缘 `GenSpawn.Spawn` 一个真正的 `Projectile`(最终炮弹)。
* 根据从 `Building_Railgun` 继承来的 `spread` 值,在玩家指定的精确落点附近随机一个最终弹着点。
* 调用 `Projectile.Launch()`,使其从地图边缘飞向并命中最终弹着点,产生爆炸和伤害。
* 最后,`WorldObject_Sabot` 调用 `Find.WorldObjects.Remove(this)` 将自己从世界上移除,完成其使命。
## 5. 总结
Rimatomics的电磁炮系统是一个设计精良的远程打击模块。它通过将**交互(Building)**、**动作(Verb)**和**飞行(WorldObject)**三个阶段清晰地分离到不同的类中,实现了高度的内聚和低耦合。这种设计不仅使得代码逻辑清晰、易于维护,还通过 `WorldObject` 机制优雅地解决了跨地图状态同步和持久化的问题并为兼容其他Mod如Save Our Ship 2留出了接口。

View File

@@ -208,6 +208,11 @@
<Compile Include="Building_ARANutrientDispenser.cs" />
<Compile Include="ARAFoodDispenserProperties.cs" />
<Compile Include="Patch_DispenserFoodSearch.cs" />
<Compile Include="Buildings\Building_CatastropheMissileSilo.cs" />
<Compile Include="Verbs\Verb_LaunchCatastropheMissile.cs" />
<Compile Include="World\WorldObject_CatastropheMissile.cs" />
<Compile Include="HarmonyPatches\Patch_ForceTargetable.cs" />
<Compile Include="Comps\CompForceTargetable.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Wormhole\Building_WormholePortal_A.cs" />

View File

@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
using Verse.Sound;
namespace ArachnaeSwarm
{
[StaticConstructorOnStartup]
public class Building_CatastropheMissileSilo : Building_TurretGun
{
public GlobalTargetInfo longTarget;
public static readonly Texture2D FireMissionTex = ContentFinder<Texture2D>.Get("UI/Commands/Attack", true);
public override void ExposeData()
{
base.ExposeData();
Scribe_TargetInfo.Look(ref this.longTarget, "longTarget");
}
public override IEnumerable<Gizmo> GetGizmos()
{
foreach (Gizmo c in base.GetGizmos())
{
yield return c;
}
// Gizmo to set the long range target
Command_Action setTarget = new Command_Action
{
defaultLabel = "CommandSetGlobalTarget".Translate(),
defaultDesc = "CommandSetGlobalTargetDesc".Translate(),
icon = FireMissionTex,
action = new Action(this.StartChoosingDestination)
};
if (!this.powerComp.PowerOn)
{
setTarget.Disable("NoPower".Translate().CapitalizeFirst());
}
yield return setTarget;
// Gizmo to clear the long range target
if (this.longTarget.IsValid)
{
Command_Action clearTarget = new Command_Action
{
defaultLabel = "CommandClearGlobalTarget".Translate(),
defaultDesc = "CommandClearGlobalTargetDesc".Translate(),
icon = ContentFinder<Texture2D>.Get("UI/Commands/Cancel"),
action = () => { this.longTarget = GlobalTargetInfo.Invalid; }
};
yield return clearTarget;
}
}
private void StartChoosingDestination()
{
CameraJumper.TryJump(CameraJumper.GetWorldTarget(this), CameraJumper.MovementMode.Pan);
Find.WorldSelector.ClearSelection();
Find.WorldTargeter.BeginTargeting(
new Func<GlobalTargetInfo, bool>(this.ChoseWorldTarget),
true,
FireMissionTex,
true,
() => GenDraw.DrawWorldRadiusRing(this.Map.Tile, 99999),
null, null, null, true);
}
private bool ChoseWorldTarget(GlobalTargetInfo target)
{
if (!target.IsValid)
{
Messages.Message("MessageTargetInvalid".Translate(), MessageTypeDefOf.RejectInput, true);
return false;
}
if (target.Map == this.Map)
{
Messages.Message("Cannot target own map for global strike.", MessageTypeDefOf.RejectInput, true);
return false;
}
// The target must be a map parent that has a loaded map.
if (target.WorldObject is MapParent mapParent && mapParent.HasMap)
{
var originalMap = this.Map;
Action onFinished = () => {
if (Current.Game.CurrentMap != originalMap) Current.Game.CurrentMap = originalMap;
};
Current.Game.CurrentMap = mapParent.Map;
Find.Targeter.BeginTargeting(new TargetingParameters { canTargetLocations = true },
(LocalTargetInfo localTarget) => // This is called when the user clicks a cell in the target map
{
this.FireMission(new GlobalTargetInfo(localTarget.Cell, mapParent.Map));
},
null, onFinished, FireMissionTex, true);
return true;
}
else
{
Messages.Message("MessageTargetMustBeMap".Translate(), MessageTypeDefOf.RejectInput, true);
return false;
}
}
public void FireMission(GlobalTargetInfo target)
{
this.longTarget = target;
this.OrderAttack(new LocalTargetInfo(this));
Messages.Message("Global target acquired. Firing sequence initiated.", MessageTypeDefOf.PositiveEvent);
}
}
}

View File

@@ -0,0 +1,18 @@
using Verse;
namespace ArachnaeSwarm
{
public class CompProperties_ForceTargetable : CompProperties
{
public CompProperties_ForceTargetable()
{
this.compClass = typeof(CompForceTargetable);
}
}
public class CompForceTargetable : ThingComp
{
// This component doesn't need any specific logic.
// Its mere presence on a turret is checked by the Harmony patch.
}
}

View File

@@ -0,0 +1,23 @@
using HarmonyLib;
using Verse;
using RimWorld;
namespace ArachnaeSwarm
{
[HarmonyPatch(typeof(Building_TurretGun), "get_CanSetForcedTarget")]
public static class Patch_Building_TurretGun_CanSetForcedTarget
{
public static void Postfix(Building_TurretGun __instance, ref bool __result)
{
if (__result)
{
return;
}
if (__instance.GetComp<CompForceTargetable>() != null && __instance.Faction == Faction.OfPlayer)
{
__result = true;
}
}
}
}

View File

@@ -0,0 +1,78 @@
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
using Verse.Sound;
namespace ArachnaeSwarm
{
public class Verb_LaunchCatastropheMissile : Verb_Shoot
{
public override bool CanHitTargetFrom(IntVec3 root, LocalTargetInfo targ)
{
var silo = this.Caster as Building_CatastropheMissileSilo;
if (silo != null && silo.longTarget.IsValid)
{
return true;
}
return base.CanHitTargetFrom(root, targ);
}
protected override bool TryCastShot()
{
var silo = this.Caster as Building_CatastropheMissileSilo;
if (silo != null && silo.longTarget.IsValid)
{
return this.TryCastGlobalShot(silo);
}
// If no long target, perform a normal local shot
return base.TryCastShot();
}
private bool TryCastGlobalShot(Building_CatastropheMissileSilo silo)
{
var refuelableComp = silo.TryGetComp<CompRefuelable>();
if (refuelableComp != null && !refuelableComp.HasFuel)
{
Messages.Message("NoMissileToLaunch".Translate(), silo, MessageTypeDefOf.RejectInput);
return false;
}
WorldObject_CatastropheMissile missile = (WorldObject_CatastropheMissile)WorldObjectMaker.MakeWorldObject(
DefDatabase<WorldObjectDef>.GetNamed("CatastropheMissile_Flying")
);
missile.Tile = silo.Map.Tile;
missile.destinationTile = silo.longTarget.Tile;
missile.destinationCell = silo.longTarget.Cell;
missile.Projectile = DefDatabase<ThingDef>.GetNamed("Projectile_CatastropheMissile");
Find.WorldObjects.Add(missile);
if(refuelableComp != null)
{
refuelableComp.ConsumeFuel(1);
}
SoundDef.Named("RocketLaunch").PlayOneShot(new TargetInfo(silo.Position, silo.Map));
// Reset target after launch
silo.longTarget = GlobalTargetInfo.Invalid;
// Manually reset cooldown
if (this.burstShotsLeft < this.verbProps.burstShotCount)
{
this.burstShotsLeft = 0;
}
if (this.verbProps.burstShotCount > 0)
{
this.ticksToNextBurstShot = this.verbProps.ticksBetweenBurstShots;
}
this.state = VerbState.Idle;
return true;
}
}
}

View File

@@ -0,0 +1,73 @@
using RimWorld.Planet;
using UnityEngine;
using Verse;
using RimWorld;
namespace ArachnaeSwarm
{
public class WorldObject_CatastropheMissile : WorldObject
{
public int destinationTile = -1;
public IntVec3 destinationCell = IntVec3.Invalid;
public ThingDef Projectile;
private int initialTile = -1;
private float traveledPct;
private const float TravelSpeed = 0.0002f; // Faster than sabot
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref destinationTile, "destinationTile", 0);
Scribe_Values.Look(ref destinationCell, "destinationCell");
Scribe_Defs.Look(ref Projectile, "Projectile");
Scribe_Values.Look(ref initialTile, "initialTile", 0);
Scribe_Values.Look(ref traveledPct, "traveledPct", 0f);
}
public override void PostAdd()
{
base.PostAdd();
this.initialTile = this.Tile;
}
private Vector3 StartPos => Find.WorldGrid.GetTileCenter(this.initialTile);
private Vector3 EndPos => Find.WorldGrid.GetTileCenter(this.destinationTile);
public override Vector3 DrawPos => Vector3.Slerp(StartPos, EndPos, traveledPct);
protected override void Tick()
{
base.Tick();
float distance = GenMath.SphericalDistance(StartPos.normalized, EndPos.normalized);
if(distance > 0)
{
traveledPct += TravelSpeed / distance;
}
else
{
traveledPct = 1;
}
if (traveledPct >= 1f)
{
Arrived();
}
}
private void Arrived()
{
Map targetMap = Current.Game.FindMap(this.destinationTile);
if (targetMap != null)
{
// Target is a loaded map, spawn the projectile to hit it
IntVec3 entryCell = CellFinder.RandomEdgeCell(targetMap);
Projectile_CruiseMissile missile = (Projectile_CruiseMissile)GenSpawn.Spawn(this.Projectile, entryCell, targetMap, WipeMode.Vanish);
missile.Launch(null, this.destinationCell, this.destinationCell, ProjectileHitFlags.IntendedTarget);
}
Find.WorldObjects.Remove(this);
}
}
}