Skip to content

Commit

Permalink
feat: support player facing direction
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Jan 17, 2024
1 parent 0ce0ddf commit fca91b1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
15 changes: 7 additions & 8 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ impl Board {
}
}

pub fn move_or_push(&mut self, direction: Direction) -> bool {
pub fn move_or_push(&mut self, direction: Direction) {
let direction_vector = direction.to_vector();
let player_next_position = self.level.player_position + direction_vector;
if self
.level
.get_unchecked(&player_next_position)
.intersects(Tile::Wall)
{
return false;
return;
}
if self
.level
Expand All @@ -42,7 +42,7 @@ impl Board {
.get_unchecked(&crate_next_position)
.intersects(Tile::Wall | Tile::Crate)
{
return false;
return;
}
self.move_crate(player_next_position, crate_next_position);

Expand All @@ -52,7 +52,6 @@ impl Board {
}
self.move_player(player_next_position);
self.undone_movements.clear();
return true;
}

pub fn reset(&mut self) {
Expand All @@ -71,7 +70,7 @@ impl Board {
}
}

fn undo_move(&mut self) {
pub fn undo_move(&mut self) {
debug_assert!(!self.movements.is_empty());
let history = self.movements.pop().unwrap();
let direction = history.direction;
Expand All @@ -94,7 +93,7 @@ impl Board {
}
}

fn redo_move(&mut self) {
pub fn redo_move(&mut self) {
debug_assert!(!self.undone_movements.is_empty());
let history = self.undone_movements.pop().unwrap();
let undone_movements = self.undone_movements.clone();
Expand All @@ -106,11 +105,11 @@ impl Board {
return self.level.crate_positions == self.level.target_positions;
}

#[allow(dead_code)]
pub fn player_facing_direction(&self) -> Direction {
self.movements
.iter()
.map(|movement| movement.direction)
.last()
.map(|movement| movement.direction)
.unwrap_or(Direction::Down)
}

Expand Down
1 change: 1 addition & 0 deletions src/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Neg for Direction {
}

impl Direction {
/// Convert to 2D unit vector.
pub fn to_vector(&self) -> Vector2<i32> {
match self {
Direction::Up => -Vector2::<i32>::y(),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn main() {
.chain(),
),
)
.add_systems(FixedUpdate, animate_camera_zoom);
.add_systems(FixedUpdate, (animate_camera_zoom, animate_player));

app.add_systems(
Update,
Expand Down
1 change: 1 addition & 0 deletions src/solver/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl Solver {
}
}

#[allow(dead_code)]
fn print_info(visited: &HashSet<State>, heap: &BinaryHeap<State>, state: &State) {
print!(
"Visited: {:<6}, Heuristic: {:<4}, Moves: {:<4}, Pushes: {:<4}, Pressure: {:<4}\r",
Expand Down
32 changes: 20 additions & 12 deletions src/systems/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::prelude::*;
use bevy::winit::WinitWindows;

use crate::components::*;
use crate::direction::Direction;
use crate::events::*;
use crate::resources::*;

Expand Down Expand Up @@ -31,8 +32,25 @@ pub fn setup_camera(mut commands: Commands) {
commands.spawn((Camera2dBundle::default(), MainCamera::default()));
}

pub fn animate_player(
mut player: Query<&mut TextureAtlasSprite, With<Player>>,
mut board: Query<&mut Board>,
) {
let board = &mut board.single_mut().board;
let sprite = &mut player.single_mut();

// TODO: 仅进行推动撤回时角色的朝向正确, 但看上去有点怪. 支持操作撤回应该可以解决该问题
let direction = board.player_facing_direction();
**sprite = TextureAtlasSprite::new(match direction {
Direction::Up => 4 + 0,
Direction::Right => 4 + 1,
Direction::Down => 4 + 2,
Direction::Left => 4 + 3,
});
}

pub fn animate_player_movement(
mut player: Query<(&mut GridPosition, &mut TextureAtlasSprite), With<Player>>,
mut player: Query<&mut GridPosition, With<Player>>,
mut crates: Query<&mut GridPosition, (With<Crate>, Without<Player>)>,
mut board: Query<&mut Board>,
mut player_movement: ResMut<PlayerMovement>,
Expand All @@ -41,23 +59,13 @@ pub fn animate_player_movement(
) {
let board = &mut board.single_mut().board;

let (player_grid_position, sprite) = &mut player.single_mut();
let player_grid_position = &mut ***player_grid_position;
let player_grid_position = &mut **player.single_mut();
if !settings.instant_move {
player_movement.timer.tick(time.delta());
if !player_movement.timer.just_finished() {
return;
}
if let Some(direction) = player_movement.directions.pop_back() {
/*
**sprite = TextureAtlasSprite::new(match direction {
Direction::Up => 4 + 0,
Direction::Right => 4 + 1,
Direction::Down => 4 + 2,
Direction::Left => 4 + 3,
});
*/

board.move_or_push(direction);

player_grid_position.x += direction.to_vector().x;
Expand Down

0 comments on commit fca91b1

Please sign in to comment.