66 lines
2.1 KiB
GDScript
66 lines
2.1 KiB
GDScript
class_name AudioLibrary
|
|
extends Node
|
|
|
|
const AMBIENCE_CONTEXTS := ["exploration", "combat", "danger"]
|
|
const SFX_EVENTS := ["attack_given", "attack_received", "menu_select", "menu_cancel", "item_use", "death"]
|
|
const ADMITTED_EXTENSIONS := ["mp3", "ogg", "oga", "wav", "flac", "m4a", "opus"]
|
|
const DEFAULT_AUDIO_HOME := ".local/share/dcwascii"
|
|
|
|
var current_ambience_context := ""
|
|
var _audio_home_override := ""
|
|
var _last_ambience_path := ""
|
|
var _last_sfx_path := ""
|
|
|
|
func _init(audio_home_override: String = "") -> void:
|
|
_audio_home_override = audio_home_override
|
|
|
|
func set_ambience_context(context: String) -> void:
|
|
if not context in AMBIENCE_CONTEXTS:
|
|
return
|
|
current_ambience_context = context
|
|
_last_ambience_path = _find_first_audio_file("audio", current_ambience_context)
|
|
|
|
func play_sfx(event: String) -> void:
|
|
_last_sfx_path = ""
|
|
if not event in SFX_EVENTS:
|
|
return
|
|
_last_sfx_path = _find_first_audio_file("sfx", event)
|
|
|
|
func get_last_ambience_path() -> String:
|
|
return _last_ambience_path
|
|
|
|
func get_last_sfx_path() -> String:
|
|
return _last_sfx_path
|
|
|
|
func has_playable_ambience() -> bool:
|
|
return false
|
|
|
|
func has_playable_sfx() -> bool:
|
|
return false
|
|
|
|
func _audio_roots() -> Array[String]:
|
|
if not _audio_home_override.is_empty():
|
|
return [_audio_home_override]
|
|
var roots: Array[String] = []
|
|
var environment_root := OS.get_environment("DCW_AUDIO_HOME")
|
|
if not environment_root.is_empty():
|
|
roots.append(environment_root)
|
|
roots.append(OS.get_environment("HOME").path_join(DEFAULT_AUDIO_HOME))
|
|
return roots
|
|
|
|
func _find_first_audio_file(kind: String, name: String) -> String:
|
|
for root in _audio_roots():
|
|
var directory_path := root.path_join(kind).path_join(name)
|
|
var directory := DirAccess.open(directory_path)
|
|
if directory == null:
|
|
continue
|
|
directory.list_dir_begin()
|
|
var entry := directory.get_next()
|
|
while not entry.is_empty():
|
|
if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS:
|
|
directory.list_dir_end()
|
|
return directory_path.path_join(entry)
|
|
entry = directory.get_next()
|
|
directory.list_dir_end()
|
|
return ""
|