Add track weights
This commit is contained in:
parent
f3dfc02119
commit
66117d14aa
1 changed files with 12 additions and 1 deletions
13
src/main.gd
13
src/main.gd
|
@ -52,7 +52,18 @@ func _get_track():
|
|||
if _tracks.size() == 0:
|
||||
return null
|
||||
|
||||
return _tracks.pick_random()
|
||||
var weight = _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:
|
||||
current_weight += track.weight if track.has("weight") else 1
|
||||
|
||||
if current_weight >= rand:
|
||||
return track
|
||||
|
||||
return null
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue