59 lines
2.3 KiB
GDScript
59 lines
2.3 KiB
GDScript
extends Node
|
|
|
|
|
|
export(float) var true_ambience_begin_threshold = 0.75
|
|
export(float) var masked_ambience_begin_threshold = 0.4
|
|
export(float) var max_volume = 0.15
|
|
export(float) var min_pitch_scale = 0.97 # The pitch won't go down lower than this when the player is sober
|
|
export(float) var sync_threshold = 0.1 # If audio streams are further apart than this (in seconds), they are re-synced
|
|
|
|
onready var melody = get_node("Melody") as AudioStreamPlayer
|
|
onready var true_ambience = get_node("TrueAmbience") as AudioStreamPlayer
|
|
onready var masked_ambience = get_node("MaskedAmbience") as AudioStreamPlayer
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
_update_volumes()
|
|
melody.volume_db = linear2db(max_volume) # This one is constant, so it's only set once here
|
|
|
|
melody.playing = true
|
|
true_ambience.playing = true
|
|
masked_ambience.playing = true
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
_update_volumes()
|
|
|
|
|
|
func _update_volumes():
|
|
var level = Pills.get_normalized_level()
|
|
|
|
if level < masked_ambience_begin_threshold:
|
|
masked_ambience.volume_db = linear2db(0.0)
|
|
true_ambience.volume_db = linear2db(max_volume)
|
|
elif level < true_ambience_begin_threshold:
|
|
# Get the distance between the masked and true thresholds as a value between 0 and 1
|
|
var normalized_distance_within_thresholds = inverse_lerp(masked_ambience_begin_threshold, true_ambience_begin_threshold, level)
|
|
|
|
# Scale volumes accordingly
|
|
masked_ambience.volume_db = linear2db(normalized_distance_within_thresholds * max_volume)
|
|
true_ambience.volume_db = linear2db((1.0 - normalized_distance_within_thresholds) * max_volume)
|
|
else:
|
|
masked_ambience.volume_db = linear2db(max_volume)
|
|
true_ambience.volume_db = linear2db(0.0)
|
|
|
|
# Decrease the pitch when the player is sober
|
|
var pitch = lerp(min_pitch_scale, 1.0, level)
|
|
|
|
melody.pitch_scale = pitch
|
|
masked_ambience.pitch_scale = pitch
|
|
true_ambience.pitch_scale = pitch
|
|
|
|
# Make sure that everything stays in sync - the melody is the leader
|
|
if abs(true_ambience.get_playback_position() - melody.get_playback_position()) > sync_threshold:
|
|
true_ambience.seek(melody.get_playback_position())
|
|
|
|
if abs(masked_ambience.get_playback_position() - melody.get_playback_position()) > sync_threshold:
|
|
masked_ambience.seek(melody.get_playback_position())
|