Skip to content

Commit

Permalink
refactor: rename 'Settings' to 'Config'
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Feb 4, 2024
1 parent 40b7419 commit 06c71c8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 44 deletions.
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ mod test;
#[allow(unused_imports)]
use bevy_editor_pls::prelude::*;

const SETTINGS_FILE_PATH: &'static str = "settings.toml";
const CONFIG_FILE_PATH: &'static str = "config.toml";
const KEYMAP_FILE_PATH: &'static str = "keymap.toml";

fn save_settings(settings: Res<Settings>) {
let settings_toml = toml::to_string(&*settings).unwrap();
fs::write(SETTINGS_FILE_PATH, settings_toml).unwrap();
fn save_config(config: Res<Config>) {
let config_toml = toml::to_string(&*config).unwrap();
fs::write(CONFIG_FILE_PATH, config_toml).unwrap();
}

#[bevy_main]
fn main() {
if !Path::new(SETTINGS_FILE_PATH).is_file() {
let default_settings_toml = toml::to_string(&Settings::default()).unwrap();
fs::write(SETTINGS_FILE_PATH, default_settings_toml).unwrap();
if !Path::new(CONFIG_FILE_PATH).is_file() {
let default_config_toml = toml::to_string(&Config::default()).unwrap();
fs::write(CONFIG_FILE_PATH, default_config_toml).unwrap();
}
let settings_toml = fs::read_to_string(SETTINGS_FILE_PATH).unwrap();
let settings: Settings = toml::from_str(settings_toml.as_str()).unwrap();
let config_toml = fs::read_to_string(CONFIG_FILE_PATH).unwrap();
let config: Config = toml::from_str(config_toml.as_str()).unwrap();

if !Path::new(KEYMAP_FILE_PATH).is_file() {
let default_keymap_toml = toml::to_string(&default_input_action_map()).unwrap();
Expand All @@ -72,7 +72,7 @@ fn main() {

let player_movement = PlayerMovement {
directions: VecDeque::new(),
timer: Timer::from_seconds(settings.player_move_speed, TimerMode::Repeating),
timer: Timer::from_seconds(config.player_move_speed, TimerMode::Repeating),
};

let mut app = App::new();
Expand Down Expand Up @@ -110,7 +110,7 @@ fn main() {
update_button_state,
handle_audio_event,
adjust_viewport,
save_settings.run_if(resource_changed_or_removed::<Settings>()),
save_config.run_if(resource_changed_or_removed::<Config>()),
(button_input_to_action, handle_actions).chain(),
),
)
Expand Down Expand Up @@ -172,7 +172,7 @@ fn main() {
app.init_resource::<ActionState<Action>>()
.insert_resource(input_action_map);

app.insert_resource(settings)
app.insert_resource(config)
.insert_resource(player_movement)
.insert_resource(AutoMoveState::default());

Expand Down
18 changes: 9 additions & 9 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;

#[derive(Resource, Serialize, Deserialize)]
pub struct Settings {
/// Disable player movement animation.
pub instant_move: bool,
pub struct Config {
/// Player movement animation speed, seconds per step.
pub player_move_speed: f32,
/// Make the floor look like a chessboard with alternating light square and dark square.
pub even_square_shades: f32,
/// Audio volume
/// Audio volume.
pub volume: f64,
/// Disable player movement animation.
pub instant_move: bool,
/// Enable auto switch to next unsolved level when the current level is solved.
pub auto_switch_to_next_unsolved_level: bool,
pub solver: SolverSettings,
pub solver: SolverConfig,
}

impl Default for Settings {
impl Default for Config {
fn default() -> Self {
Self {
instant_move: false,
player_move_speed: 0.1,
even_square_shades: 0.1,
volume: 0.5,
instant_move: false,
auto_switch_to_next_unsolved_level: true,
solver: SolverSettings::default(),
solver: SolverConfig::default(),
}
}
}

#[derive(Serialize, Deserialize, Default)]
pub struct SolverSettings {
pub struct SolverConfig {
pub strategy: Strategy,
pub lower_bound_method: LowerBoundMethod,
}
Expand Down
6 changes: 3 additions & 3 deletions src/systems/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ use crate::resources::*;
pub fn handle_audio_event(
audio: Res<Audio>,
asset_server: Res<AssetServer>,
settings: Res<Settings>,
config: Res<Config>,
mut crate_enter_target_events: EventReader<CrateEnterTarget>,
mut _crate_leave_target_events: EventReader<CrateLeaveTarget>,
mut level_solved_events: EventReader<LevelSolved>,
) {
for _ in level_solved_events.read() {
audio
.play(asset_server.load("audio/success.ogg"))
.with_volume(settings.volume);
.with_volume(config.volume);
crate_enter_target_events.clear();
}
for _ in crate_enter_target_events.read() {
audio
.play(asset_server.load("audio/correct.ogg"))
.with_volume(settings.volume);
.with_volume(config.volume);
}
}
6 changes: 3 additions & 3 deletions src/systems/auto_solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::time::{Duration, Instant};
pub fn load_solver(
mut solver_state: ResMut<SolverState>,
board: Query<&Board>,
settings: Res<Settings>,
config: Res<Config>,
) {
let board = &board.single().board;
let SolverState {
Expand All @@ -24,8 +24,8 @@ pub fn load_solver(
let mut solver = solver.lock().unwrap();
*solver = Solver::new(
level.clone(),
settings.solver.strategy,
settings.solver.lower_bound_method,
config.solver.strategy,
config.solver.lower_bound_method,
);
stopwatch.reset();
}
Expand Down
8 changes: 4 additions & 4 deletions src/systems/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn handle_actions(
mut player_movement: ResMut<PlayerMovement>,
mut level_id: ResMut<LevelId>,
database: Res<Database>,
mut settings: ResMut<Settings>,
mut config: ResMut<Config>,

mut update_grid_position_events: EventWriter<UpdateGridPositionEvent>,
) {
Expand All @@ -125,7 +125,7 @@ pub fn handle_actions(
&database,
board,
);
handle_toggle_instant_move_action(&action_state, &mut settings);
handle_toggle_instant_move_action(&action_state, &mut config);
handle_toggle_toggle_fullscreen_action(&action_state, window);
handle_undo_redo_action(
&action_state,
Expand Down Expand Up @@ -217,10 +217,10 @@ fn handle_clipboard_action(

fn handle_toggle_instant_move_action(
action_state: &ActionState<Action>,
settings: &mut ResMut<Settings>,
config: &mut ResMut<Config>,
) {
if action_state.just_pressed(Action::ToggleInstantMove) {
settings.instant_move = !settings.instant_move;
config.instant_move = !config.instant_move;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/systems/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn spawn_board(
window: Query<&Window>,
board: Query<Entity, With<Board>>,
level_id: Res<LevelId>,
settings: Res<Settings>,
config: Res<Config>,
asset_server: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
Expand Down Expand Up @@ -113,11 +113,11 @@ pub fn spawn_board(
for (tile, (sprite_index, z_order)) in tiles.into_iter() {
if level.get_unchecked(&position).intersects(tile) {
let mut sprite = TextureAtlasSprite::new(sprite_index);
if settings.even_square_shades > 0.0
if config.even_square_shades > 0.0
&& tile == Tile::Floor
&& (x + y) % 2 == 0
{
sprite.color = Color::WHITE * (1.0 - settings.even_square_shades);
sprite.color = Color::WHITE * (1.0 - config.even_square_shades);
}
let mut entity = parent.spawn((
SpriteSheetBundle {
Expand All @@ -144,9 +144,9 @@ pub fn auto_switch_to_next_unsolved_level(
mut board: Query<&mut Board>,
mut level_id: ResMut<LevelId>,
database: Res<Database>,
settings: Res<Settings>,
config: Res<Config>,
) {
if !settings.auto_switch_to_next_unsolved_level {
if !config.auto_switch_to_next_unsolved_level {
return;
}
let database = database.lock().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/systems/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn handle_player_movement(
mut board: Query<&mut Board>,
mut player_movement: ResMut<PlayerMovement>,
time: Res<Time>,
settings: Res<Settings>,
config: Res<Config>,
mut crate_enter_target_events: EventWriter<CrateEnterTarget>,
mut crate_leave_target_events: EventWriter<CrateLeaveTarget>,
mut level_solved_events: EventWriter<LevelSolved>,
Expand All @@ -95,7 +95,7 @@ pub fn handle_player_movement(
let board = &mut board.single_mut().board;

let player_grid_position = &mut **player.single_mut();
if !settings.instant_move {
if !config.instant_move {
player_movement.timer.tick(time.delta());
if !player_movement.timer.just_finished() {
return;
Expand Down Expand Up @@ -161,11 +161,11 @@ pub fn handle_player_movement(
pub fn smooth_tile_motion(
mut tiles: Query<(&mut Transform, &GridPosition)>,
board: Query<&Board>,
settings: Res<Settings>,
config: Res<Config>,
) {
let Board { board, tile_size } = &board.single();
for (mut transform, grid_position) in tiles.iter_mut() {
if !settings.instant_move {
if !config.instant_move {
let lerp = |a: f32, b: f32, t: f32| a + (b - a) * t;

let target_x = grid_position.x as f32 * tile_size.x;
Expand Down
8 changes: 4 additions & 4 deletions src/systems/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,20 @@ pub fn setup_button(mut commands: Commands, asset_server: Res<AssetServer>) {
});
}

/// Updates the state of buttons based on settings.
/// Updates the state of buttons based on config.
pub fn update_button_state(
mut buttons: Query<(&Action, &Children), With<Button>>,
mut image: Query<&mut UiImage>,
settings: Res<Settings>,
config: Res<Config>,
asset_server: Res<AssetServer>,
) {
if !settings.is_changed() {
if !config.is_changed() {
return;
}
for (button, children) in &mut buttons {
if *button == Action::ToggleInstantMove {
let mut image = image.get_mut(children[0]).unwrap();
image.texture = if settings.instant_move {
image.texture = if config.instant_move {
asset_server.load("textures/instant_move_on.png").into()
} else {
asset_server.load("textures/instant_move_off.png").into()
Expand Down

0 comments on commit 06c71c8

Please sign in to comment.