diff options
Diffstat (limited to 'src/systems.rs')
| -rw-r--r-- | src/systems.rs | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/src/systems.rs b/src/systems.rs index d00abc8..4988434 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -2,6 +2,7 @@ use specs::{ ReadStorage, WriteStorage, + Read, Write, Entities, System, @@ -11,15 +12,31 @@ use specs::{ use super::components::{ Position, Visible, - Controller + Controller, + Blocking }; use super::controls::Control; -use super::resources::TopView; +use super::resources::{ + TopView, + Size, + Floor +}; -pub struct Draw; +pub struct MakeFloor; +impl <'a> System<'a> for MakeFloor { + type SystemData = (Entities<'a>, Write<'a, Floor>, ReadStorage<'a, Position>); + fn run(&mut self, (entities, mut floor, positions): Self::SystemData) { + floor.cells.clear(); + for (ent, pos) in (&entities, &positions).join() { + floor.cells.entry(*pos).or_insert(Vec::new()).push(ent); + } + } +} + +pub struct Draw; impl <'a> System<'a> for Draw { type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Visible>, Write<'a, TopView>); @@ -27,32 +44,30 @@ impl <'a> System<'a> for Draw { fn run(&mut self, (pos, vis, mut view): Self::SystemData) { view.cells.clear(); for (pos, vis) in (&pos, &vis).join(){ - if pos.x >= 0 && pos.y >= 0 && pos.x < view.width && pos.y < view.height { - view.cells.entry(*pos).or_insert(Vec::new()).push(vis.clone()); - view.cells.get_mut(pos).unwrap().sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); - } + view.cells.entry(*pos).or_insert(Vec::new()).push(vis.clone()); + view.cells.get_mut(pos).unwrap().sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap()); } } } -// struct Control; -// impl <'a> System <'a> for Control { -// type SystemData = WriteStorage<'a, Controller>; -// fn run (&mut self, mut controller: Self::SystemData) { -// for controller in &mut controller.join() -// } -// } - pub struct Move; impl <'a> System<'a> for Move { - type SystemData = (WriteStorage<'a, Controller>, WriteStorage<'a, Position>); - fn run(&mut self, (mut controller, mut pos): Self::SystemData) { - for (controller, pos) in (&mut controller, &mut pos).join(){ + type SystemData = (ReadStorage<'a, Controller>, WriteStorage<'a, Position>, Read<'a, Size>, ReadStorage<'a, Blocking>, Read<'a, Floor>); + fn run(&mut self, (controller, mut pos, size, blocking, floor): Self::SystemData) { + for (controller, pos) in (&controller, &mut pos).join(){ match &controller.0 { Control::Move(direction) => { - let (dx, dy) = direction.to_position(); - pos.x += dx; - pos.y += dy; + let newpos = (*pos + direction.to_position()).clamp(Position::new(0, 0), Position::new(size.width - 1, size.height - 1)); + let mut blocked = false; + for ent in floor.cells.get(&newpos).unwrap_or(&Vec::new()) { + if blocking.get(*ent).is_some(){ + blocked = true; + break; + } + } + if !blocked { + pos.clone_from(&newpos); + } } _ => {} } |
