2025-02-04 13:22:05 +01:00
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Cirno.Scripts;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class AlarmManager : Node2D
|
|
|
|
|
|
{
|
2025-03-01 23:46:25 +01:00
|
|
|
|
[Export]
|
|
|
|
|
|
public AudioStream AlarmSound { get; private set; }
|
|
|
|
|
|
|
2025-02-21 18:57:00 +01:00
|
|
|
|
public static AlarmManager Instance { get; private set; }
|
|
|
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
|
public bool IsAlarmOn { get; private set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
public Vector2 LastAlarmPosition { get; private set; } = new Vector2();
|
|
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
|
public delegate void AlarmEnabledEventHandler(Vector2 location);
|
|
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
|
public delegate void AlarmDisabledEventHandler();
|
|
|
|
|
|
|
2025-03-01 23:46:25 +01:00
|
|
|
|
private AudioStreamPlayer2D _player;
|
|
|
|
|
|
|
2025-02-21 18:57:00 +01:00
|
|
|
|
public override void _Ready()
|
|
|
|
|
|
{
|
|
|
|
|
|
Instance = this;
|
2025-03-01 23:46:25 +01:00
|
|
|
|
|
|
|
|
|
|
if (AlarmSound is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var player = new AudioStreamPlayer2D();
|
|
|
|
|
|
player.Stream = AlarmSound;
|
|
|
|
|
|
this.CallDeferred("add_child", player);
|
|
|
|
|
|
|
|
|
|
|
|
_player = player;
|
|
|
|
|
|
}
|
2025-02-21 18:57:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
|
public void SoundAlarm(Vector2 location)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsAlarmOn) return;
|
|
|
|
|
|
IsAlarmOn = true;
|
|
|
|
|
|
LastAlarmPosition = location;
|
|
|
|
|
|
EmitSignal(nameof(AlarmEnabled), location);
|
|
|
|
|
|
|
|
|
|
|
|
GD.Print($"Alarm sounded at {location}");
|
2025-03-01 23:46:25 +01:00
|
|
|
|
_player?.Play();
|
2025-02-04 13:22:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 12:22:00 +02:00
|
|
|
|
public void SoundSilentAlarm(Vector2 location)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsAlarmOn) return;
|
|
|
|
|
|
EmitSignal(nameof(AlarmEnabled), location);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-04 13:22:05 +01:00
|
|
|
|
public void DisableAlarm()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsAlarmOn = false;
|
|
|
|
|
|
EmitSignal(nameof(AlarmDisabled));
|
2025-03-01 23:46:25 +01:00
|
|
|
|
_player?.Stop();
|
2025-02-04 13:22:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|