Randoloot/scripts/game/ship_engine_audio.gd

37 lines
1.3 KiB
GDScript

extends AudioStreamPlayer
const ENGINE_SOUND_PATH := "res://assets/sounds/effects/engine2.mp3"
const SILENT_VOLUME_DB := -60.0
const THRUST_VOLUME_DB := -11.0
const VOLUME_FADE_DB_PER_SECOND := 68.0
var _ship: Node
var thrust_source: Node
func _ready() -> void:
_ship = thrust_source if is_instance_valid(thrust_source) else get_parent()
var engine_stream := load(ENGINE_SOUND_PATH) as AudioStreamMP3
if engine_stream == null:
push_error("Could not decode engine sound: %s" % ENGINE_SOUND_PATH)
return
engine_stream = engine_stream.duplicate() as AudioStreamMP3
engine_stream.loop = true
stream = engine_stream
volume_db = SILENT_VOLUME_DB
pitch_scale = 0.86
play()
func _process(delta: float) -> void:
if stream == null or not is_instance_valid(_ship):
return
var thrust_active := false
if _ship.has_method("engine_thrust_active"):
thrust_active = bool(_ship.call("engine_thrust_active"))
else:
thrust_active = (Input.is_action_pressed("ship_thrust") or bool(_ship.call("is_mini_boosting"))) and not bool(_ship.get("_is_destroyed"))
var target_volume := THRUST_VOLUME_DB if thrust_active else SILENT_VOLUME_DB
volume_db = move_toward(volume_db, target_volume, VOLUME_FADE_DB_PER_SECOND * delta)
var target_pitch := 1.12 if thrust_active else 0.86
pitch_scale = move_toward(pitch_scale, target_pitch, 0.42 * delta)