Skip to content

Commit

Permalink
Added music volume control to settings menu
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyTornetta committed Oct 17, 2024
1 parent ea98d2d commit f41f0e7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cosmos_client/assets/cosmos/lang/settings/en_us.lang
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cosmos:brightness=Brightness
cosmos:sensitivity=Mouse Sensitivity
cosmos:fov=Field of View
cosmos:fov=Field of View
cosmos:music_volume=Music Volume
17 changes: 13 additions & 4 deletions cosmos_client/src/audio/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
use bevy::prelude::*;
use bevy_inspector_egui::prelude::*;
use bevy_kira_audio::prelude::*;
use cosmos_core::registry::Registry;
use dynamic_music::MusicAtmosphere;

use crate::settings::{Setting, SettingsRegistry, SettingsSet};

pub mod dynamic_music;

#[derive(Resource)]
Expand All @@ -18,14 +21,14 @@ pub struct VolumeSetting(#[inspector(min = 0.0, max = 1.0)] f64);
impl VolumeSetting {
/// Returns the volume as a decimal percent [0.0, 1.0]
pub fn percent(&self) -> f64 {
self.0
self.0.powf(2.0) * 0.2 // 1.0 is way too loud
}
}

impl Default for VolumeSetting {
/// Initializes the volume to 0.2 (20%).
/// Initializes the volume to 1.0 (100%).
fn default() -> Self {
Self(0.2) // 1.0 is way too loud - in the future introduce a global volume
Self(1.0)
}
}

Expand All @@ -50,7 +53,7 @@ fn adjust_volume(
return;
};

instance.set_volume(volume.0, AudioTween::default());
instance.set_volume(volume.percent(), AudioTween::default());
}

#[derive(Event)]
Expand All @@ -60,6 +63,10 @@ pub struct PlayMusicEvent {
pub atmosphere: MusicAtmosphere,
}

fn load_volume(settings: Res<Registry<Setting>>, mut music_volume: ResMut<VolumeSetting>) {
music_volume.0 = settings.i32_or("cosmos:music_volume", 100) as f64 / 100.0;
}

pub(super) fn register(app: &mut App) {
dynamic_music::register(app);

Expand All @@ -77,4 +84,6 @@ pub(super) fn register(app: &mut App) {
.init_resource::<VolumeSetting>()
.register_type::<VolumeSetting>()
.add_event::<PlayMusicEvent>();

app.add_systems(Update, (load_volume).in_set(SettingsSet::LoadSettings));
}
9 changes: 9 additions & 0 deletions cosmos_client/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum SettingCategory {
Graphics,
/// Mouse
Mouse,
/// Audio
Audio,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -199,6 +201,13 @@ fn register_settings(mut registry: ResMut<Registry<Setting>>) {
SettingCategory::Graphics,
Some(SettingConstraint::I32 { min: 30, max: 120 }),
));

registry.register(Setting::new(
"cosmos:music_volume",
SettingData::I32(100),
SettingCategory::Audio,
Some(SettingConstraint::I32 { min: 0, max: 100 }),
));
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Resource, Default)]
Expand Down
1 change: 1 addition & 0 deletions cosmos_client/src/ui/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fn create_settings_screen(
let category_display_name = match category {
SettingCategory::Graphics => "Graphics",
SettingCategory::Mouse => "Mouse",
SettingCategory::Audio => "Audio",
};

p.spawn(TextBundle {
Expand Down

0 comments on commit f41f0e7

Please sign in to comment.