147 lines
5.3 KiB
C#
147 lines
5.3 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using ThreatSource.Simulation;
|
||
using ThreatSource.Data;
|
||
using ThreatSource.Missile;
|
||
|
||
namespace ThreatSource.Examples
|
||
{
|
||
/// <summary>
|
||
/// 红外成像制导导弹仿真示例
|
||
/// 本示例展示了如何:
|
||
/// 1. 创建和配置一个红外成像制导导弹
|
||
/// 2. 设置仿真环境和参数
|
||
/// 3. 运行仿真并获取结果
|
||
/// </summary>
|
||
public class IRMissileSimulationExample
|
||
{
|
||
private ISimulationManager _simulationManager;
|
||
private ThreatSourceDataManager _dataManager;
|
||
|
||
public IRMissileSimulationExample()
|
||
{
|
||
_simulationManager = new SimulationManager();
|
||
_dataManager = new ThreatSourceDataManager();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 运行红外成像制导导弹仿真示例
|
||
/// </summary>
|
||
public async Task RunSimulationExample()
|
||
{
|
||
try
|
||
{
|
||
// 步骤1:初始化仿真管理器
|
||
_simulationManager.StartSimulation(0.02); // 20ms时间步长
|
||
|
||
// 步骤2:创建目标实体
|
||
var target = new BaseEquipment("target_001")
|
||
{
|
||
KState = new KinematicState
|
||
{
|
||
Position = new Vector3(5000, 0, 1000), // 5km距离,1km高度
|
||
Velocity = new Vector3(-50, 0, 0), // 50m/s向西移动
|
||
Orientation = new Orientation(0, 0, 0)
|
||
}
|
||
};
|
||
|
||
// 步骤3:从配置创建红外成像制导导弹
|
||
var missileData = _dataManager.GetMissile("IR_Missile_Example");
|
||
var missile = new InfraredImagingTerminalGuidanceMissile("missile_001", missileData)
|
||
{
|
||
KState = new KinematicState
|
||
{
|
||
Position = new Vector3(0, 0, 100), // 起始位置
|
||
Velocity = new Vector3(200, 0, 0), // 初始速度200m/s
|
||
Orientation = new Orientation(0, 0, 0) // 初始朝向
|
||
}
|
||
};
|
||
|
||
// 步骤4:注册实体到仿真管理器
|
||
_simulationManager.RegisterEntity(target);
|
||
_simulationManager.RegisterEntity(missile);
|
||
|
||
// 步骤5:订阅仿真事件
|
||
_simulationManager.Subscribe<MissileExplodeEvent>(OnMissileExplode);
|
||
_simulationManager.Subscribe<FlightPhaseChangeEvent>(OnFlightPhaseChange);
|
||
|
||
// 步骤6:运行仿真循环
|
||
double simulationTime = 0;
|
||
double maxTime = 60.0; // 最大仿真时间60秒
|
||
|
||
Console.WriteLine("开始红外成像制导导弹仿真...");
|
||
Console.WriteLine($"目标位置: {target.KState.Position}");
|
||
Console.WriteLine($"导弹初始位置: {missile.KState.Position}");
|
||
|
||
while (simulationTime < maxTime && missile.IsActive)
|
||
{
|
||
// 更新仿真
|
||
_simulationManager.UpdateSimulation();
|
||
simulationTime = _simulationManager.CurrentTime;
|
||
|
||
// 每秒输出一次状态
|
||
if (simulationTime % 1.0 < 0.02)
|
||
{
|
||
var distance = Vector3.Distance(missile.KState.Position, target.KState.Position);
|
||
Console.WriteLine($"时间: {simulationTime:F1}s, 距离目标: {distance:F1}m, 导弹阶段: {missile.FlightPhase}");
|
||
}
|
||
|
||
await Task.Delay(1); // 避免阻塞
|
||
}
|
||
|
||
// 步骤7:输出仿真结果
|
||
var finalDistance = Vector3.Distance(missile.KState.Position, target.KState.Position);
|
||
Console.WriteLine($"仿真结束 - 最终距离: {finalDistance:F2}m");
|
||
|
||
if (finalDistance < 10.0)
|
||
{
|
||
Console.WriteLine("导弹成功命中目标!");
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("导弹未能命中目标。");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"仿真过程出错: {ex.Message}");
|
||
}
|
||
finally
|
||
{
|
||
// 清理资源
|
||
_simulationManager.StopSimulation();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理导弹爆炸事件
|
||
/// </summary>
|
||
private void OnMissileExplode(MissileExplodeEvent evt)
|
||
{
|
||
Console.WriteLine($"导弹 {evt.SenderId} 在位置 {evt.Position} 爆炸");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理飞行阶段变化事件
|
||
/// </summary>
|
||
private void OnFlightPhaseChange(FlightPhaseChangeEvent evt)
|
||
{
|
||
Console.WriteLine($"导弹 {evt.SenderId} 进入 {evt.NewPhase} 阶段");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 示例程序入口点
|
||
/// </summary>
|
||
public class Program
|
||
{
|
||
public static async Task Main(string[] args)
|
||
{
|
||
var example = new IRMissileSimulationExample();
|
||
await example.RunSimulationExample();
|
||
|
||
Console.WriteLine("按任意键退出...");
|
||
Console.ReadKey();
|
||
}
|
||
}
|
||
} |