about summary refs log tree commit diff
path: root/src/types
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-07-13T13·07-0400
committerGriffin Smith <root@gws.fyi>2019-07-13T13·07-0400
commitbf03ebc549bfeef38b91110511a56a6cd24dc58d (patch)
tree6dc4d3766c25d06202af777b7213a5d4dde7feda /src/types
parent5af2429ecb5742383cf0798ce23682d316bdb24d (diff)
Add placeholder for game ticks
When the character moves, the game ticks forward equal to the
character's speed
Diffstat (limited to 'src/types')
-rw-r--r--src/types/entity_map.rs19
-rw-r--r--src/types/mod.rs32
2 files changed, 47 insertions, 4 deletions
diff --git a/src/types/entity_map.rs b/src/types/entity_map.rs
index 2d9f033a79d4..1846686d1164 100644
--- a/src/types/entity_map.rs
+++ b/src/types/entity_map.rs
@@ -73,8 +73,9 @@ impl<A> EntityMap<A> {
     /// Remove all entities at the given position
     pub fn remove_all_at(&mut self, pos: Position) {
         self.by_position.remove(&pos).map(|eids| {
-            eids.iter()
-                .map(|eid| self.by_id.remove(&eid).expect(BY_POS_INVARIANT));
+            for eid in eids {
+                self.by_id.remove(&eid).expect(BY_POS_INVARIANT);
+            }
         });
     }
 
@@ -227,16 +228,26 @@ mod tests {
             em.update_position(entity_id, new_position);
 
             if new_position != original_position {
-                assert_eq!(em.at(original_position).len(), 0);
+                assert!(em.at(original_position).iter().all(|e| e.name != ent.name));
             }
             assert_eq!(
                 em.get(entity_id).map(|e| e.position()),
                 Some(new_position)
             );
             assert_eq!(
-                em.at(new_position).iter().map(|e| e.name.clone()).collect::<Vec<_>>(),
+                em.at(new_position).iter().map(
+                    |e| e.name.clone()).collect::<Vec<_>>(),
                 vec![ent.name]
             )
         }
+
+        #[test]
+        fn test_remove_all_at(
+            mut em in gen_entity_map(),
+            pos: Position,
+        ) {
+            em.remove_all_at(pos);
+            assert_eq!(em.at(pos).len(), 0);
+        }
     }
 }
diff --git a/src/types/mod.rs b/src/types/mod.rs
index c0375a382fe2..67c773fdb107 100644
--- a/src/types/mod.rs
+++ b/src/types/mod.rs
@@ -1,3 +1,4 @@
+use std::cmp::max;
 use std::cmp::Ordering;
 use std::ops;
 use std::rc::Rc;
@@ -136,6 +137,13 @@ impl Position {
         // + 1 because Goto is 1-based, but position is 0-based
         cursor::Goto(self.x as u16 + 1, self.y as u16 + 1)
     }
+
+    /// Converts this position to the number of `Tiles` away from the origin it
+    /// represents. Usually done after subtracting two positions. Gives distance
+    /// as the crow flies
+    pub fn as_tiles(&self) -> Tiles {
+        Tiles(max(self.x.abs(), self.y.abs()).into())
+    }
 }
 
 impl PartialOrd for Position {
@@ -340,9 +348,17 @@ pub struct Tiles(pub f32);
 pub struct Speed(pub u32);
 
 impl Speed {
+    /// Returns the number of tiles that would be moved in the given number of
+    /// ticks at this speed
     pub fn ticks_to_tiles(self, ticks: Ticks) -> Tiles {
         Tiles(ticks.0 as f32 / self.0 as f32)
     }
+
+    /// Returns the number of ticks required to move the given number of tiles
+    /// at this speed
+    pub fn tiles_to_ticks(self, tiles: Tiles) -> Ticks {
+        Ticks(tiles.0 as u16 * self.0 as u16)
+    }
 }
 
 #[cfg(test)]
@@ -392,5 +408,21 @@ mod tests {
                 assert!(!(a < b))
             }
         }
+
+        #[test]
+        fn test_position_plus_dimension_as_tiles_monoid_action(
+            pos: Position,
+            dir: Direction,
+        ) {
+            prop_assume!(pos.y > 0 && pos.x > 0);
+            assert_eq!(((pos + dir) - pos).as_tiles(), Tiles(1.0));
+        }
+    }
+
+    #[test]
+    fn test_position_as_tiles() {
+        assert_eq!(pos(0, 0).as_tiles(), Tiles(0.0));
+        assert_eq!(pos(1, 1).as_tiles(), Tiles(1.0));
+        assert_eq!(pos(1, 2).as_tiles(), Tiles(2.0));
     }
 }