Randoloot/scripts/game/ship_engine_audio.gd
2026-10-09 20:59:55 +02:00

38 lines
1.2 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: CharacterBody2D
func _ready() -> void:
_ship = get_parent() as CharacterBody2D
var audio_data := FileAccess.get_file_as_bytes(ENGINE_SOUND_PATH)
if audio_data.size() <= 44:
push_error("Could not read engine sound: %s" % ENGINE_SOUND_PATH)
return
var engine_stream := AudioStreamMP3.load_from_buffer(audio_data)
if engine_stream == null:
push_error("Could not decode engine sound: %s" % ENGINE_SOUND_PATH)
return
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 := Input.is_action_pressed("ship_thrust")
if bool(_ship.get("_is_destroyed")):
thrust_active = false
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)