add a thruster sound when accelerating

This commit is contained in:
yuni 2024-03-17 00:41:06 +01:00
parent e400628af5
commit 5471b46c03
2 changed files with 27 additions and 0 deletions

View file

@ -1,8 +1,12 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy::audio::PlaybackMode;
#[derive(Component)] #[derive(Component)]
pub struct ComponentBGM; pub struct ComponentBGM;
#[derive(Component)]
pub struct ComponentThrusterSound;
#[derive(Resource)] #[derive(Resource)]
struct SoundBGM(Handle<AudioSource>); struct SoundBGM(Handle<AudioSource>);
@ -25,6 +29,17 @@ pub fn setup(
settings: PlaybackSettings::DESPAWN, 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( pub fn toggle_bgm(

View file

@ -2,6 +2,7 @@ use bevy::prelude::*;
use bevy::input::mouse::MouseMotion; use bevy::input::mouse::MouseMotion;
use bevy::window::PrimaryWindow; use bevy::window::PrimaryWindow;
use std::{f32::consts::*, fmt}; use std::{f32::consts::*, fmt};
use crate::audio;
pub struct CameraControllerPlugin; pub struct CameraControllerPlugin;
@ -96,11 +97,13 @@ fn run_camera_controller(
mut mouse_events: EventReader<MouseMotion>, mut mouse_events: EventReader<MouseMotion>,
mouse_button_input: Res<ButtonInput<MouseButton>>, mouse_button_input: Res<ButtonInput<MouseButton>>,
key_input: Res<ButtonInput<KeyCode>>, key_input: Res<ButtonInput<KeyCode>>,
thruster_sound_controller: Query<&AudioSink, With<audio::ComponentThrusterSound>>,
mut toggle_cursor_grab: Local<bool>, mut toggle_cursor_grab: Local<bool>,
mut mouse_cursor_grab: Local<bool>, mut mouse_cursor_grab: Local<bool>,
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>, mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
) { ) {
let dt = time.delta_seconds(); let dt = time.delta_seconds();
let mut play_thruster_sound = false;
let window_result = windows.get_single_mut(); let window_result = windows.get_single_mut();
let mut focused = true; let mut focused = true;
@ -164,6 +167,7 @@ fn run_camera_controller(
controller.walk_speed controller.walk_speed
}; };
controller.velocity = axis_input.normalize() * max_speed; controller.velocity = axis_input.normalize() * max_speed;
play_thruster_sound = true;
} else { } else {
let friction = controller.friction.clamp(0.0, 1.0); let friction = controller.friction.clamp(0.0, 1.0);
controller.velocity *= 1.0 - friction; controller.velocity *= 1.0 - friction;
@ -196,5 +200,13 @@ fn run_camera_controller(
transform.rotation = transform.rotation =
Quat::from_euler(EulerRot::ZYX, 0.0, controller.yaw, controller.pitch); 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()
}
}
} }
} }