aboutsummaryrefslogtreecommitdiff
path: root/game/src/MusicConductor/MusicConductor.gd
diff options
context:
space:
mode:
author ClarkeCode <33846391+ClarkeCode@users.noreply.github.com>2023-03-06 22:14:19 +0100
committer GitHub <noreply@github.com>2023-03-06 22:14:19 +0100
commitce325c16c0f5d82ed51abd8bd13928a7bc609ba5 (patch)
tree9684123684de590ecd3be99be034d68f4bc6f8b7 /game/src/MusicConductor/MusicConductor.gd
parent95173891f7c5eea7717a58ae4f1438fd09e0ee1f (diff)
Add Music Player (#49)
* Adding MusicConductor * Added selectable songs and player visibility toggle * Refinements to the music system * SongInfo compatability with various audio formats * Moved UI reqs to editor description; flipped conditions to exclude music .import files * Made selection of the first music track extension-agnostic * Fixed visual bug with play/pause button when interacting with progress slider
Diffstat (limited to 'game/src/MusicConductor/MusicConductor.gd')
-rw-r--r--game/src/MusicConductor/MusicConductor.gd68
1 files changed, 68 insertions, 0 deletions
diff --git a/game/src/MusicConductor/MusicConductor.gd b/game/src/MusicConductor/MusicConductor.gd
new file mode 100644
index 0000000..a463d6d
--- /dev/null
+++ b/game/src/MusicConductor/MusicConductor.gd
@@ -0,0 +1,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()