diff --git a/src/components.rs b/src/components.rs index 80d56d6..4834511 100644 --- a/src/components.rs +++ b/src/components.rs @@ -14,3 +14,9 @@ pub struct Enemy; #[derive(Clone, Copy, Debug, PartialEq)] pub struct MovingRandomly; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct WantsToMove { + pub entity: Entity, + pub destination: Point, +} diff --git a/src/systems/mod.rs b/src/systems/mod.rs index b9555b9..8ae8419 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -2,6 +2,7 @@ mod collisions; mod end_turn; mod entity_render; mod map_render; +mod movement; mod player_input; mod random_move; @@ -18,6 +19,8 @@ pub fn build_input_scheduler() -> Schedule { pub fn build_player_scheduler() -> Schedule { Schedule::builder() + .add_system(movement::movement_system()) + .flush() .add_system(collisions::collisions_system()) .flush() .add_system(map_render::map_render_system()) @@ -30,7 +33,7 @@ pub fn build_monster_scheduler() -> Schedule { Schedule::builder() .add_system(random_move::random_move_system()) .flush() - .add_system(collisions::collisions_system()) + .add_system(movement::movement_system()) .flush() .add_system(map_render::map_render_system()) .add_system(entity_render::entity_render_system()) diff --git a/src/systems/player_input.rs b/src/systems/player_input.rs index fb007a3..7aada28 100644 --- a/src/systems/player_input.rs +++ b/src/systems/player_input.rs @@ -1,36 +1,35 @@ use crate::prelude::*; #[system] -#[write_component(Point)] +#[read_component(Point)] #[read_component(Player)] pub fn player_input( ecs: &mut SubWorld, - #[resource] map: &Map, + commands: &mut CommandBuffer, #[resource] key: &Option, - #[resource] camera: &mut Camera, #[resource] turn_state: &mut TurnState, ) { - if let Some(key) = key { + let mut players = <(Entity, &Point)>::query().filter(component::()); + if let Some(key) = *key { let delta = match key { VirtualKeyCode::Left | VirtualKeyCode::A => Point::new(-1, 0), VirtualKeyCode::Right | VirtualKeyCode::D => Point::new(1, 0), VirtualKeyCode::Up | VirtualKeyCode::W => 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 { - let mut players = <&mut Point>::query().filter(component::()); + players.iter_mut(ecs).for_each(|(entity, pos)| { + let destination = *pos + delta; - players.iter_mut(ecs).for_each(|pos| { - let destination = *pos + delta; - - if map.can_enter_tile(destination) { - *pos = destination; - camera.on_player_move(destination); - *turn_state = TurnState::PlayerTurn; - } - }); - } + commands.push(( + (), + WantsToMove { + entity: *entity, + destination, + }, + )); + }); + *turn_state = TurnState::PlayerTurn; } }