ActiveProtect/Models/LaserDesignator.cs

140 lines
5.1 KiB
C#

using ActiveProtect.SimulationEnvironment;
using System;
namespace ActiveProtect.Models
{
public class LaserDesignator : SimulationElement
{
public string TargetId { get; private set; }
public bool IsActive { get; private set; }
public double JammingThreshold { get; private set; } = 0.0; // 将阈值调整为 0 dB
public bool IsJammed { get; private set; } = false;
public double MaxIlluminationRange { get; private set; }
public LaserDesignator(string id, LaserDesignatorConfig config, ISimulationManager simulationManager)
: base(id, config.InitialPosition, new Orientation(), simulationManager)
{
TargetId = config.TargetId;
IsActive = true;
MaxIlluminationRange = config.MaxIlluminationRange;
simulationManager.SubscribeToEvent<LaserJammingEvent>(OnLaserJamming);
}
public override void Update(double deltaTime)
{
if (IsActive && !IsJammed)
{
IlluminateTarget();
}
else
{
StopIllumination();
}
}
private void IlluminateTarget()
{
if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target)
{
double distanceToTarget = Vector3D.Distance(Position, target.Position);
if (distanceToTarget <= MaxIlluminationRange)
{
target.Illuminate();
Console.WriteLine($"激光目标指示器 {Id} 正在照射目标 {TargetId}");
// 发布LaserIlluminationEvent
PublishEvent(new LaserIlluminationEvent
{
TargetId = TargetId
});
}
else
{
StopIllumination();
Console.WriteLine($"激光目标指示器 {Id} 超出照射范围,停止照射目标 {TargetId}");
}
}
}
private void StopIllumination()
{
if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target)
{
target.StopIllumination();
Console.WriteLine($"激光目标指示器 {Id} 停止照射目标 {TargetId}");
PublishIlluminationStopEvent();
}
}
private void OnLaserJamming(LaserJammingEvent evt)
{
if (evt.TargetId == TargetId)
{
if (SimulationManager.GetEntityById(TargetId) is Tank target)
{
double distanceToTarget = Vector3D.Distance(Position, target.Position);
// 调整干扰效果计算方法
double jammingEffectiveness = 20 * Math.Log10(evt.JammingPower) - 20 * Math.Log10(distanceToTarget) - 20 * Math.Log10(4 * Math.PI);
Console.WriteLine($"激光目标指示器 {Id} 收到干扰事件,干扰功率: {evt.JammingPower:F2}, 距离: {distanceToTarget:F2}, 干扰效果: {jammingEffectiveness:F2} dB, 阈值: {JammingThreshold:F2} dB");
if (jammingEffectiveness > JammingThreshold)
{
if (!IsJammed)
{
Console.WriteLine($"激光目标指示器 {Id} 被干扰,停止工作");
IsJammed = true;
DeactivateLaser();
}
}
else
{
if (IsJammed)
{
Console.WriteLine($"激光目标指示器 {Id} 干扰解除,恢复工作");
IsJammed = false;
ActivateLaser();
}
}
}
}
}
public void ActivateLaser()
{
IsActive = true;
IsJammed = false;
Console.WriteLine($"激光目标指示器 {Id} 已激活");
IlluminateTarget(); // 立即开始照射目标
}
public void DeactivateLaser()
{
if (IsActive)
{
IsActive = false;
StopIllumination();
Console.WriteLine($"激光目标指示器 {Id} 已停用");
}
}
private void PublishIlluminationStopEvent()
{
PublishEvent(new LaserIlluminationStopEvent
{
TargetId = TargetId
});
}
public override string GetStatus()
{
return $"激光目标指示器 {Id}:\n" +
$" 位置: {Position}\n" +
$" 目标: {TargetId}\n" +
$" 激活状态: {(IsActive ? "" : "")}\n" +
$" 干扰状态: {(IsJammed ? "" : "")}\n" +
$" 最大照射范围: {MaxIlluminationRange}";
}
}
}