diff options
| author | troido <troido@protonmail.com> | 2020-02-03 16:13:58 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-03 16:13:58 +0100 |
| commit | f0153eefd580ec443b380504303620a61f24630b (patch) | |
| tree | 98fe9cb6a9039d50b180ea774396e2d162a41656 /src/systems/moving.rs | |
| parent | 4bb710c6c6df8a24a2efa8033ad3c17663345dbd (diff) | |
refactored systems in their own file each
Diffstat (limited to 'src/systems/moving.rs')
| -rw-r--r-- | src/systems/moving.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/systems/moving.rs b/src/systems/moving.rs new file mode 100644 index 0000000..c81a766 --- /dev/null +++ b/src/systems/moving.rs @@ -0,0 +1,50 @@ + +use specs::{ + ReadStorage, + WriteStorage, + Read, + System, + Join +}; + +use super::super::components::{ + Position, + Controller, + Blocking +}; + +use super::super::controls::{ + Control +}; + +use super::super::resources::{ + Size, + Floor +}; + + + +pub struct Move; +impl <'a> System<'a> for Move { + 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 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); + } + } + _ => {} + } + } + } +} |
