260 lines
8.9 KiB
C#
260 lines
8.9 KiB
C#
using ActiveProtect.SimulationEnvironment;
|
|
using System;
|
|
|
|
namespace ActiveProtect.Models
|
|
{
|
|
/// <summary>
|
|
/// 激光指示器类,用于对目标进行激光照射
|
|
/// </summary>
|
|
public class LaserDesignator : SimulationElement
|
|
{
|
|
/// <summary>
|
|
/// 目标ID
|
|
/// </summary>
|
|
public string TargetId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 导弹ID
|
|
/// </summary>
|
|
public string MissileId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 干扰阈值(分贝)
|
|
/// </summary>
|
|
public double JammingThreshold { get; private set; } = 0.0;
|
|
|
|
/// <summary>
|
|
/// 是否被干扰
|
|
/// </summary>
|
|
public bool IsJammed { get; private set; } = false;
|
|
|
|
/// <summary>
|
|
/// 最大照射范围(米)
|
|
/// </summary>
|
|
public double MaxIlluminationRange { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 是否正在照射
|
|
/// </summary>
|
|
private bool IsIlluminating { get; set; } = false;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="id">激光指示器ID</param>
|
|
/// <param name="targetId">目标ID</param>
|
|
/// <param name="missileId">导弹ID</param>
|
|
/// <param name="config">激光指示器配置</param>
|
|
/// <param name="simulationManager">仿真管理器</param>
|
|
public LaserDesignator(string id, string targetId, string missileId, LaserDesignatorConfig config, ISimulationManager simulationManager)
|
|
: base(id, config.InitialPosition, new Orientation(), simulationManager)
|
|
{
|
|
TargetId = targetId;
|
|
MissileId = missileId;
|
|
IsActive = true;
|
|
MaxIlluminationRange = config.MaxIlluminationRange;
|
|
SimulationManager.SubscribeToEvent<LaserJammingEvent>(OnLaserJamming);
|
|
SimulationManager.SubscribeToEvent<EntityDeactivatedEvent>(OnEntityDeactivatedEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新激光指示器状态
|
|
/// </summary>
|
|
/// <param name="deltaTime">时间步长</param>
|
|
public override void Update(double deltaTime)
|
|
{
|
|
if (IsActive && !IsJammed)
|
|
{
|
|
UpdateIllumination();
|
|
}
|
|
else if (IsIlluminating)
|
|
{
|
|
StopIllumination();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新照射状态
|
|
/// </summary>
|
|
private void UpdateIllumination()
|
|
{
|
|
if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target)
|
|
{
|
|
double distanceToTarget = Vector3D.Distance(Position, target.Position);
|
|
bool shouldIlluminate = distanceToTarget <= MaxIlluminationRange;
|
|
|
|
Console.WriteLine($"激光目标指示器 {Id} 状态更新:");
|
|
Console.WriteLine($" 目标: {TargetId}");
|
|
Console.WriteLine($" 距离目标: {distanceToTarget:F2} m (最大照射范围: {MaxIlluminationRange} m)");
|
|
Console.WriteLine($" 是否应该照射: {shouldIlluminate}");
|
|
Console.WriteLine($" 当前照射状态: {IsIlluminating}");
|
|
|
|
if (shouldIlluminate && !IsIlluminating)
|
|
{
|
|
StartIllumination(target);
|
|
}
|
|
else if (!shouldIlluminate && IsIlluminating)
|
|
{
|
|
StopIllumination();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"激光目标指示器 {Id} 无法找到目标 {TargetId}");
|
|
if (IsIlluminating)
|
|
{
|
|
StopIllumination();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始照射目标
|
|
/// </summary>
|
|
/// <param name="target">目标</param>
|
|
private void StartIllumination(ILaserIlluminatable target)
|
|
{
|
|
target.Illuminate();
|
|
IsIlluminating = true;
|
|
Console.WriteLine($"激光目标指示器 {Id} 开始照射目标 {TargetId}");
|
|
PublishIlluminationEvent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止照射目标
|
|
/// </summary>
|
|
private void StopIllumination()
|
|
{
|
|
if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target)
|
|
{
|
|
target.StopIllumination();
|
|
}
|
|
IsIlluminating = false;
|
|
Console.WriteLine($"激光目标指示器 {Id} 停止照射目标 {TargetId}");
|
|
PublishIlluminationStopEvent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理激光干扰事件
|
|
/// </summary>
|
|
/// <param name="evt">激光干扰事件</param>
|
|
private void OnLaserJamming(LaserJammingEvent evt)
|
|
{
|
|
if (evt.TargetId == TargetId)
|
|
{
|
|
var tank = SimulationManager.GetEntityById(TargetId) as Tank;
|
|
if (tank != null)
|
|
{
|
|
double distanceToTarget = Vector3D.Distance(Position, tank.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;
|
|
StopIllumination();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (IsJammed)
|
|
{
|
|
Console.WriteLine($"激光目标指示器 {Id} 干扰解除,恢复工作");
|
|
IsJammed = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 激活激光指示器
|
|
/// </summary>
|
|
public override void Activate()
|
|
{
|
|
if (!IsActive)
|
|
{
|
|
IsActive = true;
|
|
IsJammed = false;
|
|
Console.WriteLine($"激光目标指示器 {Id} 已激活");
|
|
UpdateIllumination();
|
|
}
|
|
base.Activate();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停用激光指示器
|
|
/// </summary>
|
|
public override void Deactivate()
|
|
{
|
|
if (IsActive)
|
|
{
|
|
IsActive = false;
|
|
StopIllumination();
|
|
Console.WriteLine($"激光目标指示器 {Id} 已停用");
|
|
}
|
|
base.Deactivate();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发布激光照射事件
|
|
/// </summary>
|
|
private void PublishIlluminationEvent()
|
|
{
|
|
var evt = new LaserIlluminationEvent
|
|
{
|
|
TargetId = TargetId,
|
|
SenderId = Id
|
|
};
|
|
PublishEvent(evt);
|
|
Console.WriteLine($"激光指示器 {Id} 发布激光照射事件, 目标ID: {TargetId}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发布激光照射停止事件
|
|
/// </summary>
|
|
private void PublishIlluminationStopEvent()
|
|
{
|
|
var evt = new LaserIlluminationStopEvent
|
|
{
|
|
TargetId = TargetId,
|
|
SenderId = Id
|
|
};
|
|
PublishEvent(evt);
|
|
Console.WriteLine($"激光指示器 {Id} 发布激光照射停止事件, 目标ID: {TargetId}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理实体停用事件
|
|
/// </summary>
|
|
/// <param name="evt">实体停用事件</param>
|
|
private void OnEntityDeactivatedEvent(EntityDeactivatedEvent evt)
|
|
{
|
|
if (evt.DeactivatedEntityId == TargetId || evt.DeactivatedEntityId == MissileId)
|
|
{
|
|
Deactivate();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取激光指示器状态信息
|
|
/// </summary>
|
|
/// <returns>状态信息字符串</returns>
|
|
public override string GetStatus()
|
|
{
|
|
return $"激光目标指示器 {Id}:\n" +
|
|
$" 位置: {Position}\n" +
|
|
$" 目标: {TargetId}\n" +
|
|
$" 导弹: {MissileId}\n" +
|
|
$" 激活状态: {(IsActive ? "激活" : "未激活")}\n" +
|
|
$" 照射状态: {(IsIlluminating ? "正在照射" : "未照射")}\n" +
|
|
$" 干扰状态: {(IsJammed ? "被干扰" : "正常")}\n" +
|
|
$" 最大照射范围: {MaxIlluminationRange}";
|
|
}
|
|
}
|
|
}
|