xiandie/scene/entity/general/sfx.gd

84 lines
2.0 KiB
GDScript

@tool
class_name Sfx extends AudioStreamPlayer
@export_enum("child", "game/八音盒", "game/旋转锁", "ghost", "lvping", "ui", "c01", "c02") var dir := "ui":
set(value):
dir = value
if Engine.is_editor_hint():
_update_files()
var file: String
var sfx_root_path = "res://asset/audio/sfx/"
var current_files := PackedStringArray()
func _ready() -> void:
bus = &"game_sfx"
# TODO 暂时停用其额外效果
# # 仅在编辑器模式下加载音频 stream
# if Engine.is_editor_hint():
# _update_files()
# _reload_sfx()
# notify_property_list_changed()
func _reload_sfx():
var path = sfx_root_path + dir + "/" + file
if file and dir and FileAccess.file_exists(path):
stream = load(sfx_root_path + dir + "/" + file) as AudioStream
if stream:
print("sfx [", name, "] stream=", stream.resource_path)
else:
print("sfx [", name, "] stream is null")
func _update_files():
current_files.clear()
if not dir:
return
var dir_access := DirAccess.open(sfx_root_path + dir) as DirAccess
if not dir_access:
push_warning("sfx dir_access is null for ", sfx_root_path + dir)
return
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)
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()
return true
return false
# queue free 导致 sfx 无法播放,使用全局声源
func global_play() -> void:
if stream:
AudioManager.play_sfx(stream)
# 注意:会导致 volume db 变化
func easing_kill(duration: float = 2.0) -> void:
# stop with easing
if playing:
var tween = create_tween()
tween.tween_property(self, "volume_db", -80.0, duration)
tween.tween_callback(stop)