mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
Concurrent audio limit and volume limiter
This commit is contained in:
parent
f2670cb1e7
commit
a533f93361
11 changed files with 162 additions and 6 deletions
52
Scripts/Misc/AudioManager.cs
Normal file
52
Scripts/Misc/AudioManager.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Misc;
|
||||
|
||||
public partial class AudioManager : Node2D
|
||||
{
|
||||
public static AudioManager Instance { get; private set; }
|
||||
|
||||
[Export]
|
||||
public int ConcurrentSounds { get; set; } = 3;
|
||||
|
||||
private Dictionary<string, int> _audioDict = new Dictionary<string, int>();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public bool CanPlay(string audioName)
|
||||
{
|
||||
var item = _audioDict.TryGetValue(audioName, out int amount);
|
||||
if (item) return amount < ConcurrentSounds;
|
||||
_audioDict.Add(audioName, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Play(string audioName)
|
||||
{
|
||||
if (!CanPlay(audioName)) return false;
|
||||
if (!_audioDict.ContainsKey(audioName))
|
||||
{
|
||||
_audioDict.Add(audioName, 0);
|
||||
}
|
||||
_audioDict[audioName] += 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Stop(string audioName)
|
||||
{
|
||||
if (_audioDict.ContainsKey(audioName))
|
||||
{
|
||||
_audioDict[audioName] -= 1;
|
||||
|
||||
if (_audioDict[audioName] < 0)
|
||||
{
|
||||
_audioDict[audioName] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Scripts/Misc/AudioManager.cs.uid
Normal file
1
Scripts/Misc/AudioManager.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ci2px8dicc0xx
|
||||
82
Scripts/Misc/LimitedAudioPlayer.cs
Normal file
82
Scripts/Misc/LimitedAudioPlayer.cs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Misc;
|
||||
|
||||
public partial class LimitedAudioPlayer : AudioStreamPlayer2D
|
||||
{
|
||||
[Export] public StringName AudioName { get; private set; }
|
||||
[Export] public bool ReparentOnCreation { get; private set; } = false;
|
||||
[Export] public float Duration { get; private set; } = -1;
|
||||
|
||||
private bool _finished = false;
|
||||
|
||||
private double _timer = 0;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (ReparentOnCreation)
|
||||
{
|
||||
CallDeferred(MethodName.ReparentDeferred);
|
||||
}
|
||||
|
||||
if (Duration >= 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Check if it can play
|
||||
if (IsAutoplayEnabled())
|
||||
{
|
||||
TryPlay();
|
||||
}
|
||||
|
||||
this.Finished += OnFinished;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Duration >= 0 && IsPlaying())
|
||||
{
|
||||
_timer += delta;
|
||||
|
||||
if (_timer >= Duration)
|
||||
{
|
||||
this.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ReparentDeferred()
|
||||
{
|
||||
this.Reparent(GameManager.Instance.BulletsContainer);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
this.Finished -= OnFinished;
|
||||
|
||||
if (!_finished)
|
||||
{
|
||||
AudioManager.Instance.Stop(AudioName);
|
||||
}
|
||||
}
|
||||
|
||||
public void TryPlay()
|
||||
{
|
||||
if (!AudioManager.Instance.Play(AudioName))
|
||||
{
|
||||
this.SetVolumeDb(-100);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.SetVolumeDb(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFinished()
|
||||
{
|
||||
AudioManager.Instance.Stop(AudioName);
|
||||
_finished = true;
|
||||
}
|
||||
}
|
||||
1
Scripts/Misc/LimitedAudioPlayer.cs.uid
Normal file
1
Scripts/Misc/LimitedAudioPlayer.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dwnqgkuj6bgay
|
||||
Loading…
Add table
Add a link
Reference in a new issue