Browse Source

Some modifications to carpet library

Getty Ritter 4 years ago
parent
commit
abd06ef07b
1 changed files with 19 additions and 9 deletions
  1. 19 9
      carpet/src/lib.rs

+ 19 - 9
carpet/src/lib.rs

@@ -8,7 +8,7 @@ const TILE_SIZE: f32 = 1.0 / 16.0;
 
 pub struct Board {
     size: Size,
-    contents: Vec<SpriteIdx>,
+    contents: Vec<(u8, SpriteIdx)>,
     tileset: Tileset,
 }
 
@@ -68,7 +68,7 @@ impl Board {
                     .src(Board::sprite_location(0u8).into())
                     .dest(tileset.to_screen([x, y]));
                 let idx = tileset.batch.add(param);
-                contents.push(idx);
+                contents.push((0u8, idx));
             }
         }
         Board { size, contents, tileset }
@@ -90,7 +90,13 @@ impl Board {
         let param = DrawParam::new()
             .src(Board::sprite_location(ch).into())
             .dest(self.tileset.to_screen(at));
-        self.tileset.batch.set(self.contents[idx], param).unwrap();
+        self.tileset.batch.set(self.contents[idx].1, param).unwrap();
+    }
+
+    pub fn get(&mut self, at: impl Into<Coord>) -> u8 {
+        let at = at.into();
+        let idx = at.x + at.y * self.size.width;
+        self.contents[idx].0
     }
 
     pub fn print(&mut self, at: impl Into<Coord>, msg: &str) {
@@ -99,12 +105,16 @@ impl Board {
             self.set([idx + x, y], ch as u8);
         }
     }
-}
 
-#[cfg(test)]
-mod tests {
-    #[test]
-    fn it_works() {
-        assert_eq!(2 + 2, 4);
+    pub fn clear(&mut self) {
+        for (n, contents) in self.contents.iter_mut().enumerate() {
+            let x = n % self.size.width;
+            let y = n / self.size.width;
+            let param = DrawParam::new()
+                .src(Board::sprite_location(0).into())
+                .dest(self.tileset.to_screen([x, y]));
+            contents.0 = 0;
+            self.tileset.batch.set(contents.1, param).unwrap();
+        }
     }
 }