Make a MusicPlayer singleton to manage music and add music fade out
This commit is contained in:
parent
4636b3aefc
commit
28b6d0fc7c
@ -36,7 +36,7 @@
|
||||
{"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": "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": "Even my brother Gary was starting to overcome his depression."},
|
||||
{"name": "Crab", "text": "And then..."}
|
||||
|
@ -38,6 +38,10 @@ boot_splash/image="res://images/titleCrab.png"
|
||||
boot_splash/use_filter=false
|
||||
config/icon="res://images/icon.png"
|
||||
|
||||
[autoload]
|
||||
|
||||
MusicPlayer="*res://scripts/MusicPlayer.gd"
|
||||
|
||||
[display]
|
||||
|
||||
window/size/width=192
|
||||
|
@ -39,7 +39,6 @@ __meta__ = {
|
||||
|
||||
[node name="Dialog" parent="UI" instance=ExtResource( 3 )]
|
||||
visible = false
|
||||
audio_player_node_path = NodePath("../../AudioStreamPlayer")
|
||||
|
||||
[node name="IntroScript" type="Node" parent="."]
|
||||
script = ExtResource( 4 )
|
||||
@ -47,5 +46,3 @@ dialogue_box_path = NodePath("../UI/Dialog")
|
||||
ely_sprite_path = NodePath("../Ely")
|
||||
herbert_sprite_path = NodePath("../Herbert")
|
||||
intro_dialogue = "res://dialogues/intro_1.json"
|
||||
|
||||
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
|
||||
|
@ -13,8 +13,7 @@ onready var text_label = get_node(text_label_path) as RichTextLabel
|
||||
export (NodePath) var expression_node_path
|
||||
onready var expression_node = get_node(expression_node_path) as RichTextLabel
|
||||
|
||||
export (NodePath) var audio_player_node_path
|
||||
onready var audio_player_node = get_node(audio_player_node_path) as AudioStreamPlayer
|
||||
onready var music_player = get_node('/root/MusicPlayer') as MusicPlayer
|
||||
|
||||
var dialogue : Array
|
||||
var index : int
|
||||
@ -28,7 +27,7 @@ var auto_advance = false
|
||||
func _ready():
|
||||
assert(name_label != null)
|
||||
assert(text_label != null)
|
||||
assert(audio_player_node != null)
|
||||
assert(music_player != null)
|
||||
|
||||
func start(_dialogue : Array):
|
||||
dialogue = _dialogue
|
||||
@ -68,12 +67,12 @@ func update() -> void:
|
||||
waiting = true
|
||||
|
||||
# Audio
|
||||
var audio = line.get('audio', '')
|
||||
if audio != '':
|
||||
var path = "res://sounds/" + audio + ".ogg"
|
||||
assert(File.new().file_exists(path))
|
||||
audio_player_node.stream = load(path)
|
||||
audio_player_node.play()
|
||||
var audio = line.get('audio', null)
|
||||
if audio != null:
|
||||
if audio == '':
|
||||
music_player.fade_out()
|
||||
else:
|
||||
music_player.play(audio)
|
||||
|
||||
# UI
|
||||
name_label.bbcode_text = character_name
|
||||
|
68
scripts/MusicPlayer.gd
Normal file
68
scripts/MusicPlayer.gd
Normal 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()
|
Loading…
Reference in New Issue
Block a user