From 2062aaf394ee6581161add4bcb23076475e236ac Mon Sep 17 00:00:00 2001 From: "George L. Albany" Date: Mon, 27 Feb 2023 11:26:54 -0500 Subject: 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 --- game/addons/kenney_ui_audio/LICENSE.txt | 21 +++++++++++++++ game/addons/kenney_ui_audio/click3.wav | Bin 0 -> 15694 bytes game/addons/kenney_ui_audio/click3.wav.import | 24 +++++++++++++++++ game/default_theme.theme | Bin 96130 -> 96231 bytes game/project.godot | 2 +- game/src/Autoload/SFX.gd | 20 -------------- game/src/Autoload/SoundManager.gd | 36 ++++++++++++++++++++++++++ game/src/MainMenu/MainMenu.gd | 5 ---- game/src/Utility/StyleBoxWithSound.gd | 33 +++++++++++++++++++++++ 9 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 game/addons/kenney_ui_audio/LICENSE.txt create mode 100644 game/addons/kenney_ui_audio/click3.wav create mode 100644 game/addons/kenney_ui_audio/click3.wav.import delete mode 100644 game/src/Autoload/SFX.gd create mode 100644 game/src/Autoload/SoundManager.gd create mode 100644 game/src/Utility/StyleBoxWithSound.gd diff --git a/game/addons/kenney_ui_audio/LICENSE.txt b/game/addons/kenney_ui_audio/LICENSE.txt new file mode 100644 index 0000000..f1f770a --- /dev/null +++ b/game/addons/kenney_ui_audio/LICENSE.txt @@ -0,0 +1,21 @@ + + + UI SFX Set + + by Kenney Vleugels (Kenney.nl) + + ------------------------------ + + License (Creative Commons Zero, CC0) + http://creativecommons.org/publicdomain/zero/1.0/ + + You may use these assets in personal and commercial projects. + Credit (Kenney or www.kenney.nl) would be nice but is not mandatory. + + ------------------------------ + + Donate: http://support.kenney.nl + Request: http://request.kenney.nl + + Follow on Twitter for updates: + @KenneyNL \ No newline at end of file diff --git a/game/addons/kenney_ui_audio/click3.wav b/game/addons/kenney_ui_audio/click3.wav new file mode 100644 index 0000000..73dc62e Binary files /dev/null and b/game/addons/kenney_ui_audio/click3.wav differ diff --git a/game/addons/kenney_ui_audio/click3.wav.import b/game/addons/kenney_ui_audio/click3.wav.import new file mode 100644 index 0000000..1212f15 --- /dev/null +++ b/game/addons/kenney_ui_audio/click3.wav.import @@ -0,0 +1,24 @@ +[remap] + +importer="wav" +type="AudioStreamWAV" +uid="uid://bsldcs3l8s7ug" +path="res://.godot/imported/click3.wav-191e18de041e05b52c4992cf5dba790b.sample" + +[deps] + +source_file="res://addons/kenney_ui_audio/click3.wav" +dest_files=["res://.godot/imported/click3.wav-191e18de041e05b52c4992cf5dba790b.sample"] + +[params] + +force/8_bit=false +force/mono=false +force/max_rate=false +force/max_rate_hz=44100 +edit/trim=false +edit/normalize=false +edit/loop_mode=0 +edit/loop_begin=0 +edit/loop_end=-1 +compress/mode=0 diff --git a/game/default_theme.theme b/game/default_theme.theme index 514ab3f..2782dba 100644 Binary files a/game/default_theme.theme and b/game/default_theme.theme differ diff --git a/game/project.godot b/game/project.godot index 5031276..d24cbee 100644 --- a/game/project.godot +++ b/game/project.godot @@ -20,7 +20,7 @@ config/project_settings_override.template="user://settings.cfg" Events="*res://src/Autoload/Events.gd" Resolution="*res://src/Autoload/Resolution.gd" -SFX="*res://src/Autoload/SFX.gd" +SoundManager="*res://src/Autoload/SoundManager.gd" Keychain="*res://addons/keychain/Keychain.gd" [display] 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") diff --git a/game/src/MainMenu/MainMenu.gd b/game/src/MainMenu/MainMenu.gd index 7cd02cf..1e15d10 100644 --- a/game/src/MainMenu/MainMenu.gd +++ b/game/src/MainMenu/MainMenu.gd @@ -21,28 +21,23 @@ func _ready(): # REQUIREMENTS: # * UIFUN-32 func _on_new_game_button_pressed(): - SFX.play("click") print("Start a new game!") new_game_button_pressed.emit() func _on_continue_button_pressed(): - SFX.play("click") print("Continue last game!") func _on_multi_player_button_pressed(): - SFX.play("click") print("Have fun with friends!") func _on_options_button_pressed(): - SFX.play("click") print("Check out some options!") options_button_pressed.emit() func _on_exit_button_pressed(): - await SFX.play("click") print("See you later!") get_tree().quit() diff --git a/game/src/Utility/StyleBoxWithSound.gd b/game/src/Utility/StyleBoxWithSound.gd new file mode 100644 index 0000000..8de1af1 --- /dev/null +++ b/game/src/Utility/StyleBoxWithSound.gd @@ -0,0 +1,33 @@ +@tool +extends StyleBox +class_name StyleBoxWithSound + +@export +var style_box : StyleBox: + get: return style_box + set(v): + style_box = v + emit_changed() + +@export +var sound : AudioStream: + get: return sound + set(v): + sound = v + emit_changed() + +func _get_draw_rect(rect : Rect2) -> Rect2: + if style_box == null: return Rect2() + return style_box._get_draw_rect(rect) + +func _draw(to_canvas_item : RID, rect : Rect2) -> void: + # This is a hack + # Works fine for simple non-normal style cases + # Normal styles being drawn immediately tho will trigger sound on startup + # This would require further work to be applicable for release sounds + # Is there any other reason aside from release sounds (might be useful for toggles?) + # This should be fast enough to not cause draw issues + if sound != null: + SoundManager.play_effect_stream(sound) + if style_box != null: + style_box.draw(to_canvas_item, rect) -- cgit v1.2.3-56-ga3b1