Skip to content

Commit

Permalink
refactor: refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Jan 21, 2024
1 parent f6b9546 commit 0ac0c41
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ impl Board {
return self.level.crate_positions == self.level.target_positions;
}

#[allow(dead_code)]
pub fn player_facing_direction(&self) -> Direction {
pub fn player_orientation(&self) -> Direction {
self.movements
.last()
.map(|movement| movement.direction)
Expand Down
8 changes: 6 additions & 2 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use siphasher::sip::SipHasher24;

use crate::direction::Direction;

use std::cmp;
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::{Hash, Hasher};
use std::path::Path;
Expand Down Expand Up @@ -125,6 +126,9 @@ impl Level {
return Err(ParseMapError::MismatchBetweenCratesAndTargets);
}

crate_positions.shrink_to_fit();
target_positions.shrink_to_fit();

let mut instance = Self {
data,
dimensions,
Expand Down Expand Up @@ -213,7 +217,7 @@ impl Level {
map_data.push(line.to_string());
}

map_dimensions.x = std::cmp::max(line.len() as i32, map_dimensions.x);
map_dimensions.x = cmp::max(line.len() as i32, map_dimensions.x);
map_dimensions.y += 1;
}

Expand Down Expand Up @@ -336,7 +340,7 @@ impl Level {
self.hash(&mut hasher);
let hash = hasher.finish();

min_hash = std::cmp::min(min_hash, hash);
min_hash = cmp::min(min_hash, hash);
}

for i in 1..=8 {
Expand Down
7 changes: 3 additions & 4 deletions src/solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use std::cell::OnceCell;
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::hash::Hash;
use std::time;

use std::io::Write;
use std::time::{Duration, Instant};

#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum Strategy {
Expand Down Expand Up @@ -80,9 +79,9 @@ impl Solver {
));
}

pub fn solve(&mut self, timeout: time::Duration) -> Result<Movements> {
pub fn solve(&mut self, timeout: Duration) -> Result<Movements> {
debug_assert!(!self.heap.is_empty());
let timer = std::time::Instant::now();
let timer = Instant::now();
while let Some(state) = self.heap.pop() {
self.visited.insert(state.normalized(&self));

Expand Down
2 changes: 1 addition & 1 deletion src/solver/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl State {
}

fn calculate_lower_bound(&self, solver: &Solver) -> usize {
// FIXME: panic on Microban #155
// FIXME: Panic on Microban #155
self.crate_positions
.iter()
.map(|crate_position| solver.lower_bounds()[&crate_position])
Expand Down
6 changes: 4 additions & 2 deletions src/systems/auto_solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::solver::solver::*;
use crate::systems::input::*;
use crate::AppState;

use std::time::{Duration, Instant};

pub fn spawn_lowerbound_marks(
solver_state: Res<SolverState>,
mut commands: Commands,
Expand Down Expand Up @@ -98,8 +100,8 @@ pub fn update_solver(
*board = crate::board::Board::with_level(level.clone());

let mut solver = solver.lock().unwrap();
let timeout = std::time::Duration::from_millis(50);
let timer = std::time::Instant::now();
let timeout = Duration::from_millis(50);
let timer = Instant::now();
match solver.solve(timeout) {
Ok(solution) => {
let mut verify_board = board.clone();
Expand Down
3 changes: 1 addition & 2 deletions src/systems/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ pub fn mouse_input(
&& board.level.crate_positions.contains(&grid_position)
{
// auto_crate_push_state.selected_crate = grid_position;
// FIXME: https://github.com/bevyengine/bevy/issues/9130
// Re-entering AppState::AutoCratePush
// FIXME: Re-entering AppState::AutoCratePush https://github.com/bevyengine/bevy/issues/9130
// next_state.set(AppState::AutoCratePush);
next_state.set(AppState::Main);
return;
Expand Down
3 changes: 2 additions & 1 deletion src/systems/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::level::Level;
use crate::level::Tile;
use crate::resources::*;

use std::cmp;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -71,7 +72,7 @@ pub fn spawn_board(
let (mut transform, mut main_camera) = camera.single_mut();
transform.translation.x = (board_size.x - tile_size.x) / 2.0;
transform.translation.y = (board_size.y + tile_size.y) / 2.0;
main_camera.target_scale = 0.2 * (std::cmp::max(level.dimensions.x, level.dimensions.y) as f32);
main_camera.target_scale = 0.2 * (cmp::max(level.dimensions.x, level.dimensions.y) as f32);

// despawn the previous `Board`
commands.entity(board.single()).despawn_recursive();
Expand Down
27 changes: 13 additions & 14 deletions src/systems/render.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use benimator::{Animation, FrameRate};
use bevy::prelude::*;
use bevy::winit::WinitWindows;

Expand All @@ -7,6 +8,7 @@ use crate::events::*;
use crate::resources::*;

use std::collections::HashSet;
use std::time::Duration;

pub fn setup_window(mut window: Query<&mut Window>, winit_windows: NonSend<WinitWindows>) {
let mut window = window.single_mut();
Expand Down Expand Up @@ -42,31 +44,28 @@ pub fn animate_player(
let (animation_state, sprite) = &mut player.single_mut();

// TODO: 仅进行推动撤回时角色的朝向正确, 但看上去有点怪. 支持操作撤回应该可以解决该问题
let direction = board.player_facing_direction();
let player_orientation = board.player_orientation();

let face_up = benimator::Animation::from_indices([55], benimator::FrameRate::from_fps(1.0));
let face_down = benimator::Animation::from_indices([52], benimator::FrameRate::from_fps(1.0));
let face_left = benimator::Animation::from_indices([81], benimator::FrameRate::from_fps(1.0));
let face_right = benimator::Animation::from_indices([78], benimator::FrameRate::from_fps(1.0));
let face_up = Animation::from_indices([55], FrameRate::from_frame_duration(Duration::MAX));
let face_down = Animation::from_indices([52], FrameRate::from_frame_duration(Duration::MAX));
let face_left = Animation::from_indices([81], FrameRate::from_frame_duration(Duration::MAX));
let face_right = Animation::from_indices([78], FrameRate::from_frame_duration(Duration::MAX));

let move_up = benimator::Animation::from_indices(55..=57, benimator::FrameRate::from_fps(6.0));
let move_down =
benimator::Animation::from_indices(52..=54, benimator::FrameRate::from_fps(6.0));
let move_left =
benimator::Animation::from_indices(81..=83, benimator::FrameRate::from_fps(6.0));
let move_right =
benimator::Animation::from_indices(78..=80, benimator::FrameRate::from_fps(6.0));
let move_up = Animation::from_indices(55..=57, FrameRate::from_fps(6.0));
let move_down = Animation::from_indices(52..=54, FrameRate::from_fps(6.0));
let move_left = Animation::from_indices(81..=83, FrameRate::from_fps(6.0));
let move_right = Animation::from_indices(78..=80, FrameRate::from_fps(6.0));

let animation;
if player_movement.directions.is_empty() {
animation = match direction {
animation = match player_orientation {
Direction::Up => face_up,
Direction::Right => face_right,
Direction::Down => face_down,
Direction::Left => face_left,
};
} else {
animation = match direction {
animation = match player_orientation {
Direction::Up => move_up,
Direction::Right => move_right,
Direction::Down => move_down,
Expand Down
7 changes: 4 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod tests {
use std::fs;
use std::ops::RangeBounds;
use std::path::Path;
use std::time::Duration;

// use test::Bencher;

Expand All @@ -33,7 +34,7 @@ mod tests {
let level = levels[id].clone();
let mut solver = Solver::new(level.clone());
solver.initial(Strategy::Fast, LowerBoundMethod::PushCount);
let solution = solver.solve(std::time::Duration::from_secs(time_limit));
let solution = solver.solve(Duration::from_secs(time_limit));
if solution.is_err() {
println!("{}", level.export_map());
println!("{:?}\n\n", solution.clone().err());
Expand All @@ -60,8 +61,8 @@ mod tests {
&levels,
0..155,
&[
87, 92, 96, 97, 98, 107, 108, 110, 113, 116, 121, 122, 137, 138, 142, 144, 145,
149, 150, 152, 154
35, 87, 92, 96, 97, 98, 107, 108, 110, 113, 116, 121, 122, 137, 138, 142, 144,
145, 149, 150, 152, 154
],
15 * 2
) == 0
Expand Down

0 comments on commit 0ac0c41

Please sign in to comment.