summaryrefslogtreecommitdiff
path: root/src/systems/moving.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-03 20:16:09 +0100
committertroido <troido@protonmail.com>2020-02-03 20:16:09 +0100
commit53f358f73c37e86f4db9e7bd7af309697dc7237e (patch)
treed118cdfb10e1519995a92ac0c58db1051c41325d /src/systems/moving.rs
parent30509956d4274b2565052dc045eec3742a159357 (diff)
only send changed cells, not the whole field each time
Diffstat (limited to 'src/systems/moving.rs')
-rw-r--r--src/systems/moving.rs15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/systems/moving.rs b/src/systems/moving.rs
index 38588d5..2ea0650 100644
--- a/src/systems/moving.rs
+++ b/src/systems/moving.rs
@@ -11,7 +11,8 @@ use super::super::pos::Pos;
use super::super::components::{
Controller,
- Blocking
+ Blocking,
+ Position
};
use super::super::controls::{
@@ -27,12 +28,12 @@ use super::super::resources::{
pub struct Move;
impl <'a> System<'a> for Move {
- type SystemData = (ReadStorage<'a, Controller>, WriteStorage<'a, Pos>, 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(){
+ type SystemData = (ReadStorage<'a, Controller>, WriteStorage<'a, Position>, Read<'a, Size>, ReadStorage<'a, Blocking>, Read<'a, Floor>);
+ fn run(&mut self, (controllers, mut positions, size, blocking, floor): Self::SystemData) {
+ for (controller, mut pos) in (&controllers, &mut positions.restrict_mut()).join(){
match &controller.0 {
Control::Move(direction) => {
- let newpos = (*pos + direction.to_position()).clamp(Pos::new(0, 0), Pos::new(size.width - 1, size.height - 1));
+ 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;
for ent in floor.cells.get(&newpos).unwrap_or(&Vec::new()) {
if blocking.get(*ent).is_some(){
@@ -41,7 +42,9 @@ impl <'a> System<'a> for Move {
}
}
if !blocked {
- pos.clone_from(&newpos);
+ let mut pos_mut = pos.get_mut_unchecked();
+ pos_mut.prev = Some(pos_mut.pos);
+ pos_mut.pos = newpos.clone();
}
}
_ => {}