summaryrefslogtreecommitdiff
path: root/src/location.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/location.rs')
-rw-r--r--src/location.rs56
1 files changed, 20 insertions, 36 deletions
diff --git a/src/location.rs b/src/location.rs
index eb8110c..ca40700 100644
--- a/src/location.rs
+++ b/src/location.rs
@@ -1,40 +1,24 @@
-use std::cmp;
-
-pub struct Location {
- pub x : i32,
- pub y : i32
-}
-
-impl Copy for Location {}
-impl Clone for Location {
- fn clone(&self) -> Location {
- *self
- }
-}
-
-impl cmp::PartialEq for Location {
- fn eq(&self, rhs : &Location) -> bool {
- if self.x == rhs.x && self.y == rhs.y {
- true
- }
- else {
- false
- }
- }
-}
+#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd)]
+pub struct Location(pub i32, pub i32);
impl Location {
- pub fn get_around(self) -> Vec<Location> {
- let mut around = Vec::new();
- around.push(Location { x : self.x, y : self.y });
- around.push(Location { x : self.x+1, y : self.y });
- around.push(Location { x : self.x-1, y : self.y });
- around.push(Location { x : self.x, y : self.y+1 });
- around.push(Location { x : self.x, y : self.y-1 });
- around.push(Location { x : self.x+1, y : self.y+1 });
- around.push(Location { x : self.x-1, y : self.y-1 });
- around.push(Location { x : self.x+1, y : self.y-1 });
- around.push(Location { x : self.x-1, y : self.y+1 });
- around
+ pub fn distance(&self, other: &Location) -> usize {
+ (((self.0 - other.0).pow(2) + (self.1 - other.1).pow(2)) as f64).sqrt() as usize
+ }
+ pub fn neighbours(self, impassable : Vec<(Location, usize)>) -> Vec<(Location, usize)> {
+ let mut nearby = vec![Location(self.0 + 1, self.1), Location(self.0 - 1, self.1),
+ Location(self.0, self.1 + 1), Location(self.0, self.1 - 1),
+ Location(self.0 + 1, self.1 + 1), Location(self.0 - 1, self.1 - 1),
+ Location(self.0 + 1, self.1 - 1), Location(self.0 - 1, self.1 + 1)];
+ nearby.retain(|potential_location| {
+ let mut keep = true;
+ for location in impassable.iter() {
+ if *potential_location == location.0 {
+ keep = false;
+ }
+ }
+ keep
+ });
+ nearby.into_iter().map(|p| (p, 1)).collect()
}
}