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)]
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 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())

View File

@ -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<VirtualKeyCode>,
#[resource] camera: &mut Camera,
#[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 {
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::<Player>());
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;
}
}