cirnogodot/Scripts/AlarmManager.cs

76 lines
1.8 KiB
C#
Raw Normal View History

2025-02-04 13:22:05 +01:00
using Godot;
namespace Cirno.Scripts;
2025-06-23 18:30:42 +02:00
public partial class AlarmManager : Node
2025-02-04 13:22:05 +01:00
{
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();
2025-06-23 18:30:42 +02:00
public Vector3 LastAlarmPosition3D { get; private set; } = new Vector3();
2025-02-04 13:22:05 +01:00
[Signal]
public delegate void AlarmEnabledEventHandler(Vector2 location);
2025-06-23 18:30:42 +02:00
[Signal]
public delegate void AlarmEnabled3DEventHandler(Vector3 location);
2025-02-04 13:22:05 +01:00
[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;
2025-06-23 18:30:42 +02:00
EmitSignalAlarmEnabled(location);
GD.Print($"Alarm sounded at {location}");
_player?.Play();
}
public void SoundAlarm(Vector3 location)
{
if (IsAlarmOn) return;
IsAlarmOn = true;
LastAlarmPosition3D = location;
EmitSignalAlarmEnabled3D(location);
2025-02-04 13:22:05 +01:00
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
}
}