-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial config file implementation * Log unexpected key and use systems Uses only ECS systems now to tidy it up. Also will log a warning if there is an unexpected key in the config file. * Add clippy lints Add `semicolon_if_nothing_returned` and `uninlined_format_args` for more consistent code style * Update README.md * Add `shell.nix` * Add instructions and feature for wayland * Remove config plugin We don't need to pretend that we can change the config while the game is running (yet).
- Loading branch information
Showing
5 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//! Load the settings file for the game. This will be under the config folder by OS convention, for | ||
//! example: | ||
//! | ||
//! Linux: `~/.config/bomby/config.toml` | ||
//! | ||
//! Currently, the config is loaded at startup and cannot be changed from inside the game. So, this | ||
//! module does not export a bevy plugin (yet). | ||
use bevy::prelude::*; | ||
|
||
use directories::ProjectDirs; | ||
use serde_derive::{Deserialize, Serialize}; | ||
|
||
use std::fs; | ||
|
||
const DEFAULT_ASPECT_RATIO: f32 = 16.0 / 9.0; | ||
const DEFAULT_WINDOW_HEIGHT: f32 = 900.0; | ||
const DEFAULT_WINDOW_WIDTH: f32 = DEFAULT_WINDOW_HEIGHT * DEFAULT_ASPECT_RATIO; | ||
|
||
/// Config resource containing runtime settings for the game. | ||
#[derive(Resource, Debug, Serialize, Deserialize)] | ||
#[serde(default)] | ||
pub struct Config { | ||
pub window_resizable: bool, | ||
pub window_width: f32, | ||
pub window_height: f32, | ||
pub bgm_volume: f64, | ||
pub sfx_volume: f64, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Self { | ||
window_resizable: true, | ||
window_width: DEFAULT_WINDOW_WIDTH, | ||
window_height: DEFAULT_WINDOW_HEIGHT, | ||
bgm_volume: 1.0, | ||
sfx_volume: 1.0, | ||
} | ||
} | ||
} | ||
|
||
/// Load the [`Config`] or generate a new one and insert it as a resource. | ||
pub fn load_config() -> Config { | ||
let dirs = ProjectDirs::from("com", "Spicy Lobster", "Bomby"); | ||
let mut config = dirs | ||
.map(|dirs| { | ||
let mut path = dirs.config_dir().to_path_buf(); | ||
path.push("config.toml"); | ||
let config_str = fs::read_to_string(&path).unwrap_or_else(|_| "".to_string()); | ||
let mut de = toml::de::Deserializer::new(&config_str); | ||
let mut unused_keys = Vec::new(); | ||
let config = | ||
serde_ignored::deserialize(&mut de, |path| unused_keys.push(path.to_string())) | ||
.unwrap_or_else(|e| { | ||
warn!("failed to parse config file {path:?}: {e}"); | ||
Config::default() | ||
}); | ||
|
||
for key in unused_keys { | ||
warn!("unrecognised config setting: {key}"); | ||
} | ||
config | ||
}) | ||
.unwrap_or_else(|| { | ||
warn!("failed to get config path"); | ||
Config::default() | ||
}); | ||
|
||
// Ensure sensible bounds. | ||
config.bgm_volume = config.bgm_volume.clamp(0.0, 1.0); | ||
config.sfx_volume = config.sfx_volume.clamp(0.0, 1.0); | ||
|
||
config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters