xiandie/scene/entity/ux/sfx.gd

103 lines
2.3 KiB
GDScript3
Raw Normal View History

@tool
2025-01-07 10:54:50 +00:00
class_name Sfx extends AudioStreamPlayer
2025-01-07 10:54:50 +00:00
# @export var volume_db := 0.0
@export_enum("child", "game/八音盒", "game/旋转锁", "ghost", "lvping", "ui", "c01", "c02") var dir := "ui":
set(value):
dir = value
_update_files()
#@export var file: String = "":
2025-01-07 10:54:50 +00:00
#set(val):
#if val and current_files.has(val):
#file = val
2025-01-21 10:52:36 +00:00
var sfx: AudioStream
var file: String
var sfx_root_path = "res://asset/audio/sfx/"
var current_files := PackedStringArray()
func _ready() -> void:
2025-01-21 10:52:36 +00:00
if Engine.is_editor_hint():
_update_files()
_reload_sfx()
2025-01-07 10:54:50 +00:00
bus = &"game_sfx"
2025-01-14 00:56:51 +00:00
func _reload_sfx():
if file and dir:
2025-01-06 08:06:20 +00:00
# print("reload sfx", sfx_root_path + dir + "/" + file)
sfx = load(sfx_root_path + dir + "/" + file) as AudioStream
else:
sfx = null
2025-01-07 10:54:50 +00:00
stream = sfx
func _update_files():
current_files.clear()
if not dir:
return
var dir_access := DirAccess.open(sfx_root_path + dir) as DirAccess
for f in dir_access.get_files():
if f.ends_with(".wav") or f.ends_with(".ogg") or f.ends_with(".mp3"):
current_files.push_back(f)
#for d in dir_access.get_directories():
2025-01-07 10:54:50 +00:00
#var sub_dir_access := DirAccess.open(sfx_root_path + dir + "/" + d) as DirAccess
#for f in sub_dir_access.get_files():
#if f.ends_with(".wav") or f.ends_with(".ogg") or f.ends_with(".mp3"):
#current_files.push_back(d + "/" + f)
if current_files.is_empty():
file = ""
elif not file or not current_files.has(file):
file = current_files[0]
_reload_sfx()
notify_property_list_changed()
#func _property_can_revert(property: StringName) -> bool:
2025-01-07 10:54:50 +00:00
#if property == &"file":
#return true
#return false
#
#func _property_get_revert(property: StringName) -> Variant:
2025-01-07 10:54:50 +00:00
#if property == &"file":
#return ""
#return null
func _get_property_list():
return [
{
"name": &"file",
"type": TYPE_STRING,
"hint": PROPERTY_HINT_ENUM_SUGGESTION,
"hint_string": ",".join(current_files),
}
]
func _get(property: StringName) -> Variant:
if property == &"file":
return file
return null
func _set(property: StringName, value: Variant) -> bool:
if property == &"file":
file = value
_reload_sfx()
# notify_property_list_changed()
return true
return false
2025-01-21 10:52:36 +00:00
2025-01-07 10:54:50 +00:00
# func play() -> void:
# if sfx:
# AudioManager.play_sfx(sfx, volume_db)
2025-01-14 00:56:51 +00:00
2025-01-21 10:52:36 +00:00
2025-01-14 00:56:51 +00:00
# queue free 导致 sfx 无法播放,使用全局声源
func global_play() -> void:
if sfx:
AudioManager.play_sfx(sfx)