From cc0853eb9a7a7f6b8c32dc1937baeaf5e43d6664 Mon Sep 17 00:00:00 2001 From: Piturnah Date: Thu, 15 Dec 2022 05:05:00 +0000 Subject: [PATCH 1/7] Initial config file implementation --- Cargo.lock | 35 +++++++++++++++++++++++++++++ Cargo.toml | 4 ++++ src/audio.rs | 25 +++++++++++++++++---- src/config.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 +++++- 5 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index a2efa83..fa2e44e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -951,11 +951,15 @@ dependencies = [ "bevy_ecs_ldtk", "bevy_kira_audio", "bevy_ninepatch", + "directories", "itertools", "iyes_loopless", "leafwing-input-manager", "noise", "rand 0.8.5", + "serde", + "serde_derive", + "toml", ] [[package]] @@ -1330,6 +1334,26 @@ dependencies = [ "syn", ] +[[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "discard" version = "1.0.4" @@ -2785,6 +2809,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom 0.2.7", + "redox_syscall", + "thiserror", +] + [[package]] name = "regex" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index bfd37c2..f8077a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,9 +11,13 @@ bevy_ninepatch = "0.9" iyes_loopless = "0.9" leafwing-input-manager = "0.7" +directories = "4.0" itertools = "0.10" noise = { git = "https://github.com/bsurmanski/noise-rs", rev = "5abdde1b819eccc47e74969c15e1b56ae5a055d6" } rand = { version = "0.8", default_features = false, features = ["std", "small_rng"] } +serde = "1.0" +serde_derive = "1.0" +toml = "0.5" [dependencies.bevy] version = "0.9" diff --git a/src/audio.rs b/src/audio.rs index 4e42be4..051e38c 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -3,7 +3,7 @@ use bevy_kira_audio::prelude::*; use iyes_loopless::prelude::*; use rand::prelude::*; -use crate::{GameRng, GameState}; +use crate::{config::Config, GameRng, GameState}; pub struct AudioPlugin; @@ -11,11 +11,13 @@ impl Plugin for AudioPlugin { fn build(&self, app: &mut App) { app.add_event::() .add_audio_channel::() + .add_audio_channel::() .add_startup_system(load_audio) .add_enter_system(GameState::MainMenu, start_title_bgm) .add_enter_system(GameState::InGame, start_fight_bgm) .add_exit_system(GameState::MainMenu, stop_bgm) .add_exit_system(GameState::InGame, stop_bgm) + .add_system(set_volume) .add_system_set( ConditionSet::new() .run_in_state(GameState::InGame) @@ -25,11 +27,14 @@ impl Plugin for AudioPlugin { } } -/// Resource for the background music channel. Exists so in future a user may change BGM volume -/// independently of SFX. +/// Resource for the background music channel. #[derive(Resource)] struct BgmChannel; +/// Resource for the sound effects channel. +#[derive(Resource)] +struct SfxChannel; + /// Event for SFX. #[derive(Debug)] pub enum PlaySfx { @@ -38,6 +43,18 @@ pub enum PlaySfx { BombFuse, } +/// Update the channel volumes if the config has changed. +fn set_volume( + bgm_channel: Res>, + sfx_channel: Res>, + config: Res, +) { + if config.is_changed() { + bgm_channel.set_volume(config.bgm_volume); + sfx_channel.set_volume(config.sfx_volume); + } +} + fn start_title_bgm(audio: Res>, bgm: Res) { audio.play(bgm.title_screen.clone()).looped(); } @@ -60,7 +77,7 @@ fn stop_bgm(audio: Res>) { } fn play_sfx( - audio: Res