allow controlling volume for sound/music

This commit is contained in:
2025-12-19 09:08:02 -05:00
parent 1eb88cc1a6
commit 7b233f2220
5 changed files with 86 additions and 41 deletions

View File

@@ -0,0 +1,27 @@
mod music;
mod sounds;
use bevy::prelude::*;
#[derive(Resource, Reflect, Clone, Copy, Debug)]
#[reflect(Resource)]
pub struct SoundSettings {
pub sound_volume: f32,
pub music_volume: f32,
}
impl Default for SoundSettings {
fn default() -> Self {
Self {
sound_volume: 0.8,
music_volume: 0.5,
}
}
}
pub fn plugin(app: &mut App) {
app.init_resource::<SoundSettings>();
app.add_plugins(music::plugin);
app.add_plugins(sounds::plugin);
}

View File

@@ -0,0 +1,45 @@
use crate::{GameState, client::audio::SoundSettings, loading_assets::AudioAssets};
use bevy::{
audio::{PlaybackMode, Volume},
prelude::*,
};
#[derive(Component)]
struct Music;
pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), music);
app.add_systems(Update, settings_changed);
}
fn settings_changed(settings: Res<SoundSettings>, mut query: Query<&mut AudioSink, With<Music>>) {
if settings.is_changed() {
for mut sink in query.iter_mut() {
sink.set_volume(Volume::Linear(settings.music_volume));
}
}
}
fn music(mut commands: Commands, assets: Res<AudioAssets>, settings: Res<SoundSettings>) {
commands.spawn((
Name::new("sfx-music"),
Music,
AudioPlayer::new(assets.music.clone()),
PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::Linear(settings.music_volume),
..default()
},
));
commands.spawn((
Name::new("sfx-ambient"),
Music,
AudioPlayer::new(assets.ambient.clone()),
PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::Linear(settings.music_volume),
..default()
},
));
}

View File

@@ -1,5 +1,7 @@
use crate::{global_observer, loading_assets::AudioAssets, protocol::PlaySound}; use crate::{
use bevy::prelude::*; client::audio::SoundSettings, global_observer, loading_assets::AudioAssets, protocol::PlaySound,
};
use bevy::{audio::Volume, prelude::*};
pub fn plugin(app: &mut App) { pub fn plugin(app: &mut App) {
global_observer!(app, on_spawn_sounds); global_observer!(app, on_spawn_sounds);
@@ -8,14 +10,14 @@ pub fn plugin(app: &mut App) {
fn on_spawn_sounds( fn on_spawn_sounds(
trigger: On<PlaySound>, trigger: On<PlaySound>,
mut commands: Commands, mut commands: Commands,
// settings: SettingsRead,
assets: Res<AudioAssets>, assets: Res<AudioAssets>,
settings: Res<SoundSettings>,
) { ) {
let event = trigger.event(); if settings.sound_volume <= f32::EPSILON {
return;
}
// if !settings.is_sound_on() { let event = trigger.event();
// continue;
// }
let source = match event { let source = match event {
PlaySound::Hit => { PlaySound::Hit => {
@@ -60,6 +62,7 @@ fn on_spawn_sounds(
AudioPlayer::new(source), AudioPlayer::new(source),
PlaybackSettings { PlaybackSettings {
mode: bevy::audio::PlaybackMode::Despawn, mode: bevy::audio::PlaybackMode::Despawn,
volume: Volume::Linear(settings.sound_volume),
..Default::default() ..Default::default()
}, },
)); ));

View File

@@ -26,6 +26,7 @@ use std::{
time::SystemTime, time::SystemTime,
}; };
pub mod audio;
pub mod backpack; pub mod backpack;
pub mod control; pub mod control;
pub mod debug; pub mod debug;
@@ -33,7 +34,6 @@ pub mod enemy;
pub mod heal_effect; pub mod heal_effect;
pub mod player; pub mod player;
pub mod setup; pub mod setup;
pub mod sounds;
pub mod steam; pub mod steam;
pub mod ui; pub mod ui;
@@ -46,7 +46,7 @@ pub fn plugin(app: &mut App) {
heal_effect::plugin, heal_effect::plugin,
player::plugin, player::plugin,
setup::plugin, setup::plugin,
sounds::plugin, audio::plugin,
steam::plugin, steam::plugin,
ui::plugin, ui::plugin,
)); ));

View File

@@ -1,10 +1,5 @@
use crate::{DebugVisuals, GameState, camera::MainCamera, loading_assets::AudioAssets}; use crate::{DebugVisuals, camera::MainCamera};
use bevy::{ use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, render::view::ColorGrading};
audio::{PlaybackMode, Volume},
core_pipeline::tonemapping::Tonemapping,
prelude::*,
render::view::ColorGrading,
};
use bevy_trenchbroom::TrenchBroomServer; use bevy_trenchbroom::TrenchBroomServer;
pub fn plugin(app: &mut App) { pub fn plugin(app: &mut App) {
@@ -21,36 +16,11 @@ pub fn plugin(app: &mut App) {
..Default::default() ..Default::default()
}); });
app.insert_resource(ClearColor(Color::BLACK)); app.insert_resource(ClearColor(Color::BLACK));
//TODO: let user control this
app.insert_resource(GlobalVolume::new(Volume::Linear(0.4)));
app.add_systems(Startup, write_trenchbroom_config); app.add_systems(Startup, write_trenchbroom_config);
app.add_systems(OnEnter(GameState::Playing), music);
app.add_systems(Update, (set_materials_unlit, set_tonemapping, set_shadows)); app.add_systems(Update, (set_materials_unlit, set_tonemapping, set_shadows));
} }
fn music(assets: Res<AudioAssets>, mut commands: Commands) {
commands.spawn((
Name::new("sfx-music"),
AudioPlayer::new(assets.music.clone()),
PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::Linear(0.6),
..default()
},
));
commands.spawn((
Name::new("sfx-ambient"),
AudioPlayer::new(assets.ambient.clone()),
PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::Linear(0.8),
..default()
},
));
}
fn write_trenchbroom_config(server: Res<TrenchBroomServer>, type_registry: Res<AppTypeRegistry>) { fn write_trenchbroom_config(server: Res<TrenchBroomServer>, type_registry: Res<AppTypeRegistry>) {
if let Err(e) = server if let Err(e) = server
.config .config