Implement audio management system with effects and music players; add settings for sound and music volume
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
class_name OwniverseEffectsPlayer
|
||||
extends AudioStreamPlayer
|
||||
|
||||
const EFFECT_COOLDOWN_MSEC := 2000
|
||||
|
||||
var enabled: bool = true
|
||||
var _effects: Dictionary = OwniverseAudioLoader.get_effects()
|
||||
var _last_played_msec: Dictionary = {}
|
||||
|
||||
|
||||
func set_enabled(value: bool) -> void:
|
||||
enabled = value
|
||||
stream_paused = not enabled
|
||||
|
||||
|
||||
func play_effect(effect_id: String) -> bool:
|
||||
if not enabled:
|
||||
return false
|
||||
if not _effects.has(effect_id):
|
||||
push_warning("Unknown sound effect: " + effect_id)
|
||||
return false
|
||||
if playing and stream == _effects[effect_id]:
|
||||
return false
|
||||
var now: int = Time.get_ticks_msec()
|
||||
if now - int(_last_played_msec.get(effect_id, -EFFECT_COOLDOWN_MSEC)) < EFFECT_COOLDOWN_MSEC:
|
||||
return false
|
||||
|
||||
stream = _effects[effect_id]
|
||||
play()
|
||||
_last_played_msec[effect_id] = now
|
||||
return true
|
||||
@@ -0,0 +1 @@
|
||||
uid://botb0k4qr0hqo
|
||||
@@ -0,0 +1,69 @@
|
||||
class_name OwniverseMusicPlayer
|
||||
extends AudioStreamPlayer
|
||||
|
||||
const EXCLUDED_TRACK := "apocalypse_the_end"
|
||||
const MIN_PAUSE_SECONDS := 120.0
|
||||
const MAX_PAUSE_SECONDS := 300.0
|
||||
|
||||
var enabled: bool = true
|
||||
var last_track: String = ""
|
||||
var _pause_timer: Timer
|
||||
var _rng := RandomNumberGenerator.new()
|
||||
var _music: Dictionary = OwniverseAudioLoader.get_music()
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_rng.randomize()
|
||||
_pause_timer = Timer.new()
|
||||
_pause_timer.one_shot = true
|
||||
_pause_timer.timeout.connect(_play_next)
|
||||
add_child(_pause_timer)
|
||||
finished.connect(_schedule_pause)
|
||||
if enabled:
|
||||
_schedule_pause()
|
||||
|
||||
|
||||
func set_enabled(value: bool) -> void:
|
||||
enabled = value
|
||||
if not is_node_ready():
|
||||
return
|
||||
|
||||
stream_paused = not enabled
|
||||
_pause_timer.paused = not enabled
|
||||
if enabled and not playing and _pause_timer.is_stopped():
|
||||
_play_next()
|
||||
|
||||
|
||||
func play_apocalypse() -> void:
|
||||
if not enabled:
|
||||
return
|
||||
if not _music.has(EXCLUDED_TRACK):
|
||||
push_warning("Apocalypse music is unavailable.")
|
||||
return
|
||||
|
||||
_pause_timer.stop()
|
||||
stream = _music[EXCLUDED_TRACK]
|
||||
play()
|
||||
|
||||
|
||||
func _play_next() -> void:
|
||||
if not enabled:
|
||||
return
|
||||
|
||||
var candidates: Array = _music.keys()
|
||||
candidates.erase(EXCLUDED_TRACK)
|
||||
candidates.erase(last_track)
|
||||
if candidates.is_empty():
|
||||
push_warning("No music tracks available for playback.")
|
||||
return
|
||||
|
||||
var track_id: String = str(candidates[_rng.randi_range(0, candidates.size() - 1)])
|
||||
last_track = track_id
|
||||
stream = _music[track_id]
|
||||
play()
|
||||
|
||||
|
||||
func _schedule_pause() -> void:
|
||||
if not enabled:
|
||||
return
|
||||
_pause_timer.start(_rng.randf_range(MIN_PAUSE_SECONDS, MAX_PAUSE_SECONDS))
|
||||
@@ -0,0 +1 @@
|
||||
uid://btsrt6rw3nr04
|
||||
@@ -1 +1,67 @@
|
||||
extends VBoxContainer
|
||||
|
||||
const MIN_VOLUME_DB := -50.0
|
||||
const SILENT_VOLUME_DB := -80.0
|
||||
|
||||
@onready var sound_button: TextureButton = $Media/Sound
|
||||
@onready var music_button: TextureButton = $Media/Music
|
||||
@onready var sound_volume: HSlider = $Volume/SoundVolume
|
||||
@onready var music_volume: HSlider = $Volume/MusicVolume
|
||||
@onready var effects_player: OwniverseEffectsPlayer = $"../../FXPlayer"
|
||||
@onready var music_player: OwniverseMusicPlayer = $"../../MusicPlayer"
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
SaveManager.load_settings()
|
||||
sound_button.set_pressed_no_signal(bool(Global.Settings.sound_enabled))
|
||||
music_button.set_pressed_no_signal(bool(Global.Settings.music_enabled))
|
||||
sound_volume.set_value_no_signal(float(Global.Settings.sound_volume) * sound_volume.max_value)
|
||||
music_volume.set_value_no_signal(float(Global.Settings.music_volume) * music_volume.max_value)
|
||||
_apply_sound_settings()
|
||||
_apply_music_settings()
|
||||
|
||||
|
||||
func _on_sound_pressed() -> void:
|
||||
Global.Settings.sound_enabled = sound_button.button_pressed
|
||||
_apply_sound_settings()
|
||||
SaveManager.save_settings()
|
||||
|
||||
|
||||
func _on_music_pressed() -> void:
|
||||
Global.Settings.music_enabled = music_button.button_pressed
|
||||
_apply_music_settings()
|
||||
SaveManager.save_settings()
|
||||
|
||||
|
||||
func _on_sound_volume_value_changed(value: float) -> void:
|
||||
Global.Settings.sound_volume = value / sound_volume.max_value
|
||||
_apply_bus_volume("FX", float(Global.Settings.sound_volume))
|
||||
SaveManager.save_settings()
|
||||
|
||||
|
||||
func _on_music_volume_value_changed(value: float) -> void:
|
||||
Global.Settings.music_volume = value / music_volume.max_value
|
||||
_apply_bus_volume("Music", float(Global.Settings.music_volume))
|
||||
SaveManager.save_settings()
|
||||
|
||||
|
||||
func _apply_sound_settings() -> void:
|
||||
var bus_index: int = AudioServer.get_bus_index("FX")
|
||||
AudioServer.set_bus_mute(bus_index, not bool(Global.Settings.sound_enabled))
|
||||
effects_player.set_enabled(bool(Global.Settings.sound_enabled))
|
||||
_apply_bus_volume("FX", float(Global.Settings.sound_volume))
|
||||
|
||||
|
||||
func _apply_music_settings() -> void:
|
||||
music_player.set_enabled(bool(Global.Settings.music_enabled))
|
||||
_apply_bus_volume("Music", float(Global.Settings.music_volume))
|
||||
|
||||
|
||||
func _apply_bus_volume(bus_name: String, value: float) -> void:
|
||||
var bus_index: int = AudioServer.get_bus_index(bus_name)
|
||||
if bus_index < 0:
|
||||
push_warning("Missing audio bus: " + bus_name)
|
||||
return
|
||||
var normalized: float = clampf(value, 0.0, 1.0)
|
||||
var volume_db: float = SILENT_VOLUME_DB if is_zero_approx(normalized) else lerpf(MIN_VOLUME_DB, 0.0, normalized)
|
||||
AudioServer.set_bus_volume_db(bus_index, volume_db)
|
||||
|
||||
Reference in New Issue
Block a user