Randoloot/scripts/game/ship_engine_audio.gd
2026-10-11 13:26:14 +02:00

34 lines
1.1 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 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 := 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)