Skip to content

Commit

Permalink
feat: support reading settings from settings.toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Jan 15, 2024
1 parent 8fe342c commit 204b265
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.fleet
/.idea
database.db
settings.toml
50 changes: 44 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ nalgebra = "0.32"
bevy = "0.12"
leafwing-input-manager = "0.11"
bevy_editor_pls = "0.7"
rusqlite = { version = "0.30", features = ["bundled"] }
siphasher = "1.0"
itertools = "0.12.0"
serde = { version = "1.0.195", features = ["derive"] }

rusqlite = { version = "0.30", features = ["bundled"] }
arboard = "3.3" # 系统剪切板
image = "0.24"
winit = "0.28" # 版本需要与 bevy 中使用的保持一致
itertools = "0.12.0"
toml = "0.8.8"

[profile.dev.package."*"]
opt-level = 3
30 changes: 24 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// #![feature(test)]

use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;

use std::collections::VecDeque;
use std::fs;
use std::path::Path;

mod level;

use level::*;

mod systems;
Expand All @@ -26,15 +35,24 @@ mod movement;
mod solver;
mod test;

use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;

#[allow(unused_imports)]
use bevy_editor_pls::prelude::*;

#[bevy_main]
fn main() {
const SETTINGS_FILE_PATH: &'static str = "settings.toml";
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);
}
let settings_toml = fs::read_to_string(SETTINGS_FILE_PATH).unwrap();
let settings: Settings = toml::from_str(settings_toml.as_str()).unwrap();

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

App::new()
.add_plugins((
DefaultPlugins,
Expand Down Expand Up @@ -87,10 +105,10 @@ fn main() {
.add_event::<SelectCrate>()
.add_event::<UnselectCrate>()
.add_event::<UpdateGridPositionEvent>()
.insert_resource(Settings::default())
.init_resource::<ActionState<Action>>()
.insert_resource(Action::input_map())
.insert_resource(PlayerMovement::default())
.insert_resource(settings)
.insert_resource(player_movement)
.insert_resource(CrateReachable::default())
.run();
}
44 changes: 28 additions & 16 deletions src/resources.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
use bevy::prelude::*;
use nalgebra::Vector2;
use serde::{Deserialize, Serialize};

use crate::database;
use crate::direction::Direction;
use crate::level::PushState;
use crate::solver::solver::*;

use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;

#[derive(Resource)]
#[derive(Resource, Serialize, Deserialize)]
pub struct Settings {
pub instant_move: bool,
pub player_move_speed: f32,
pub solver: SolverSettings,
}

impl Default for Settings {
fn default() -> Self {
Self {
instant_move: false,
player_move_speed: 0.05,
solver: SolverSettings::default(),
}
}
}

#[derive(Serialize, Deserialize)]
pub struct SolverSettings {
pub strategy: Strategy,
pub lower_bound_method: LowerBoundMethod,
}

impl Default for SolverSettings {
fn default() -> Self {
Self {
strategy: Strategy::Fast,
lower_bound_method: LowerBoundMethod::PushCount,
}
}
}
Expand All @@ -27,6 +48,12 @@ pub struct Database(pub Mutex<database::Database>);
#[derive(Resource, Deref, DerefMut)]
pub struct LevelId(pub u64);

#[derive(Resource)]
pub struct PlayerMovement {
pub directions: VecDeque<Direction>,
pub timer: Timer,
}

#[derive(Resource)]
pub enum CrateReachable {
None,
Expand All @@ -41,18 +68,3 @@ impl Default for CrateReachable {
Self::None
}
}

#[derive(Resource)]
pub struct PlayerMovement {
pub directions: VecDeque<Direction>,
pub timer: Timer,
}

impl Default for PlayerMovement {
fn default() -> Self {
Self {
directions: VecDeque::new(),
timer: Timer::from_seconds(0.05, TimerMode::Repeating),
}
}
}
5 changes: 3 additions & 2 deletions src/solver/solver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use nalgebra::Vector2;
use serde::{Deserialize, Serialize};

use crate::direction::Direction;
use crate::level::{Level, Tile};
Expand All @@ -13,7 +14,7 @@ use std::time;

use std::io::Write;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum Strategy {
/// Find any solution
Fast,
Expand All @@ -28,7 +29,7 @@ pub enum Strategy {
Mixed,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum LowerBoundMethod {
PushCount,
MoveCount,
Expand Down
14 changes: 9 additions & 5 deletions src/systems/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Action {
pub fn player_move_to(
target: &Vector2<i32>,
board: &mut crate::board::Board,
player_movement: &mut ResMut<PlayerMovement>,
player_movement: &mut PlayerMovement,
) {
if let Some(path) = find_path(&board.level.player_position, target, |position| {
board
Expand All @@ -222,7 +222,7 @@ pub fn player_move_to(
fn player_move_or_push(
direction: Direction,
board: &mut crate::board::Board,
player_movement: &mut ResMut<PlayerMovement>,
player_movement: &mut PlayerMovement,
) {
if board.move_or_push(direction) {
player_movement.directions.push_front(direction);
Expand All @@ -244,9 +244,13 @@ fn print_solver_lowerbounds(solver: &Solver) {
}
}

fn solve_level(board: &mut crate::board::Board, player_movement: &mut ResMut<PlayerMovement>) {
fn solve_level(
board: &mut crate::board::Board,
player_movement: &mut PlayerMovement,
settings: &Settings,
) {
let mut solver = Solver::new(board.level.clone());
solver.initial(Strategy::Fast, LowerBoundMethod::PushCount);
solver.initial(settings.solver.strategy, settings.solver.lower_bound_method);
// print_solver_lowerbounds(&solver);

let timeout = std::time::Duration::from_secs(15);
Expand Down Expand Up @@ -364,7 +368,7 @@ pub fn action_input(
any_pressed = true;
}
if action_state.just_pressed(Action::AutomaticSolution) {
solve_level(board, &mut player_movement);
solve_level(board, &mut player_movement, &*settings);
any_pressed = true;
}

Expand Down

0 comments on commit 204b265

Please sign in to comment.