Music to my ears.
This commit is contained in:
parent
884f49fed3
commit
1570f6986f
15 changed files with 168 additions and 25 deletions
53
autoloads/music.gd
Normal file
53
autoloads/music.gd
Normal file
|
@ -0,0 +1,53 @@
|
|||
@tool
|
||||
extends Node
|
||||
|
||||
|
||||
@export_dir var songs_dir: String
|
||||
@export_tool_button("Update Songs") var _update_songs_action = _update_songs
|
||||
@export var songs: Dictionary[StringName, AudioStream]
|
||||
|
||||
@export var player_a: AudioStreamPlayer
|
||||
@export var player_b: AudioStreamPlayer
|
||||
|
||||
|
||||
@onready var _active_player: AudioStreamPlayer = player_a
|
||||
@onready var _tween: Tween = null
|
||||
|
||||
|
||||
func play_song(id: StringName, crossfade_time: float = 0.0, restart: bool = false) -> void:
|
||||
var new_stream = songs.get(id) as AudioStream
|
||||
if new_stream:
|
||||
if not restart and _active_player.stream == new_stream:
|
||||
return
|
||||
var new_player = player_b if _active_player == player_a else player_a
|
||||
new_player.stream = new_stream
|
||||
new_player.play()
|
||||
|
||||
if crossfade_time > 0.0:
|
||||
new_player.volume_linear = 0.0
|
||||
if _tween:
|
||||
_tween.kill()
|
||||
_tween = create_tween().set_parallel()
|
||||
_tween.tween_property(new_player, ^"volume_linear", 1.0, crossfade_time)
|
||||
_tween.tween_property(_active_player, ^"volume_linear", 0.0, crossfade_time)
|
||||
_tween.chain().tween_callback(_active_player.stop)
|
||||
else:
|
||||
new_player.volume_linear = 1.0
|
||||
_active_player.stop()
|
||||
|
||||
_active_player = new_player
|
||||
|
||||
|
||||
func _update_songs() -> void:
|
||||
songs.clear()
|
||||
var dir = DirAccess.open(songs_dir)
|
||||
if dir:
|
||||
dir.list_dir_begin()
|
||||
var file = dir.get_next()
|
||||
while file != "":
|
||||
if not dir.current_is_dir() and not file.ends_with(".import"):
|
||||
var stream := load(songs_dir + "/" + file) as AudioStream
|
||||
if stream:
|
||||
songs[file.get_basename()] = stream
|
||||
file = dir.get_next()
|
||||
notify_property_list_changed()
|
Loading…
Add table
Add a link
Reference in a new issue