breadwinner/scenes/player/sound_controller_component.gd
Balogh Krisztián 3367698d10 (maybe) broken resource loader for the sounds
added a dloading screen, currently only used to load sounds in automatically (you can cahnge the sounds in the assets and it will work automatically since this autoload will load it in on game start ready)
2026-08-02 16:19:24 +02:00

250 lines
8.9 KiB
GDScript

extends Node
#@export_category("Audio")
#@export var NORMAL_FOOTSTEP_SOUNDS: Array[AudioStream] ##normal footsteps -- walking
#@export var HEAVY_FOOTSTEP_SOUNDS: Array[AudioStream] ##heavy footsteps -- running
#@export var LIGHT_FOOTSTEP_SOUNDS: Array[AudioStream] ##light footsteps -- crouching
#@export var FLASHLIGHT_CLICK_SOUNDS: Array[AudioStream] ##flashlight click
#@export var SHUFFLE_FOOTSTEP_SOUNDS: Array[AudioStream] ##shuffling -- random chance of playing while running
#
#@export var JUMP_LANDING_SOUNDS: Array[AudioStream]
#@export var JUMP_STARTING_SOUNDS: Array[AudioStream]
#@export var JUMP_LANDING_BREATH_SOUNDS: Array[AudioStream]
#@export var JUMP_STARTING_BREATH_SOUNDS: Array[AudioStream]
#
#@export_group("Breathing")
#@export var BREATHE_IN_CALM: Array[AudioStream]
#@export var BREATHE_IN_LOW: Array[AudioStream]
#@export var BREATHE_IN_MID: Array[AudioStream]
#@export var BREATHE_IN_HEAVY: Array[AudioStream]
#@export var BREATHE_IN_MAX: Array[AudioStream]
#@export var BREATHE_IN_EXHAUSTED: Array[AudioStream]
#
#@export var BREATHE_OUT_CALM: Array[AudioStream]
#@export var BREATHE_OUT_LOW: Array[AudioStream]
#@export var BREATHE_OUT_MID: Array[AudioStream]
#@export var BREATHE_OUT_HEAVY: Array[AudioStream]
#@export var BREATHE_OUT_MAX: Array[AudioStream]
#@export var BREATHE_OUT_EXHAUSTED: Array[AudioStream]
#
#@export_group("Whitenoise")
#@export var WHITENOISE: AudioStream
var _current_flashlight_sound = null
var _current_footstep_sound = null
var _current_jump_start_sound = null
var _current_jump_start_breath_sound = null
var _current_jump_land_sound = null
var _current_jump_land_breath_sound = null
var _current_breath_in_sound = null
var _current_breath_out_sound = null
var _current_leg_is_right = false
@onready var player_state: Node = %PlayerState
@onready var player: CharacterBody3D = $"../.."
@onready var input_controller: Node = %InputController
@onready var left_foot: SpatialAudio3D = %LeftFoot
@onready var right_foot: SpatialAudio3D = %RightFoot
@onready var right_foot_texture: SpatialAudio3D = %RightFootTexture
@onready var left_foot_texture: SpatialAudio3D = %LeftFootTexture
@onready var head: SpatialAudio3D = %Head
@onready var head_texture: SpatialAudio3D = %HeadTexture
@onready var wind: AudioStreamPlayer = %Wind
var audio_cooldowns = {
"left_foot": 0,
"right_foot": 0,
"right_foot_texture": 0,
"left_foot_texture": 0,
"head": 0,
"head_texture": 0
}
var get_player_str = func (player:SpatialAudio3D):
match player:
left_foot: return "left_foot"
right_foot: return "right_foot"
right_foot_texture: return "right_foot_texture"
left_foot_texture: return "left_foot_texture"
head: return "head"
head_texture: return "head_texture"
_: push_error("problem in get_player_str in sound_controller_component.gd. fuck????")
var debug: bool = false
var reverb: bool = true
func _ready() -> void:
var sound_players:Array[SpatialAudio3D] = [left_foot, left_foot_texture, right_foot, right_foot_texture, head, head_texture]
for i in sound_players:
i.debug = debug
i.reverb_enabled = reverb
get_tree().create_timer(1).timeout.connect(init_wind)
player_state.landed.connect(play_jump_sound)
player_state.jumped.connect(play_jump_sound)
func _process(delta: float) -> void:
if audio_cooldowns != null:
for i in audio_cooldowns:
if audio_cooldowns[i] > 0:
audio_cooldowns[i] -= delta
play_wind()
var is_right_leg := func () -> SpatialAudio3D:
if _current_leg_is_right:
_current_leg_is_right = !_current_leg_is_right
return right_foot
else:
_current_leg_is_right = !_current_leg_is_right
return left_foot
##only use for sparse effects
func create_spatial_audio_sphere(pos:Vector3, audio:AudioStream, pitch:float = 1.0):
var spatial_audio = SpatialAudio3D.new()
spatial_audio.collision_mask = 1
spatial_audio.stream = audio
add_child(spatial_audio)
spatial_audio.global_position = pos
if is_instance_valid(spatial_audio) and audio != null:
spatial_audio.do_play()
spatial_audio.get_child(0).name = audio.resource_name
await get_tree().create_timer(audio.get_length() + 2).timeout
if is_instance_valid(spatial_audio):
spatial_audio.queue_free()
elif is_instance_valid(spatial_audio):
spatial_audio.queue_free()
func play_stream_on_player(player:SpatialAudio3D, audio:AudioStream, pitch: float = randf_range(.8, 1.2), volume = 1.0):
if not is_instance_valid(player) or audio == null:
return
if audio_cooldowns[get_player_str.call(player)] <= 0:
player.do_set_stream(audio)
player.pitch_scale = pitch
player.volume_db = player.volume_db * volume
player.do_play()
audio_cooldowns[get_player_str.call(player)] = audio.get_length() / 1.5
func play_breath_sound(breathe_in:bool = true):
var play_sound
if breathe_in:
play_sound = _current_breath_in_sound
while play_sound == _current_breath_in_sound:
match player_state.breathing_mode:
player_state.breath.calm:
play_sound = Sounds.BREATHE_IN_CALM.pick_random()
player_state.breath.low:
play_sound = Sounds.BREATHE_IN_LOW.pick_random()
player_state.breath.mid:
play_sound = Sounds.BREATHE_IN_MID.pick_random()
player_state.breath.heavy:
play_sound = Sounds.BREATHE_IN_HEAVY.pick_random()
player_state.breath.max:
play_sound = Sounds.BREATHE_IN_MAX.pick_random()
player_state.breath.exhausted:
play_sound = Sounds.BREATHE_IN_EXHAUSTED.pick_random()
_current_breath_in_sound = play_sound
else:
play_sound = _current_breath_out_sound
while play_sound == _current_breath_out_sound:
match player_state.breathing_mode:
player_state.breath.calm:
play_sound = Sounds.BREATHE_OUT_CALM.pick_random()
player_state.breath.low:
play_sound = Sounds.BREATHE_OUT_LOW.pick_random()
player_state.breath.mid:
play_sound = Sounds.BREATHE_OUT_MID.pick_random()
player_state.breath.heavy:
play_sound = Sounds.BREATHE_OUT_HEAVY.pick_random()
player_state.breath.max:
play_sound = Sounds.BREATHE_OUT_MAX.pick_random()
player_state.breath.exhausted:
play_sound = Sounds.BREATHE_OUT_EXHAUSTED.pick_random()
_current_breath_out_sound = play_sound
play_stream_on_player(head, play_sound)
func play_flashlight_sound():
var play_sound = Sounds.FLASHLIGHT_CLICK_SOUNDS.pick_random()
while play_sound == _current_flashlight_sound:
play_sound = Sounds.FLASHLIGHT_CLICK_SOUNDS.pick_random()
play_stream_on_player(head_texture, play_sound)
_current_flashlight_sound = play_sound
func play_jump_sound(start:bool = true):
var sound_to_play_foot: AudioStream = null
var sound_to_play_breath: AudioStream = null
var volume:float = 1.0
##TODO: make the landing sound volume scale with the fallen height (the higher you fall from, the bigger the stomp)
match start:
true:
while sound_to_play_foot == _current_jump_start_sound:
sound_to_play_foot = Sounds.JUMP_STARTING_SOUNDS.pick_random()
while sound_to_play_breath == _current_jump_start_breath_sound:
sound_to_play_breath = Sounds.JUMP_STARTING_BREATH_SOUNDS.pick_random()
false:
print(volume)
while sound_to_play_foot == _current_jump_land_sound:
sound_to_play_foot = Sounds.JUMP_LANDING_SOUNDS.pick_random()
while sound_to_play_breath == _current_jump_land_breath_sound:
sound_to_play_breath = Sounds.JUMP_LANDING_BREATH_SOUNDS.pick_random()
play_stream_on_player(head, sound_to_play_breath, randf_range(.8, 1.2), volume)
play_stream_on_player(is_right_leg.call(), sound_to_play_foot)
func play_footstep_sound():
var sound_to_play:AudioStream
if player_state.walking:
sound_to_play = Sounds.NORMAL_FOOTSTEP_SOUNDS.pick_random()
while sound_to_play == _current_footstep_sound:
sound_to_play = Sounds.NORMAL_FOOTSTEP_SOUNDS.pick_random()
elif player_state.running:
sound_to_play = Sounds.HEAVY_FOOTSTEP_SOUNDS.pick_random()
while sound_to_play == _current_footstep_sound:
sound_to_play = Sounds.HEAVY_FOOTSTEP_SOUNDS.pick_random()
if randf_range(1, 100) > 100 - PlayerConsts.SHUFFLE_CHANCE:
play_stream_on_player(is_right_leg.call().get_child(0), Sounds.SHUFFLE_FOOTSTEP_SOUNDS.pick_random())
elif player_state.crouching:
sound_to_play = Sounds.LIGHT_FOOTSTEP_SOUNDS.pick_random()
while sound_to_play == _current_footstep_sound:
sound_to_play = Sounds.LIGHT_FOOTSTEP_SOUNDS.pick_random()
_current_footstep_sound = sound_to_play
play_stream_on_player(is_right_leg.call(), sound_to_play)
var wind_filter: AudioEffectFilter
var wind_strength: float = 0 ## set the strength of the base wind, VERY SENSITIVE
func init_wind():
var bus_idx = AudioServer.get_bus_index("wind")
wind_filter = AudioServer.get_bus_effect(bus_idx,0)
wind.volume_db = -80
wind.play()
func play_wind():
var strength = player.velocity.length() * 10 + abs(player.velocity.y) * 15
var mouse_strength = input_controller.mouse_motion_delta.length() * 5
if wind_filter is AudioEffectFilter:
wind_filter.cutoff_hz = strength / 2 + mouse_strength / 10 + wind_strength
wind_filter.resonance = 0.5
wind.volume_linear = strength / 100 + mouse_strength / 10000 + wind_strength