Add unified movement system

This commit is contained in:
Daniel Lynn 2021-07-07 12:05:45 -05:00
parent 1326400616
commit 8d595d8508
3 changed files with 26 additions and 18 deletions

View File

@ -14,3 +14,9 @@ pub struct Enemy;
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub struct MovingRandomly; pub struct MovingRandomly;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WantsToMove {
pub entity: Entity,
pub destination: Point,
}

View File

@ -2,6 +2,7 @@ mod collisions;
mod end_turn; mod end_turn;
mod entity_render; mod entity_render;
mod map_render; mod map_render;
mod movement;
mod player_input; mod player_input;
mod random_move; mod random_move;
@ -18,6 +19,8 @@ pub fn build_input_scheduler() -> Schedule {
pub fn build_player_scheduler() -> Schedule { pub fn build_player_scheduler() -> Schedule {
Schedule::builder() Schedule::builder()
.add_system(movement::movement_system())
.flush()
.add_system(collisions::collisions_system()) .add_system(collisions::collisions_system())
.flush() .flush()
.add_system(map_render::map_render_system()) .add_system(map_render::map_render_system())
@ -30,7 +33,7 @@ pub fn build_monster_scheduler() -> Schedule {
Schedule::builder() Schedule::builder()
.add_system(random_move::random_move_system()) .add_system(random_move::random_move_system())
.flush() .flush()
.add_system(collisions::collisions_system()) .add_system(movement::movement_system())
.flush() .flush()
.add_system(map_render::map_render_system()) .add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system()) .add_system(entity_render::entity_render_system())

View File

@ -1,36 +1,35 @@
use crate::prelude::*; use crate::prelude::*;
#[system] #[system]
#[write_component(Point)] #[read_component(Point)]
#[read_component(Player)] #[read_component(Player)]
pub fn player_input( pub fn player_input(
ecs: &mut SubWorld, ecs: &mut SubWorld,
#[resource] map: &Map, commands: &mut CommandBuffer,
#[resource] key: &Option<VirtualKeyCode>, #[resource] key: &Option<VirtualKeyCode>,
#[resource] camera: &mut Camera,
#[resource] turn_state: &mut TurnState, #[resource] turn_state: &mut TurnState,
) { ) {
if let Some(key) = key { let mut players = <(Entity, &Point)>::query().filter(component::<Player>());
if let Some(key) = *key {
let delta = match key { let delta = match key {
VirtualKeyCode::Left | VirtualKeyCode::A => Point::new(-1, 0), VirtualKeyCode::Left | VirtualKeyCode::A => Point::new(-1, 0),
VirtualKeyCode::Right | VirtualKeyCode::D => Point::new(1, 0), VirtualKeyCode::Right | VirtualKeyCode::D => Point::new(1, 0),
VirtualKeyCode::Up | VirtualKeyCode::W => Point::new(0, -1), VirtualKeyCode::Up | VirtualKeyCode::W => Point::new(0, -1),
VirtualKeyCode::Down | VirtualKeyCode::S => Point::new(0, 1), VirtualKeyCode::Down | VirtualKeyCode::S => Point::new(0, 1),
_ => Point::zero(), _ => Point::new(0, 0),
}; };
if delta.x != 0 || delta.y != 0 { players.iter_mut(ecs).for_each(|(entity, pos)| {
let mut players = <&mut Point>::query().filter(component::<Player>()); let destination = *pos + delta;
players.iter_mut(ecs).for_each(|pos| { commands.push((
let destination = *pos + delta; (),
WantsToMove {
if map.can_enter_tile(destination) { entity: *entity,
*pos = destination; destination,
camera.on_player_move(destination); },
*turn_state = TurnState::PlayerTurn; ));
} });
}); *turn_state = TurnState::PlayerTurn;
}
} }
} }