aboutsummaryrefslogtreecommitdiff
path: root/game/src/MusicConductor/MusicConductor.gd
blob: a463d6d5f70be205073aa21de698b0e63d488089 (plain) (blame)
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
extends Node

# SS-67
@export_dir var musicDir : String
@export_file var firstSongName : String

var selectedTrack = 0
var availableSongs : Array[SongInfo] = []
var autoPlayNextSong : bool = true

func getAllSongNames() -> Array[String]:
   var songNames : Array[String] = []
   for si in availableSongs:
      songNames.append(si.readableName)
   return songNames

func getCurrentSongIndex() -> int:
   return selectedTrack

func getCurrentSongName() -> String:
   return availableSongs[selectedTrack].readableName

func scrubSongByPercentage(percentage: float) -> void:
   var percentInSeconds : float = (percentage / 100.0) * $AudioStreamPlayer.stream.get_length()
   $AudioStreamPlayer.play(percentInSeconds)

func getCurrentSongProgressPercentage() -> float:
   return 100 * ($AudioStreamPlayer.get_playback_position() / $AudioStreamPlayer.stream.get_length())

func isPaused() -> bool:
   return $AudioStreamPlayer.stream_paused

func togglePlayPause() -> void:
   $AudioStreamPlayer.stream_paused = !$AudioStreamPlayer.stream_paused
   
func startCurrentSong() -> void:
   $AudioStreamPlayer.stream = availableSongs[selectedTrack].loadedStream
   $AudioStreamPlayer.play()

# SS-70
func startSongByIndex(id: int) -> void:
   selectedTrack = id
   startCurrentSong()

# SS-69
func nextSong() -> void:
   selectedTrack = (selectedTrack + 1) % len(availableSongs)
   startCurrentSong()

func prevSong() -> void:
   selectedTrack = (len(availableSongs) - 1) if (selectedTrack == 0) else (selectedTrack - 1)
   startCurrentSong()

# Called when the node enters the scene tree for the first time.
func _ready():
   var dir = DirAccess.open(musicDir)
   for fname in dir.get_files():
      if !fname.ends_with(".import"):
         if fname.get_basename() == firstSongName:
            selectedTrack = availableSongs.size()
         availableSongs.append(SongInfo.new(musicDir, fname))
   startCurrentSong()


func _on_audio_stream_player_finished():
   if autoPlayNextSong:
      nextSong()
      startCurrentSong()