add settings.rs

This commit is contained in:
yuni 2024-03-16 22:20:23 +01:00
parent 50e731a3ca
commit b223dcdb29
2 changed files with 40 additions and 0 deletions

View file

@ -2,6 +2,7 @@ mod audio;
mod player; mod player;
mod camera; mod camera;
mod world; mod world;
mod settings;
use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode }; use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode };
use bevy::prelude::*; use bevy::prelude::*;
@ -10,6 +11,7 @@ fn main() {
App::new() App::new()
.add_systems(Startup, ( .add_systems(Startup, (
setup, setup,
settings::setup,
audio::setup, audio::setup,
player::setup, player::setup,
world::setup, world::setup,
@ -37,9 +39,16 @@ fn setup(
fn handle_input( fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut settings: Query<&mut settings::Settings>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>> mut app_exit_events: ResMut<Events<bevy::app::AppExit>>
) { ) {
if keyboard_input.pressed(KeyCode::KeyQ) { if keyboard_input.pressed(KeyCode::KeyQ) {
app_exit_events.send(bevy::app::AppExit); app_exit_events.send(bevy::app::AppExit);
} }
if keyboard_input.just_pressed(KeyCode::F12) {
let result = settings.get_single_mut();
if result.is_ok() {
result.unwrap().reset()
}
}
} }

31
src/settings.rs Normal file
View file

@ -0,0 +1,31 @@
use bevy::prelude::*;
#[derive(Component)]
pub struct Settings {
pub mute_sfx: bool,
pub mute_music: bool,
pub volume_sfx: u8,
pub volume_music: u8,
}
impl Default for Settings {
fn default() -> Self {
Settings {
mute_sfx: false,
mute_music: false,
volume_sfx: 100,
volume_music: 100,
}
}
}
impl Settings {
pub fn reset(&mut self) {
println!("Resetting settings!");
*self = Self::default();
}
}
pub fn setup(mut commands: Commands) {
commands.spawn(Settings::default());
}