breadwinner/scenes/player/sound_controller_component.gd

70 lines
2.7 KiB
GDScript3
Raw Normal View History

extends Node
@export_category("Audio")
@export var AUDIO_LOUDNESS: float = 100.0
@export var NORMAL_FOOTSTEP_SOUNDS: Array[AudioStreamWAV] ##normal footsteps -- walking
@export var HEAVY_FOOTSTEP_SOUNDS: Array[AudioStreamWAV] ##heavy footsteps -- running
@export var LIGHT_FOOTSTEP_SOUNDS: Array[AudioStreamWAV] ##light footsteps -- crouching
@export var SHUFFLE_FOOTSTEP_SOUNDS: Array[AudioStreamWAV] ##shuffling -- random chance of playing while running
@export var FLASHLIGHT_CLICK_SOUNDS: Array[AudioStreamWAV] ##flashlight click
@export var SHUFFLE_CHANCE: float = 20 ##percentage of chance for a shuffle noise to be played whilst running
var _current_flashlight_sound = null
var _current_footstep_sound = null
var _current_leg_is_right = false
@onready var flashlight_sound_player: SpatialAudio3D = %FlashlightSound
@onready var player_state: Node = %PlayerState
@onready var left_foot_texture: SpatialAudio3D = %LeftFootTexture
@onready var right_foot: SpatialAudio3D = %RightFoot
@onready var right_foot_texture: SpatialAudio3D = %RightFootTexture
@onready var left_foot: SpatialAudio3D = %LeftFoot
func play_flashlight_sound():
var play_sound = FLASHLIGHT_CLICK_SOUNDS.pick_random()
while play_sound == _current_flashlight_sound:
play_sound = FLASHLIGHT_CLICK_SOUNDS.pick_random()
flashlight_sound_player.do_set_stream(play_sound)
flashlight_sound_player.pitch_scale = randf_range(.8, 1.2)
flashlight_sound_player.do_play()
_current_flashlight_sound = play_sound
func play_footstep_sound():
var sound_to_play
if player_state.walking:
sound_to_play = NORMAL_FOOTSTEP_SOUNDS.pick_random()
while sound_to_play == _current_footstep_sound:
sound_to_play = NORMAL_FOOTSTEP_SOUNDS.pick_random()
elif player_state.running:
sound_to_play = HEAVY_FOOTSTEP_SOUNDS.pick_random()
while sound_to_play == _current_footstep_sound:
sound_to_play = HEAVY_FOOTSTEP_SOUNDS.pick_random()
if randf_range(1, 100) > 100 - SHUFFLE_CHANCE:
match _current_leg_is_right:
true:
right_foot_texture.do_set_stream(SHUFFLE_FOOTSTEP_SOUNDS.pick_random())
right_foot_texture.do_play()
false:
left_foot_texture.do_set_stream(SHUFFLE_FOOTSTEP_SOUNDS.pick_random())
left_foot_texture.do_play()
elif player_state.crouching:
sound_to_play = LIGHT_FOOTSTEP_SOUNDS.pick_random()
while sound_to_play == _current_footstep_sound:
sound_to_play = LIGHT_FOOTSTEP_SOUNDS.pick_random()
_current_footstep_sound = sound_to_play
if _current_leg_is_right:
right_foot.do_set_stream(sound_to_play)
right_foot.pitch_scale = randf_range(0.8, 1.2)
right_foot.do_play()
_current_leg_is_right = false
else:
left_foot.do_set_stream(sound_to_play)
left_foot.pitch_scale = randf_range(0.8, 1.2)
left_foot.do_play()
_current_leg_is_right = true