aboutsummaryrefslogtreecommitdiff
path: root/game/src/Autoload
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2023-02-27 17:26:54 +0100
committer GitHub <noreply@github.com>2023-02-27 17:26:54 +0100
commit2062aaf394ee6581161add4bcb23076475e236ac (patch)
treeadb1a6e5fe4c7d06c778188961a6e660ee911413 /game/src/Autoload
parent150c11376765af5dc0c180de9cf7bbcc8fa9b99b (diff)
Refactor SFX into a SoundManager (#45)
* Refactor SFX into a SoundManager Add SoundManager able to play arbitrary sound streams Make SoundManager use only one AudioStreamPlayer per bus Add StyleBoxWithSound Add Kenney UI Audio click3.wav Removed sound play via pressed signals in MainMenu Make Button_MainMenu pressed style StyleBoxWithSound with click3.wav sound * Add playing mp3 files by name to the SoundManager * Fix missing quotation
Diffstat (limited to 'game/src/Autoload')
-rw-r--r--game/src/Autoload/SFX.gd20
-rw-r--r--game/src/Autoload/SoundManager.gd36
2 files changed, 36 insertions, 20 deletions
diff --git a/game/src/Autoload/SFX.gd b/game/src/Autoload/SFX.gd
deleted file mode 100644
index 5299848..0000000
--- a/game/src/Autoload/SFX.gd
+++ /dev/null
@@ -1,20 +0,0 @@
-extends Node
-
-var _loaded_sfx = {}
-
-func _ready():
- var dir = DirAccess.open("res://audio/sfx/")
- for fname in dir.get_files():
- if fname.get_extension() == "ogg":
- _loaded_sfx[fname.get_basename()] = load("res://audio/sfx/" + fname) # SND-10
-
-# SND-7
-func play(sound):
- var player = AudioStreamPlayer.new()
- player.bus = "SFX"
- player.stream = _loaded_sfx[sound]
- add_child(player)
- player.play()
- await player.finished
- remove_child(player)
-
diff --git a/game/src/Autoload/SoundManager.gd b/game/src/Autoload/SoundManager.gd
new file mode 100644
index 0000000..d3f8039
--- /dev/null
+++ b/game/src/Autoload/SoundManager.gd
@@ -0,0 +1,36 @@
+extends Node
+
+const _audio_directory_path : StringName = &"res://audio/sfx/"
+
+var _loaded_sound : Dictionary = {}
+
+var _bus_to_stream_player : Dictionary = {}
+
+func _ready():
+ var dir = DirAccess.open(_audio_directory_path)
+ for fname in dir.get_files():
+ match fname.get_extension():
+ "ogg", "wav", "mp3":
+ _loaded_sound[fname.get_basename()] = load(_audio_directory_path.path_join(fname)) # SND-10
+
+func play_stream(sound : AudioStream, bus_type : String) -> void:
+ var player : AudioStreamPlayer = _bus_to_stream_player.get(bus_type)
+ if player == null:
+ player = AudioStreamPlayer.new()
+ player.bus = bus_type
+ player.stream = AudioStreamPolyphonic.new()
+ _bus_to_stream_player[bus_type] = player
+ add_child(player)
+ player.play()
+ var poly_playback : AudioStreamPlaybackPolyphonic = player.get_stream_playback()
+ poly_playback.play_stream(sound)
+
+func play(sound : String, bus_type : String) -> void:
+ play_stream(_loaded_sound[sound], bus_type)
+
+# SND-7
+func play_effect_stream(sound : AudioStream) -> void:
+ play_stream(sound, "SFX")
+
+func play_effect(sound : String) -> void:
+ play(sound, "SFX")