164 lines
5.9 KiB
C#
164 lines
5.9 KiB
C#
using ActiveProtect.SimulationEnvironment;
|
|
using System;
|
|
|
|
namespace ActiveProtect.Models
|
|
{
|
|
public class LaserDesignator : SimulationElement
|
|
{
|
|
public string TargetId { get; private set; }
|
|
public double JammingThreshold { get; private set; } = 0.0; // 调整干扰阈值
|
|
public bool IsJammed { get; private set; } = false;
|
|
public double MaxIlluminationRange { get; private set; }
|
|
private bool IsIlluminating { get; set; } = false;
|
|
|
|
public LaserDesignator(string id, string targetId, LaserDesignatorConfig config, ISimulationManager simulationManager)
|
|
: base(id, config.InitialPosition, new Orientation(), simulationManager)
|
|
{
|
|
TargetId = targetId;
|
|
IsActive = true;
|
|
MaxIlluminationRange = config.MaxIlluminationRange;
|
|
SimulationManager.SubscribeToEvent<LaserJammingEvent>(OnLaserJamming);
|
|
}
|
|
|
|
public override void Update(double deltaTime)
|
|
{
|
|
if (IsActive && !IsJammed)
|
|
{
|
|
UpdateIllumination();
|
|
}
|
|
else if (IsIlluminating)
|
|
{
|
|
StopIllumination();
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void StartIllumination(ILaserIlluminatable target)
|
|
{
|
|
target.Illuminate();
|
|
IsIlluminating = true;
|
|
Console.WriteLine($"激光目标指示器 {Id} 开始照射目标 {TargetId}");
|
|
PublishIlluminationEvent();
|
|
}
|
|
|
|
private void StopIllumination()
|
|
{
|
|
if (SimulationManager.GetEntityById(TargetId) is ILaserIlluminatable target)
|
|
{
|
|
target.StopIllumination();
|
|
}
|
|
IsIlluminating = false;
|
|
Console.WriteLine($"激光目标指示器 {Id} 停止照射目标 {TargetId}");
|
|
PublishIlluminationStopEvent();
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ActivateLaser()
|
|
{
|
|
IsActive = true;
|
|
IsJammed = false;
|
|
Console.WriteLine($"激光目标指示器 {Id} 已激活");
|
|
UpdateIllumination();
|
|
}
|
|
|
|
public void DeactivateLaser()
|
|
{
|
|
if (IsActive)
|
|
{
|
|
IsActive = false;
|
|
StopIllumination();
|
|
Console.WriteLine($"激光目标指示器 {Id} 已停用");
|
|
}
|
|
}
|
|
|
|
private void PublishIlluminationEvent()
|
|
{
|
|
PublishEvent(new LaserIlluminationEvent
|
|
{
|
|
TargetId = TargetId
|
|
});
|
|
}
|
|
|
|
private void PublishIlluminationStopEvent()
|
|
{
|
|
PublishEvent(new LaserIlluminationStopEvent
|
|
{
|
|
TargetId = TargetId
|
|
});
|
|
}
|
|
|
|
public override string GetStatus()
|
|
{
|
|
return $"激光目标指示器 {Id}:\n" +
|
|
$" 位置: {Position}\n" +
|
|
$" 目标: {TargetId}\n" +
|
|
$" 激活状态: {(IsActive ? "激活" : "未激活")}\n" +
|
|
$" 照射状态: {(IsIlluminating ? "正在照射" : "未照射")}\n" +
|
|
$" 干扰状态: {(IsJammed ? "被干扰" : "正常")}\n" +
|
|
$" 最大照射范围: {MaxIlluminationRange}";
|
|
}
|
|
}
|
|
} |