Skip to content
Snippets Groups Projects
Verified Commit a8f2badb authored by Louis's avatar Louis :fire:
Browse files

Save & load audio settings

parent bfdb44db
No related branches found
No related tags found
No related merge requests found
......@@ -2414,6 +2414,7 @@ checksum = "f7cd331da7327ecb5727449abb3a63654bd92003bf9a95eed8c9b0d4a93098eb"
dependencies = [
"bevy",
"bevy_kira_audio",
"serde",
]
[[package]]
......
......@@ -16,7 +16,7 @@ serde = "1.0.147"
serde_json = "1.0.87"
iyes_loopless = "0.9.1"
micro_musicbox = { version = "0.5.0", features = ["mp3"] }
micro_musicbox = { version = "0.5.0", features = ["mp3", "serde"] }
micro_banimate = { version = "0.2.1", features = ["ecs_tilemap"] }
kayak_ui = { rev = "c8437b382b0cd1ce950c15cacd170a1c7c7fe5dc", git = "https://github.com/StarArawn/kayak_ui" }
......
......@@ -4,6 +4,8 @@ use game_core::system::flow::AppState;
use iyes_loopless::prelude::AppLooplessStateExt;
fn main() {
let options = game_core::persistance::fs_utils::read_config_file();
App::new()
.add_loopless_state(AppState::Preload)
.add_plugins(game_core::system::resources::InitAppPlugins)
......@@ -20,5 +22,6 @@ fn main() {
.add_plugins(game_core::ui::AdventUIPlugins)
.add_plugin(game_core::persistance::PersistencePlugin)
.add_plugin(bevy_prototype_lyon::plugin::ShapePlugin)
.insert_resource(options.map(|opts| opts.audio.clone()).unwrap_or_default())
.run();
}
use std::path::PathBuf;
use micro_musicbox::prelude::AudioSettings;
use serde::{Deserialize, Serialize};
pub const AUTOSAVE_NAME: &str = "autosave.json";
pub fn get_root_save_dir() -> Option<PathBuf> {
......@@ -15,3 +18,23 @@ pub fn has_auto_save() -> bool {
.map(|dir: PathBuf| dir.join(AUTOSAVE_NAME).exists())
.unwrap_or(false)
}
#[derive(Serialize, Deserialize)]
pub struct Options {
pub audio: AudioSettings,
}
pub fn read_config_file() -> Option<Options> {
let root_dir = get_root_save_dir()?;
let save_path = root_dir.join("config.toml");
let file = std::fs::read(save_path).ok()?;
toml::from_slice(file.as_slice()).ok()
}
pub fn save_config_file(options: Options) -> Option<()> {
let root_dir = get_root_save_dir()?;
let save_path = root_dir.join("config.toml");
std::fs::write(save_path, toml::to_string_pretty(&options).ok()?).ok()
}
......@@ -7,6 +7,7 @@ use kayak_ui::widgets::{
use micro_musicbox::prelude::AudioSettings;
use crate::assets::AssetHandles;
use crate::persistance::fs_utils::{save_config_file, Options};
use crate::persistance::{fs_utils, LoadFileEvent};
use crate::system::flow::AppState;
use crate::ui::components::*;
......@@ -174,6 +175,9 @@ pub fn render_settings_panel(
AudioSettings,
>| {
settings.music_volume = (settings.music_volume - 0.05).max(0.0);
save_config_file(Options {
audio: *settings
});
})}
/>
<BackgroundBundle
......@@ -204,6 +208,9 @@ pub fn render_settings_panel(
AudioSettings,
>| {
settings.music_volume = (settings.music_volume + 0.05).min(1.0);
save_config_file(Options {
audio: *settings
});
})}
/>
</BackgroundBundle>
......
......@@ -49,7 +49,7 @@ impl HungerState {
pub struct StarvationMarker;
pub const PLAYER_FOOD_TARGET: usize = 25;
pub const TOWN_FOOD_TARGET: usize = 75;
pub const TOWN_FOOD_TARGET: usize = 125;
pub fn process_player_hunger_ticks(
world_ticks: EventReader<WorldTickEvent>,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment