fix music not playing in exported builds
This commit is contained in:
parent
6a59b54c11
commit
203696e022
45 changed files with 357 additions and 321 deletions
|
@ -1,79 +1,8 @@
|
|||
[gd_scene load_steps=10 format=3 uid="uid://df5m716pd6mej"]
|
||||
[gd_scene load_steps=9 format=3 uid="uid://df5m716pd6mej"]
|
||||
|
||||
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_vumbr"]
|
||||
bounce = 0.2
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_rvq6k"]
|
||||
script/source = "extends RigidBody3D
|
||||
|
||||
var stream_players: Array[AudioStreamPlayer] = []
|
||||
|
||||
func makeAudioStreamPlayers(music_name: String):
|
||||
var path: String = \"res://music/\" + music_name
|
||||
var dir: DirAccess = DirAccess.open(path)
|
||||
var file_paths: Array[String] = []
|
||||
|
||||
assert(dir, path + \" is not a valid path!\")
|
||||
if dir:
|
||||
dir.list_dir_begin()
|
||||
var file_name = dir.get_next()
|
||||
while file_name != \"\":
|
||||
if !dir.current_is_dir():
|
||||
if file_name.ends_with(\".ogg\"):
|
||||
file_paths.push_back(path + \"/\" + file_name)
|
||||
file_name = dir.get_next()
|
||||
assert(len(file_paths), path + \" had no .ogg files!\")
|
||||
|
||||
var players: Array[AudioStreamPlayer] = []
|
||||
for file_path in file_paths:
|
||||
var stream: AudioStreamOggVorbis = AudioStreamOggVorbis.load_from_file(file_path)
|
||||
stream.loop = true
|
||||
stream.bpm = 124
|
||||
|
||||
var player: AudioStreamPlayer = AudioStreamPlayer.new()
|
||||
player.name = file_path.substr(10)
|
||||
player.stream = stream
|
||||
player.volume_db = -50
|
||||
players.push_back(player)
|
||||
add_child(player)
|
||||
|
||||
stream_players = players
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
var level = get_parent().get_parent()
|
||||
if is_instance_valid(level) and level is Node3D:
|
||||
makeAudioStreamPlayers(level.name.to_lower())
|
||||
for player in stream_players:
|
||||
player.play()
|
||||
|
||||
func handleMusicWithVelocity(velocity: float, delta: float) -> void:
|
||||
var instruments_needed = floor(velocity / 7)
|
||||
var instruments_playing = stream_players.filter(func(p: AudioStreamPlayer): return p.volume_db > -50)
|
||||
|
||||
for index_p in len(instruments_playing):
|
||||
var playing = instruments_playing[index_p]
|
||||
if index_p + 1 > instruments_needed:
|
||||
playing.volume_db -= delta * 32
|
||||
elif playing.volume_db <= 0:
|
||||
playing.volume_db += delta * 8
|
||||
|
||||
if instruments_needed > len(instruments_playing):
|
||||
var instruments_not_playing = stream_players.filter(func(p: AudioStreamPlayer): return p.volume_db <= -50)
|
||||
if len(instruments_not_playing):
|
||||
var to_play = instruments_not_playing.pick_random()
|
||||
if is_instance_valid(to_play):
|
||||
to_play.volume_db += delta * 16
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
for player in stream_players:
|
||||
if player.volume_db > 0:
|
||||
player.volume_db = 0
|
||||
var velocity: float = abs(self.linear_velocity.x) + abs(self.linear_velocity.y)
|
||||
handleMusicWithVelocity(velocity, delta)
|
||||
"
|
||||
|
||||
[sub_resource type="SphereShape3D" id="SphereShape3D_xfgrm"]
|
||||
|
||||
[sub_resource type="Gradient" id="Gradient_bgp4x"]
|
||||
|
@ -105,7 +34,6 @@ collision_priority = 100.0
|
|||
axis_lock_linear_z = true
|
||||
physics_material_override = SubResource("PhysicsMaterial_vumbr")
|
||||
contact_monitor = true
|
||||
script = SubResource("GDScript_rvq6k")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("SphereShape3D_xfgrm")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue