breadwinner/scenes/player/sound_controller_component.gd

192 lines
7.1 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 FLASHLIGHT_CLICK_SOUNDS: Array[AudioStreamWAV] ##flashlight click
@export var SHUFFLE_FOOTSTEP_SOUNDS: Array[AudioStreamWAV] ##shuffling -- random chance of playing while running
@export var SHUFFLE_CHANCE: float = 20 ##percentage of chance for a shuffle noise to be played whilst 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]
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 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 player: PlayerCharacter = $"../.."
## TODO figure out why the sound keeps cutting off when
## shit changes, like the player_property.breathing_mode, even though its change should not change the sound of the stream but still does for some reason??
var debug: bool = true
func _ready() -> void:
left_foot.debug = debug
right_foot.debug = debug
right_foot_texture.debug = debug
left_foot_texture.debug = debug
head.debug = debug
head_texture.debug = debug
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)):
if player.finished:
player.process_mode = Node.PROCESS_MODE_INHERIT
player.do_set_stream(audio)
player.pitch_scale = pitch
player.do_play()
await get_tree().create_timer(3).timeout
player.process_mode = Node.PROCESS_MODE_DISABLED
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 = BREATHE_IN_CALM.pick_random()
player_state.breath.low:
play_sound = BREATHE_IN_LOW.pick_random()
player_state.breath.mid:
play_sound = BREATHE_IN_MID.pick_random()
player_state.breath.heavy:
play_sound = BREATHE_IN_HEAVY.pick_random()
player_state.breath.max:
play_sound = BREATHE_IN_MAX.pick_random()
player_state.breath.exhausted:
play_sound = 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 = BREATHE_OUT_CALM.pick_random()
player_state.breath.low:
play_sound = BREATHE_OUT_LOW.pick_random()
player_state.breath.mid:
play_sound = BREATHE_OUT_MID.pick_random()
player_state.breath.heavy:
play_sound = BREATHE_OUT_HEAVY.pick_random()
player_state.breath.max:
play_sound = BREATHE_OUT_MAX.pick_random()
player_state.breath.exhausted:
play_sound = 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 = FLASHLIGHT_CLICK_SOUNDS.pick_random()
while play_sound == _current_flashlight_sound:
play_sound = 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
match start:
true:
while sound_to_play_foot == _current_jump_start_sound:
sound_to_play_foot = JUMP_STARTING_SOUNDS.pick_random()
while sound_to_play_breath == _current_jump_start_breath_sound:
sound_to_play_breath = JUMP_STARTING_BREATH_SOUNDS.pick_random()
false:
while sound_to_play_foot == _current_jump_land_sound:
sound_to_play_foot = JUMP_LANDING_SOUNDS.pick_random()
while sound_to_play_breath == _current_jump_land_breath_sound:
sound_to_play_breath = JUMP_LANDING_BREATH_SOUNDS.pick_random()
play_stream_on_player(head, sound_to_play_breath)
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 = 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:
play_stream_on_player(is_right_leg.call().get_child(0), SHUFFLE_FOOTSTEP_SOUNDS.pick_random())
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
play_stream_on_player(is_right_leg.call(), sound_to_play)