monsterfangen/globals/sound_manager.gd

39 lines
1.3 KiB
GDScript

extends Node
@export var music: Array[SoundFile]
@export var sfx: Array[SoundFile]
const FALLBACK: AudioStream = preload("res://assets/sounds/sfx/fallback.wav")
@onready var background_music_player: AudioStreamPlayer = %BackgroundMusicPlayer
var current_background_music: SoundFile = null
func play_background_music(resource_name: String) -> void:
var sound_file: SoundFile = get_sound_file_resource(resource_name, music)
background_music_player.stream = sound_file.file
background_music_player.play()
current_background_music = sound_file
func play_sound_effect(resource_name: String, p_bus: String = "SFX") -> void:
var sound_file: SoundFile = get_sound_file_resource(resource_name, sfx)
# Create a new unlocalized sound effect
var sfx_player: AudioStreamPlayer = AudioStreamPlayer.new()
sfx_player.bus = p_bus
sfx_player.stream = sound_file.file
sfx_player.finished.connect(sfx_player.queue_free)
self.add_child(sfx_player)
sfx_player.play()
func get_sound_file_resource(_name: String, array: Array) -> Variant:
for sound_file: SoundFile in array:
if sound_file.name == _name:
return sound_file
# No sound file with the specified name could be found. Oops!
push_warning("No Sound effect named \"%s\" could be found." % _name)
var oops: SoundFile = SoundFile.new()
oops.file = FALLBACK
return oops