58 lines
1.3 KiB
GDScript
58 lines
1.3 KiB
GDScript
@tool
|
|
class_name Sfx2D extends AudioStreamPlayer2D
|
|
|
|
@export var loop := false
|
|
# 0 一个接一个循环; >0 则每 loop_round_time 播放一次
|
|
@export var loop_round_time := 0.0
|
|
@export var debug_play := false:
|
|
set(val):
|
|
debug_play = false
|
|
if not Engine.is_editor_hint() or not is_node_ready():
|
|
return
|
|
if loop_round_time > 0.0:
|
|
timer.wait_time = loop_round_time
|
|
timer.start()
|
|
else:
|
|
timer.stop()
|
|
play()
|
|
|
|
var timer: Timer
|
|
|
|
|
|
func _ready() -> void:
|
|
bus = &"game_sfx"
|
|
finished.connect(_on_finished)
|
|
timer = Timer.new()
|
|
timer.autostart = autoplay and loop and loop_round_time > 0.0 and not Engine.is_editor_hint()
|
|
timer.one_shot = false
|
|
timer.wait_time = max(1.0, loop_round_time)
|
|
timer.timeout.connect(play)
|
|
add_child(timer)
|
|
|
|
|
|
func _on_finished() -> void:
|
|
if not loop:
|
|
timer.stop()
|
|
elif loop_round_time <= 0:
|
|
play()
|
|
|
|
|
|
func start_timer():
|
|
if timer.wait_time > 0:
|
|
timer.start()
|
|
|
|
|
|
# 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)
|