ld-56/components/Audio/audio.gd
2024-10-06 04:22:46 -04:00

99 lines
2.3 KiB
GDScript

@icon("res://components/Audio/music.svg")
extends Base
var music_data
var sfx_data
var current_playing_music
var tweens = {}
func _spawned():
if _triggerer:
_triggerer.listen("any", _on_trigger)
music_data = _data.data.music
sfx_data = _data.data.sfx
for music in music_data:
var music_value = music_data[music]
if not _data.data.audio.has(music):
_error("Could not find file for music %s" % [music])
continue
music_value.track = _data.data.audio[music]
var music_object = AudioStreamPlayer.new()
music_object.name = "Music - %s" % [music_value.name]
music_object.stream = music_value.track
add_child(music_object)
music_value.object = music_object
_info("Loaded music %s" % [music])
for sfx in sfx_data:
var sfx_value = sfx_data[sfx]
if not _data.data.audio.has(sfx):
_error("Could not find file for sfx %s" % [sfx])
continue
sfx_value.track = _data.data.audio[sfx]
var sfx_object = AudioStreamPlayer.new()
sfx_object.name = "SFX - %s" % [sfx_value.name]
sfx_object.stream = sfx_value.track
add_child(sfx_object)
sfx_value.object = sfx_object
_info("Loaded sfx %s" % [sfx])
func _on_trigger(data: Dictionary) -> void:
var trigger = data.trigger
if not trigger.begins_with("audio_"):
return
var audio_name = trigger.split("_", false, 1)[1]
for music in music_data:
var music_value = music_data[music]
if not music == audio_name:
continue
if not music_value.has("track"):
continue
if current_playing_music:
if tweens.has(current_playing_music):
tweens[current_playing_music].kill()
tweens[current_playing_music] = create_tween()
tweens[current_playing_music].tween_property(music_data[current_playing_music].object, "volume_db", -30, 1)
tweens[current_playing_music].tween_callback(func():
music_data[current_playing_music].object.stop()
)
if tweens.has(music):
tweens[music].kill()
if data.has("keep_position") and data.keep_position:
music_value.object.play(music_data[current_playing_music].object.get_playback_position())
else:
music_value.object.play()
tweens[music] = create_tween()
tweens[music].tween_property(music_data[music].object, "volume_db", 0, 1)
current_playing_music = music
return
for sfx in sfx_data:
if not sfx == audio_name:
continue
pass