forked from RubyGB/frog-twist-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_music_player.gd
74 lines (63 loc) · 1.83 KB
/
global_music_player.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extends AudioStreamPlayer
signal finished_fadeout
signal finished_fadein
var setVolumeDB : float = 0.0
const SILENCE : float = -80.0
const DEFAULT_FADE_DURATION : float = 1.0
const titleStream = preload("res://ui/main menu/frogtwist_title.wav")
const levelAStream = preload("res://level/frogtwist_bgm1.wav")
const levelBStream = preload("res://level/frogtwist_bgm2.wav")
const levelCStream = preload("res://level/frogtwist_bgm3.wav")
const resultsStream = preload("res://level/frogtwist_results.wav")
enum { NO_MUSIC = -1, TITLE, LEVEL_A, LEVEL_B, LEVEL_C, RESULTS }
const ID_MAP = {
TITLE : titleStream,
LEVEL_A : levelAStream,
LEVEL_B : levelBStream,
LEVEL_C : levelCStream,
RESULTS : resultsStream
}
enum FadeState { NONE, OUT, IN }
var currentID : int
var fading : int = FadeState.NONE
var fadeSpeed : float
func _ready() -> void:
bus = "BGM"
playID(TITLE)
func _process(delta : float) -> void:
if fading == FadeState.OUT:
volume_db -= delta * fadeSpeed
if volume_db <= SILENCE:
hardStop()
emit_signal("finished_fadeout")
if fading == FadeState.IN:
volume_db += delta * fadeSpeed
if volume_db >= setVolumeDB:
volume_db = setVolumeDB
fading = FadeState.NONE
emit_signal("finished_fadein")
func playID(id : int) -> void:
fading = FadeState.NONE
volume_db = setVolumeDB
stream = ID_MAP[id]
currentID = id
play()
func hardStop() -> void:
stop()
currentID = NO_MUSIC
fading = FadeState.NONE
func fadeout(duration : float = DEFAULT_FADE_DURATION) -> void:
if fading == FadeState.OUT:
return
fading = FadeState.OUT
fadeSpeed = -SILENCE / duration
volume_db = setVolumeDB
func fadein(id : int, duration : float = DEFAULT_FADE_DURATION) -> void:
if fading == FadeState.IN and id == currentID:
return
fading = FadeState.IN
fadeSpeed = -SILENCE / duration
volume_db = SILENCE
stream = ID_MAP[id]
currentID = id
play()