197 lines
6.5 KiB
C#
197 lines
6.5 KiB
C#
using System;
|
|
using ActiveProtect.SimulationEnvironment;
|
|
|
|
namespace ActiveProtect.Models
|
|
{
|
|
/// <summary>
|
|
/// 激光干扰器类,用于对抗激光制导武器
|
|
/// </summary>
|
|
public class LaserJammer : SimulationElement
|
|
{
|
|
/// <summary>
|
|
/// 是否正在进行干扰
|
|
/// </summary>
|
|
public bool IsJamming { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 当前干扰功率(瓦特)
|
|
/// </summary>
|
|
public double JammingPower { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 当前冷却时间(秒)
|
|
/// </summary>
|
|
private double jammingCooldown = 0;
|
|
|
|
/// <summary>
|
|
/// 最大冷却时间(秒)
|
|
/// </summary>
|
|
private readonly double maxJammingCooldown;
|
|
|
|
/// <summary>
|
|
/// 最大干扰功率(瓦特)
|
|
/// </summary>
|
|
private readonly double maxJammingPower;
|
|
|
|
/// <summary>
|
|
/// 初始干扰功率(瓦特)
|
|
/// </summary>
|
|
private readonly double initialJammingPower;
|
|
|
|
/// <summary>
|
|
/// 功率增加速率(瓦特/秒)
|
|
/// </summary>
|
|
private readonly double powerIncreaseRate;
|
|
|
|
/// <summary>
|
|
/// 被监视实体的ID
|
|
/// </summary>
|
|
public string MonitoredEntityId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="id">激光干扰器ID</param>
|
|
/// <param name="position">初始位置</param>
|
|
/// <param name="orientation">初始朝向</param>
|
|
/// <param name="simulationManager">仿真管理器</param>
|
|
/// <param name="monitoredEntityId">被监视实体的ID</param>
|
|
/// <param name="config">激光干扰器配置</param>
|
|
public LaserJammer(string id, Vector3D position, Orientation orientation, ISimulationManager simulationManager, string monitoredEntityId, LaserJammerConfig config)
|
|
: base(id, position, orientation, 0, simulationManager)
|
|
{
|
|
MonitoredEntityId = monitoredEntityId;
|
|
IsJamming = false;
|
|
JammingPower = 0;
|
|
maxJammingCooldown = config.MaxJammingCooldown;
|
|
maxJammingPower = config.MaxJammingPower;
|
|
initialJammingPower = config.InitialJammingPower;
|
|
powerIncreaseRate = config.PowerIncreaseRate;
|
|
base.SimulationManager.SubscribeToEvent<LaserWarnerAlarmEvent>(OnLaserWarnerAlarmEvent);
|
|
base.SimulationManager.SubscribeToEvent<LaserWarnerAlarmStopEvent>(OnLaserWarnerAlarmStopEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新激光干扰器状态
|
|
/// </summary>
|
|
/// <param name="deltaTime">时间步长</param>
|
|
public override void Update(double deltaTime)
|
|
{
|
|
if (!IsActive) return;
|
|
|
|
if (SimulationManager.GetEntityById(MonitoredEntityId) is Tank tank)
|
|
{
|
|
Position = tank.Position;
|
|
Orientation = tank.Orientation;
|
|
}
|
|
|
|
if (IsJamming)
|
|
{
|
|
JammingPower = Math.Min(JammingPower + powerIncreaseRate * deltaTime, maxJammingPower);
|
|
|
|
jammingCooldown += deltaTime;
|
|
if (jammingCooldown >= maxJammingCooldown)
|
|
{
|
|
StopJamming();
|
|
}
|
|
else
|
|
{
|
|
// 发布干扰事件
|
|
PublishEvent(new LaserJammingEvent
|
|
{
|
|
TargetId = MonitoredEntityId,
|
|
JammingPower = JammingPower
|
|
});
|
|
}
|
|
|
|
Console.WriteLine($"激光干扰器 {Id} 正在工作,当前功率: {JammingPower:F2}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理激光告警器警报事件
|
|
/// </summary>
|
|
/// <param name="evt">激光告警器警报事件</param>
|
|
private void OnLaserWarnerAlarmEvent(LaserWarnerAlarmEvent evt)
|
|
{
|
|
if (evt.TargetId == MonitoredEntityId)
|
|
{
|
|
StartJamming();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理激光告警器警报停止事件
|
|
/// </summary>
|
|
/// <param name="evt">激光告警器警报停止事件</param>
|
|
private void OnLaserWarnerAlarmStopEvent(LaserWarnerAlarmStopEvent evt)
|
|
{
|
|
if (evt.TargetId == MonitoredEntityId)
|
|
{
|
|
StopJamming();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始干扰
|
|
/// </summary>
|
|
private void StartJamming()
|
|
{
|
|
if (!IsJamming)
|
|
{
|
|
IsJamming = true;
|
|
JammingPower = initialJammingPower;
|
|
jammingCooldown = 0;
|
|
Console.WriteLine($"激光干扰器 {Id} 开始工作,初始功率: {JammingPower:F2}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止干扰
|
|
/// </summary>
|
|
private void StopJamming()
|
|
{
|
|
if (IsJamming)
|
|
{
|
|
IsJamming = false;
|
|
JammingPower = 0;
|
|
jammingCooldown = 0;
|
|
Console.WriteLine($"激光干扰器 {Id} 停止工作");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取激光干扰器状态信息
|
|
/// </summary>
|
|
/// <returns>状态信息字符串</returns>
|
|
public override string GetStatus()
|
|
{
|
|
return $"激光干扰器 {Id}:\n" +
|
|
$" 监视实体: {MonitoredEntityId}\n" +
|
|
$" 干扰状态: {(IsJamming ? "干扰中" : "未干扰")}\n" +
|
|
$" 当前功率: {JammingPower:F2}/{maxJammingPower:F2}\n" +
|
|
$" 冷却时间: {jammingCooldown:F2}/{maxJammingCooldown:F2}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 激活激光干扰器
|
|
/// </summary>
|
|
public override void Activate()
|
|
{
|
|
base.Activate();
|
|
SimulationManager.SubscribeToEvent<LaserWarnerAlarmEvent>(OnLaserWarnerAlarmEvent);
|
|
SimulationManager.SubscribeToEvent<LaserWarnerAlarmStopEvent>(OnLaserWarnerAlarmStopEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停用激光干扰器
|
|
/// </summary>
|
|
public override void Deactivate()
|
|
{
|
|
base.Deactivate();
|
|
SimulationManager.UnsubscribeFromEvent<LaserWarnerAlarmEvent>(OnLaserWarnerAlarmEvent);
|
|
SimulationManager.UnsubscribeFromEvent<LaserWarnerAlarmStopEvent>(OnLaserWarnerAlarmStopEvent);
|
|
}
|
|
}
|
|
}
|