Add functionality to make track selection less random

This commit is contained in:
Ategon 2024-08-28 19:56:18 -04:00
parent 8dce1ee84b
commit abeb696804

View file

@ -6,6 +6,7 @@ extends CanvasLayer
var music_bus = AudioServer.get_bus_index("Music")
var _tracks = []
var current_track
func _ready():
@ -37,6 +38,7 @@ func _play():
audio_stream_player.stream = load(song.track)
audio_stream_player.play()
music_display.show_song(song)
current_track = song
func _stop():
@ -52,12 +54,29 @@ func _get_track():
if _tracks.size() == 0:
return null
var weight = _tracks.reduce(func(accum, track): return accum + (track.weight if track.has("weight") else 1), 0)
var reduced_tracks = _tracks
var random_type_split = randf()
if reduced_tracks.size() > 1:
reduced_tracks = reduced_tracks.filter(func(track): return track.track != current_track.track)
var temp_tracks
if current_track:
if random_type_split < 0.4:
temp_tracks = reduced_tracks.filter(func(track): return track.artist == current_track.artist)
elif random_type_split < 0.8:
temp_tracks = reduced_tracks.filter(func(track): return track.definition_file == current_track.definition_file)
if temp_tracks.size() > 0:
reduced_tracks = temp_tracks
var weight = reduced_tracks.reduce(func(accum, track): return accum + (track.weight if track.has("weight") else 1), 0)
var rand = randf() * weight
var current_weight = 0
for track in _tracks:
for track in reduced_tracks:
current_weight += track.weight if track.has("weight") else 1
if current_weight >= rand:
@ -66,7 +85,6 @@ func _get_track():
return null
func _load_tracks():
_load_directory("res://tracks")
print("Loaded music")
@ -88,6 +106,7 @@ func _load_directory(path: String):
for track in new_tracks:
if track.has("disabled") and track.disabled:
continue
track.definition_file = "%s/%s" % [path, file_name]
_tracks.push_back(track)
else:
_load_directory("res://tracks/%s" % [file_name])