2026-08-09 03:24:28 +02:00
|
|
|
class_name SoundController extends Node
|
2026-07-29 00:05:40 +02:00
|
|
|
|
2026-08-02 16:19:24 +02:00
|
|
|
#@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
|
2026-07-29 02:37:45 +02:00
|
|
|
|
2026-07-29 00:05:40 +02:00
|
|
|
var _current_flashlight_sound = null
|
|
|
|
|
var _current_footstep_sound = null
|
2026-07-29 02:37:45 +02:00
|
|
|
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
|
2026-07-29 00:05:40 +02:00
|
|
|
var _current_leg_is_right = false
|
|
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
var cur_priorities: Dictionary = {
|
|
|
|
|
left_foot: -1,
|
|
|
|
|
right_foot: -1,
|
|
|
|
|
right_foot_texture: -1,
|
|
|
|
|
left_foot_texture: -1,
|
|
|
|
|
head: -1,
|
|
|
|
|
head_texture: -1
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-12 01:47:15 +02:00
|
|
|
@export var player: Player
|
2026-07-30 01:44:27 +02:00
|
|
|
|
2026-08-14 18:10:29 +02:00
|
|
|
@export var left_foot: ChangableAudioStreamPlayer3D
|
|
|
|
|
@export var right_foot: ChangableAudioStreamPlayer3D
|
|
|
|
|
@export var right_foot_texture: ChangableAudioStreamPlayer3D
|
|
|
|
|
@export var left_foot_texture: ChangableAudioStreamPlayer3D
|
|
|
|
|
@export var head: ChangableAudioStreamPlayer3D
|
|
|
|
|
@export var head_texture: ChangableAudioStreamPlayer3D
|
2026-08-09 03:24:28 +02:00
|
|
|
@export var wind: AudioStreamPlayer3D
|
2026-07-30 01:44:27 +02:00
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
#var audio_cooldowns = {
|
|
|
|
|
#"left_foot": 0,
|
|
|
|
|
#"right_foot": 0,
|
|
|
|
|
#"right_foot_texture": 0,
|
|
|
|
|
#"left_foot_texture": 0,
|
|
|
|
|
#"head": 0,
|
|
|
|
|
#"head_texture": 0
|
|
|
|
|
#}
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
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????")
|
|
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
enum Priority {
|
|
|
|
|
LOW = 0,
|
|
|
|
|
MEDIUM = 1,
|
|
|
|
|
HIGH = 2,
|
|
|
|
|
MAX = 3
|
|
|
|
|
}
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
var debug: bool = false
|
2026-08-14 18:10:29 +02:00
|
|
|
var reverb: bool = false
|
2026-07-29 02:37:45 +02:00
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2026-08-14 18:10:29 +02:00
|
|
|
var sound_players:Array[ChangableAudioStreamPlayer3D] = [left_foot, left_foot_texture, right_foot, right_foot_texture, head, head_texture]
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
for i in sound_players:
|
|
|
|
|
i.debug = debug
|
|
|
|
|
i.reverb_enabled = reverb
|
2026-08-13 13:28:13 +02:00
|
|
|
cur_priorities.get_or_add(i, -1)
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
get_tree().create_timer(1).timeout.connect(init_wind)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
2026-08-13 13:28:13 +02:00
|
|
|
#if audio_cooldowns != null:
|
|
|
|
|
#for i in audio_cooldowns:
|
|
|
|
|
#if audio_cooldowns[i] > 0:
|
|
|
|
|
#audio_cooldowns[i] -= delta
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
play_wind()
|
2026-07-29 02:37:45 +02:00
|
|
|
|
2026-08-14 18:10:29 +02:00
|
|
|
var is_right_leg := func () -> ChangableAudioStreamPlayer3D:
|
2026-07-29 02:37:45 +02:00
|
|
|
if _current_leg_is_right:
|
2026-07-29 13:52:14 +02:00
|
|
|
_current_leg_is_right = !_current_leg_is_right
|
2026-07-29 02:37:45 +02:00
|
|
|
return right_foot
|
|
|
|
|
else:
|
2026-07-29 13:52:14 +02:00
|
|
|
_current_leg_is_right = !_current_leg_is_right
|
2026-07-29 02:37:45 +02:00
|
|
|
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
|
2026-08-13 13:28:13 +02:00
|
|
|
spatial_audio.pitch_scale = pitch
|
2026-07-29 02:37:45 +02:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
@warning_ignore("shadowed_variable")
|
2026-08-14 18:10:29 +02:00
|
|
|
func play_stream_on_player(audio_player:ChangableAudioStreamPlayer3D, audio:AudioStream, pitch: float = randf_range(.8, 1.2), volume = 1.0, priority: Priority = Priority.LOW):
|
2026-08-13 13:28:13 +02:00
|
|
|
if not is_instance_valid(audio_player) or audio == null or not audio_player:
|
2026-07-30 01:44:27 +02:00
|
|
|
return
|
|
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
if audio_player.playing:
|
|
|
|
|
if priority < cur_priorities[audio_player]:
|
|
|
|
|
return
|
|
|
|
|
#dont play shi
|
|
|
|
|
|
|
|
|
|
cur_priorities[audio_player] = priority
|
|
|
|
|
|
|
|
|
|
audio_player.do_stop()
|
|
|
|
|
audio_player.do_set_stream(audio)
|
|
|
|
|
audio_player.pitch_scale = pitch
|
|
|
|
|
audio_player.volume_linear = volume ** 2
|
|
|
|
|
audio_player.do_play()
|
|
|
|
|
#audio_cooldowns[get_player_str.call(player)] = audio.get_length() / 1.5
|
2026-07-29 02:37:45 +02:00
|
|
|
|
|
|
|
|
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:
|
2026-08-09 23:18:13 +02:00
|
|
|
match player.player_state.breathing_mode:
|
|
|
|
|
player.player_state.breath.calm:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_IN_CALM)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.low:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_IN_LOW)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.mid:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_IN_MID)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.heavy:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_IN_HEAVY)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.max:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_IN_MAX)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.exhausted:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_IN_EXHAUSTED)
|
2026-07-29 02:37:45 +02:00
|
|
|
_current_breath_in_sound = play_sound
|
|
|
|
|
else:
|
|
|
|
|
play_sound = _current_breath_out_sound
|
|
|
|
|
while play_sound == _current_breath_out_sound:
|
2026-08-09 23:18:13 +02:00
|
|
|
match player.player_state.breathing_mode:
|
|
|
|
|
player.player_state.breath.calm:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_OUT_CALM)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.low:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_OUT_LOW)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.mid:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_OUT_MID)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.heavy:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_OUT_HEAVY)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.max:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_OUT_MAX)
|
2026-08-09 23:18:13 +02:00
|
|
|
player.player_state.breath.exhausted:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.BREATHE_OUT_EXHAUSTED)
|
2026-07-29 02:37:45 +02:00
|
|
|
_current_breath_out_sound = play_sound
|
|
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
play_stream_on_player(head, play_sound, randf_range(0.1, 1.0))
|
2026-07-29 00:05:40 +02:00
|
|
|
|
|
|
|
|
func play_flashlight_sound():
|
2026-08-05 23:37:19 +02:00
|
|
|
var play_sound = Sounds.get_random_sound(Sounds.FLASHLIGHT_CLICK_SOUNDS)
|
2026-07-29 00:05:40 +02:00
|
|
|
|
|
|
|
|
while play_sound == _current_flashlight_sound:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_sound = Sounds.get_random_sound(Sounds.FLASHLIGHT_CLICK_SOUNDS)
|
2026-07-29 00:05:40 +02:00
|
|
|
|
2026-08-09 23:18:13 +02:00
|
|
|
_current_flashlight_sound = play_sound
|
|
|
|
|
|
2026-07-29 02:37:45 +02:00
|
|
|
play_stream_on_player(head_texture, play_sound)
|
2026-07-29 00:05:40 +02:00
|
|
|
_current_flashlight_sound = play_sound
|
2026-07-29 02:37:45 +02:00
|
|
|
|
2026-08-09 23:18:13 +02:00
|
|
|
func play_land_sound(velocity: float):
|
2026-08-13 13:28:13 +02:00
|
|
|
var sound_to_play_foot: AudioStream = Sounds.get_random_sound(Sounds.JUMP_LANDING_SOUNDS)
|
|
|
|
|
var sound_to_play_breath: AudioStream = Sounds.get_random_sound(Sounds.JUMP_LANDING_BREATH_SOUNDS)
|
2026-07-30 01:44:27 +02:00
|
|
|
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)
|
2026-08-09 03:24:28 +02:00
|
|
|
|
2026-08-09 23:18:13 +02:00
|
|
|
volume = velocity
|
|
|
|
|
while sound_to_play_foot == _current_jump_land_sound:
|
|
|
|
|
sound_to_play_foot = Sounds.get_random_sound(Sounds.JUMP_LANDING_SOUNDS)
|
|
|
|
|
while sound_to_play_breath == _current_jump_land_breath_sound:
|
|
|
|
|
sound_to_play_breath = Sounds.get_random_sound(Sounds.JUMP_LANDING_BREATH_SOUNDS)
|
|
|
|
|
|
|
|
|
|
_current_jump_land_sound = sound_to_play_foot
|
|
|
|
|
_current_jump_land_breath_sound = sound_to_play_breath
|
|
|
|
|
|
2026-08-13 13:28:13 +02:00
|
|
|
play_stream_on_player(head, sound_to_play_breath, randf_range(.8, 1.2), volume, Priority.HIGH)
|
|
|
|
|
play_stream_on_player(is_right_leg.call(), sound_to_play_foot, randf_range(.8, 1.2), volume, Priority.HIGH)
|
2026-08-09 23:18:13 +02:00
|
|
|
|
|
|
|
|
func play_jump_sound():
|
2026-08-13 13:28:13 +02:00
|
|
|
var sound_to_play_foot: AudioStream = Sounds.get_random_sound(Sounds.JUMP_STARTING_SOUNDS)
|
|
|
|
|
var sound_to_play_breath: AudioStream = Sounds.get_random_sound(Sounds.JUMP_STARTING_BREATH_SOUNDS)
|
2026-08-09 23:18:13 +02:00
|
|
|
var volume:float = 1.0
|
|
|
|
|
|
|
|
|
|
while sound_to_play_foot == _current_jump_start_sound:
|
|
|
|
|
sound_to_play_foot = Sounds.get_random_sound(Sounds.JUMP_STARTING_SOUNDS)
|
|
|
|
|
while sound_to_play_breath == _current_jump_start_breath_sound:
|
|
|
|
|
sound_to_play_breath = Sounds.get_random_sound(Sounds.JUMP_STARTING_BREATH_SOUNDS)
|
|
|
|
|
|
|
|
|
|
_current_jump_start_sound = sound_to_play_foot
|
|
|
|
|
_current_jump_start_breath_sound = sound_to_play_breath
|
2026-07-29 00:05:40 +02:00
|
|
|
|
2026-07-30 01:44:27 +02:00
|
|
|
play_stream_on_player(head, sound_to_play_breath, randf_range(.8, 1.2), volume)
|
2026-07-29 02:37:45 +02:00
|
|
|
play_stream_on_player(is_right_leg.call(), sound_to_play_foot)
|
|
|
|
|
|
2026-07-29 00:05:40 +02:00
|
|
|
func play_footstep_sound():
|
2026-07-29 02:37:45 +02:00
|
|
|
var sound_to_play:AudioStream
|
2026-08-09 23:18:13 +02:00
|
|
|
if player.player_state.walking:
|
2026-08-05 23:37:19 +02:00
|
|
|
sound_to_play = Sounds.get_random_sound(Sounds.NORMAL_FOOTSTEP_SOUNDS)
|
2026-07-29 00:05:40 +02:00
|
|
|
while sound_to_play == _current_footstep_sound:
|
2026-08-05 23:37:19 +02:00
|
|
|
sound_to_play = Sounds.get_random_sound(Sounds.NORMAL_FOOTSTEP_SOUNDS)
|
2026-08-09 23:18:13 +02:00
|
|
|
elif player.player_state.running:
|
2026-08-05 23:37:19 +02:00
|
|
|
sound_to_play = Sounds.get_random_sound(Sounds.HEAVY_FOOTSTEP_SOUNDS)
|
2026-07-29 00:05:40 +02:00
|
|
|
while sound_to_play == _current_footstep_sound:
|
2026-08-05 23:37:19 +02:00
|
|
|
sound_to_play = Sounds.get_random_sound(Sounds.HEAVY_FOOTSTEP_SOUNDS)
|
2026-08-02 16:19:24 +02:00
|
|
|
if randf_range(1, 100) > 100 - PlayerConsts.SHUFFLE_CHANCE:
|
2026-08-05 23:37:19 +02:00
|
|
|
play_stream_on_player(is_right_leg.call().get_child(0), Sounds.get_random_sound(Sounds.SHUFFLE_FOOTSTEP_SOUNDS))
|
2026-08-09 23:18:13 +02:00
|
|
|
elif player.player_state.crouching:
|
2026-08-05 23:37:19 +02:00
|
|
|
sound_to_play = Sounds.get_random_sound(Sounds.LIGHT_FOOTSTEP_SOUNDS)
|
2026-07-29 00:05:40 +02:00
|
|
|
while sound_to_play == _current_footstep_sound:
|
2026-08-05 23:37:19 +02:00
|
|
|
sound_to_play = Sounds.get_random_sound(Sounds.LIGHT_FOOTSTEP_SOUNDS)
|
2026-07-29 00:05:40 +02:00
|
|
|
|
|
|
|
|
_current_footstep_sound = sound_to_play
|
|
|
|
|
|
2026-07-29 02:37:45 +02:00
|
|
|
play_stream_on_player(is_right_leg.call(), sound_to_play)
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2026-08-09 23:18:13 +02:00
|
|
|
var mouse_strength = player.movement_component.mouse_motion_delta.length() * 5
|
2026-07-30 01:44:27 +02:00
|
|
|
|
|
|
|
|
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
|