Browse Source

Add rudimentary collision

Getty Ritter 4 years ago
parent
commit
f0f895e7b9
1 changed files with 11 additions and 2 deletions
  1. 11 2
      ch3/src/main.rs

+ 11 - 2
ch3/src/main.rs

@@ -40,6 +40,10 @@ impl Map {
         });
         Map { tiles }
     }
+
+    fn passable(&self, (x, y): (usize, usize)) -> bool {
+        Some(&TileType::Floor) == self.tiles.get(x, y)
+    }
 }
 
 #[derive(Component)]
@@ -96,11 +100,16 @@ system_impl! {
 
 system! {
     Move (
+        resource map: Map,
         mut motion: Motion,
         mut pos: Pos,
     ) {
-        pos.x = (pos.x as i8 + motion.right) as usize;
-        pos.y = (pos.y as i8 + motion.down) as usize;
+        let tgt_x = (pos.x as i8 + motion.right) as usize;
+        let tgt_y = (pos.y as i8 + motion.down) as usize;
+        if map.passable((tgt_x, tgt_y)) {
+            pos.x = tgt_x;
+            pos.y = tgt_y;
+        }
     } finally {
         motion.clear();
     }