diff options
| author | troido <troido@protonmail.com> | 2020-02-08 18:50:58 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-08 18:50:58 +0100 |
| commit | 3ebe9e6f792a0457c6f3b37b6e9d92c83f8694e2 (patch) | |
| tree | aaa81d23a4816ebecacc64593419d3d811b7283b /src/systems/moving.rs | |
| parent | 7dfc7956a7c2df9a1df3ea0b32e0c3d2036fa3ce (diff) | |
don't rebuild the ground each step
Diffstat (limited to 'src/systems/moving.rs')
| -rw-r--r-- | src/systems/moving.rs | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/systems/moving.rs b/src/systems/moving.rs index 28c3d3f..35f73ea 100644 --- a/src/systems/moving.rs +++ b/src/systems/moving.rs @@ -1,5 +1,8 @@ +use std::collections::HashSet; + use specs::{ + Entities, ReadStorage, WriteStorage, Read, @@ -13,7 +16,8 @@ use super::super::components::{ Controller, Blocking, Position, - Floor + Floor, + Moved }; use super::super::controls::{ @@ -30,22 +34,33 @@ use super::super::resources::{ pub struct Move; impl <'a> System<'a> for Move { type SystemData = ( + Entities<'a>, ReadStorage<'a, Controller>, WriteStorage<'a, Position>, Read<'a, Size>, ReadStorage<'a, Blocking>, Read<'a, Ground>, ReadStorage<'a, Floor>, + WriteStorage<'a, Moved> ); - fn run(&mut self, (controllers, mut positions, size, blocking, ground, floor): Self::SystemData) { - for (controller, mut pos) in (&controllers, &mut positions.restrict_mut()).join(){ + fn run(&mut self, (entities, controllers, mut positions, size, blocking, ground, floor, mut moved): Self::SystemData) { + { + let mut ents = Vec::new(); + for (ent, _moved) in (&*entities, &moved).join() { + ents.push(ent); + } + for ent in ents { + moved.remove(ent); + } + } + for (ent, controller, mut pos) in (&entities, &controllers, &mut positions.restrict_mut()).join(){ match &controller.0 { Control::Move(direction) => { let newpos = (pos.get_unchecked().pos + direction.to_position()).clamp(Pos::new(0, 0), Pos::new(size.width - 1, size.height - 1)); let mut blocked = false; let mut on_floor = false; - for ent in ground.cells.get(&newpos).unwrap_or(&Vec::new()) { + for ent in ground.cells.get(&newpos).unwrap_or(&HashSet::new()) { if blocking.get(*ent).is_some(){ blocked = true; break; @@ -57,6 +72,7 @@ impl <'a> System<'a> for Move { if !blocked && on_floor { let mut pos_mut = pos.get_mut_unchecked(); pos_mut.prev = Some(pos_mut.pos); + moved.insert(ent, Moved{from: pos_mut.pos}).expect("can't insert Moved"); pos_mut.pos = newpos.clone(); } } |
