69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using ActiveProtect.SimulationEnvironment;
|
|
using System;
|
|
|
|
namespace ActiveProtect.Models
|
|
{
|
|
public class LaserWarner
|
|
{
|
|
private readonly Tank tank;
|
|
public bool IsAlarming { get; private set; }
|
|
private double alarmDuration = 5.0; // 警报持续5秒
|
|
private double alarmTimer = 0;
|
|
|
|
public LaserWarner(Tank tank)
|
|
{
|
|
this.tank = tank;
|
|
IsAlarming = false;
|
|
tank.SimulationManager.SubscribeToEvent<LaserIlluminationEvent>(OnLaserIllumination);
|
|
tank.SimulationManager.SubscribeToEvent<LaserIlluminationStopEvent>(OnLaserIlluminationStop);
|
|
}
|
|
|
|
public void Update(double deltaTime)
|
|
{
|
|
if (IsAlarming)
|
|
{
|
|
alarmTimer += deltaTime;
|
|
if (alarmTimer >= alarmDuration)
|
|
{
|
|
StopAlarm();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnLaserIllumination(LaserIlluminationEvent evt)
|
|
{
|
|
if (evt.TargetId == tank.Id)
|
|
{
|
|
TriggerAlarm();
|
|
}
|
|
}
|
|
|
|
private void OnLaserIlluminationStop(LaserIlluminationStopEvent evt)
|
|
{
|
|
if (evt.TargetId == tank.Id)
|
|
{
|
|
StopAlarm();
|
|
}
|
|
}
|
|
|
|
public void TriggerAlarm()
|
|
{
|
|
if (!IsAlarming)
|
|
{
|
|
IsAlarming = true;
|
|
alarmTimer = 0;
|
|
Console.WriteLine($"坦克 {tank.Id} 的激光告警器发出警报!");
|
|
}
|
|
}
|
|
|
|
public void StopAlarm()
|
|
{
|
|
if (IsAlarming)
|
|
{
|
|
IsAlarming = false;
|
|
alarmTimer = 0;
|
|
Console.WriteLine($"坦克 {tank.Id} 的激光告警器停止警报");
|
|
}
|
|
}
|
|
}
|
|
} |