Make a MusicPlayer singleton to manage music and add music fade out

This commit is contained in:
Alice Gaudon 2020-08-18 11:55:07 +02:00
parent 4636b3aefc
commit 28b6d0fc7c
5 changed files with 81 additions and 13 deletions

View File

@ -36,7 +36,7 @@
{"name": "Ely", "text": "Also, what made you the first crab to talk to me [i]ever[/i]?"}, {"name": "Ely", "text": "Also, what made you the first crab to talk to me [i]ever[/i]?"},
{"name": "Herbert", "text": "I'll tell you my tale, but then please tell me yours."}, {"name": "Herbert", "text": "I'll tell you my tale, but then please tell me yours."},
{"name": "Ely", "text": "Sure!"}, {"name": "Ely", "text": "Sure!"},
{"name": "Crab", "text": "Everything was fine."}, {"name": "Crab", "text": "Everything was fine.", "audio": ""},
{"name": "Crab", "text": "I was fine, my friends were fine..."}, {"name": "Crab", "text": "I was fine, my friends were fine..."},
{"name": "Crab", "text": "Even my brother Gary was starting to overcome his depression."}, {"name": "Crab", "text": "Even my brother Gary was starting to overcome his depression."},
{"name": "Crab", "text": "And then..."} {"name": "Crab", "text": "And then..."}

View File

@ -38,6 +38,10 @@ boot_splash/image="res://images/titleCrab.png"
boot_splash/use_filter=false boot_splash/use_filter=false
config/icon="res://images/icon.png" config/icon="res://images/icon.png"
[autoload]
MusicPlayer="*res://scripts/MusicPlayer.gd"
[display] [display]
window/size/width=192 window/size/width=192

View File

@ -39,7 +39,6 @@ __meta__ = {
[node name="Dialog" parent="UI" instance=ExtResource( 3 )] [node name="Dialog" parent="UI" instance=ExtResource( 3 )]
visible = false visible = false
audio_player_node_path = NodePath("../../AudioStreamPlayer")
[node name="IntroScript" type="Node" parent="."] [node name="IntroScript" type="Node" parent="."]
script = ExtResource( 4 ) script = ExtResource( 4 )
@ -47,5 +46,3 @@ dialogue_box_path = NodePath("../UI/Dialog")
ely_sprite_path = NodePath("../Ely") ely_sprite_path = NodePath("../Ely")
herbert_sprite_path = NodePath("../Herbert") herbert_sprite_path = NodePath("../Herbert")
intro_dialogue = "res://dialogues/intro_1.json" intro_dialogue = "res://dialogues/intro_1.json"
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]

View File

@ -13,8 +13,7 @@ onready var text_label = get_node(text_label_path) as RichTextLabel
export (NodePath) var expression_node_path export (NodePath) var expression_node_path
onready var expression_node = get_node(expression_node_path) as RichTextLabel onready var expression_node = get_node(expression_node_path) as RichTextLabel
export (NodePath) var audio_player_node_path onready var music_player = get_node('/root/MusicPlayer') as MusicPlayer
onready var audio_player_node = get_node(audio_player_node_path) as AudioStreamPlayer
var dialogue : Array var dialogue : Array
var index : int var index : int
@ -28,7 +27,7 @@ var auto_advance = false
func _ready(): func _ready():
assert(name_label != null) assert(name_label != null)
assert(text_label != null) assert(text_label != null)
assert(audio_player_node != null) assert(music_player != null)
func start(_dialogue : Array): func start(_dialogue : Array):
dialogue = _dialogue dialogue = _dialogue
@ -68,12 +67,12 @@ func update() -> void:
waiting = true waiting = true
# Audio # Audio
var audio = line.get('audio', '') var audio = line.get('audio', null)
if audio != '': if audio != null:
var path = "res://sounds/" + audio + ".ogg" if audio == '':
assert(File.new().file_exists(path)) music_player.fade_out()
audio_player_node.stream = load(path) else:
audio_player_node.play() music_player.play(audio)
# UI # UI
name_label.bbcode_text = character_name name_label.bbcode_text = character_name

68
scripts/MusicPlayer.gd Normal file
View File

@ -0,0 +1,68 @@
extends Node
var fading = false
var animation_time : float
var animation_duration : float
var target_track : AudioStreamPlayer
var previous_tracks : Array
func play(music, offset=0, duration=2):
print('Play music: ' + music)
var path = "res://sounds/" + music + ".ogg"
if !File.new().file_exists(path):
path = "res://sounds/" + music
assert(File.new().file_exists(path))
var stream = load(path)
target_track = get_free_track()
previous_tracks = get_playing_tracks()
if previous_tracks.size() > 0:
fading = true
animation_time = 0
animation_duration = duration
target_track.stream = stream
target_track.play(offset)
func fade_out(duration=2):
target_track = null
previous_tracks = get_playing_tracks()
fading = true
animation_time = 0
animation_duration = duration
func get_playing_tracks() -> Array:
var tracks = []
for track in get_children():
if track.playing:
tracks.append(track)
return tracks
func get_free_track() -> AudioStreamPlayer:
var free_track = null
for track in get_children():
if !track.playing:
free_track = track
if free_track == null:
free_track = AudioStreamPlayer.new()
print('yo')
add_child(free_track)
print(free_track)
return free_track
func _process(delta):
if fading:
animation_time += delta
var p = clamp(animation_time / animation_duration, 0, 1)
if target_track != null:
target_track.volume_db = lerp(-90, 0, clamp(p * 2, 0, 1))
for previous_track in previous_tracks:
previous_track.volume_db = lerp(0, -90, clamp((p * 2) - 1, 0, 1))
if p == 1:
fading = false
for previous_track in previous_tracks:
previous_track.stop()
previous_track.queue_free()