36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
use crate::prelude::*;
|
|
|
|
#[system]
|
|
#[read_component(Point)]
|
|
#[read_component(Player)]
|
|
pub fn player_input(
|
|
ecs: &mut SubWorld,
|
|
commands: &mut CommandBuffer,
|
|
#[resource] key: &Option<VirtualKeyCode>,
|
|
#[resource] turn_state: &mut TurnState,
|
|
) {
|
|
let mut players = <(Entity, &Point)>::query().filter(component::<Player>());
|
|
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::new(0, 0),
|
|
};
|
|
|
|
players.iter_mut(ecs).for_each(|(entity, pos)| {
|
|
let destination = *pos + delta;
|
|
|
|
commands.push((
|
|
(),
|
|
WantsToMove {
|
|
entity: *entity,
|
|
destination,
|
|
},
|
|
));
|
|
});
|
|
*turn_state = TurnState::PlayerTurn;
|
|
}
|
|
}
|