Skip to content

Commit

Permalink
convert GameConfig to resource
Browse files Browse the repository at this point in the history
  • Loading branch information
thombruce committed Oct 31, 2023
1 parent e627197 commit da7f101
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
22 changes: 21 additions & 1 deletion src/core/resources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@ use bevy::reflect::{TypePath, TypeUuid};
use bevy::window::WindowMode;
use bevy_common_assets::ron::RonAssetPlugin;

use super::{assets::DataAssets, state::GameState};

pub struct ConfigPlugin;
impl Plugin for ConfigPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(RonAssetPlugin::<GameConfig>::new(&["config.ron"]));

app.insert_resource(GameConfig {
window_mode: WindowMode::Fullscreen,
master_volume: 1.0,
});

app.add_systems(OnExit(GameState::Loading), load_config);
}
}

#[derive(serde::Deserialize, TypeUuid, TypePath)]
#[derive(serde::Deserialize, TypeUuid, TypePath, Resource)]
#[uuid = "bdb624ed-62bc-447f-9f89-f361ed58748c"]
pub struct GameConfig {
pub(crate) window_mode: WindowMode,
pub(crate) master_volume: f32,
}

fn load_config(
data: Res<DataAssets>,
mut configs: ResMut<Assets<GameConfig>>,
mut game_config: ResMut<GameConfig>,
) {
if let Some(config) = configs.remove(data.config.id()) {
game_config.window_mode = config.window_mode;
game_config.master_volume = config.master_volume;
}
}
12 changes: 3 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,12 @@ fn main() {

/// The setup function
fn setup(
// TODO: Consider setting config as a resource, so that we
// don't have to query it like this every time we want to
// reference some value.
data: Res<DataAssets>,
configs: ResMut<Assets<GameConfig>>,
config: Res<GameConfig>,
mut window: Query<&mut Window>,
mut volume: ResMut<GlobalVolume>,
) {
if let Some(config) = configs.get(&data.config.clone()) {
window.single_mut().mode = config.window_mode;
volume.volume = VolumeLevel::new(config.master_volume);
}
window.single_mut().mode = config.window_mode;
volume.volume = VolumeLevel::new(config.master_volume);
}

// Documented:
Expand Down

0 comments on commit da7f101

Please sign in to comment.