From 5471b46c037df04ea29cae356e371026f635d0e5 Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 17 Mar 2024 00:41:06 +0100 Subject: [PATCH] add a thruster sound when accelerating --- src/audio.rs | 15 +++++++++++++++ src/camera.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/audio.rs b/src/audio.rs index 9159d19..d44d26b 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,8 +1,12 @@ use bevy::prelude::*; +use bevy::audio::PlaybackMode; #[derive(Component)] pub struct ComponentBGM; +#[derive(Component)] +pub struct ComponentThrusterSound; + #[derive(Resource)] struct SoundBGM(Handle); @@ -25,6 +29,17 @@ pub fn setup( settings: PlaybackSettings::DESPAWN, }, )); + commands.spawn(( + ComponentThrusterSound, + AudioBundle { + source: asset_server.load("restricted/loopingthrust-95548.ogg"), + settings: PlaybackSettings { + mode: PlaybackMode::Loop, + paused: true, + ..default() + }, + }, + )); } pub fn toggle_bgm( diff --git a/src/camera.rs b/src/camera.rs index ae7df81..cdcb025 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; use bevy::input::mouse::MouseMotion; use bevy::window::PrimaryWindow; use std::{f32::consts::*, fmt}; +use crate::audio; pub struct CameraControllerPlugin; @@ -96,11 +97,13 @@ fn run_camera_controller( mut mouse_events: EventReader, mouse_button_input: Res>, key_input: Res>, + thruster_sound_controller: Query<&AudioSink, With>, mut toggle_cursor_grab: Local, mut mouse_cursor_grab: Local, mut query: Query<(&mut Transform, &mut CameraController), With>, ) { let dt = time.delta_seconds(); + let mut play_thruster_sound = false; let window_result = windows.get_single_mut(); let mut focused = true; @@ -164,6 +167,7 @@ fn run_camera_controller( controller.walk_speed }; controller.velocity = axis_input.normalize() * max_speed; + play_thruster_sound = true; } else { let friction = controller.friction.clamp(0.0, 1.0); controller.velocity *= 1.0 - friction; @@ -196,5 +200,13 @@ fn run_camera_controller( transform.rotation = Quat::from_euler(EulerRot::ZYX, 0.0, controller.yaw, controller.pitch); } + + if let Ok(sink) = thruster_sound_controller.get_single() { + if play_thruster_sound { + sink.play() + } else { + sink.pause() + } + } } }