diff options
author | Tom Barrett <tombarrett@siu.edu> | 2017-11-23 02:58:34 -0600 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-11-23 02:58:34 -0600 |
commit | 72ea9deb1cf959602a038e5141a86228186a35b3 (patch) | |
tree | a2169071db31acf4c8810a6b6ee740fcf0ab3a0b /src/location.rs | |
parent | 1c3ec15a9a8b7cef3c544af225d028b3de13d75e (diff) |
-added pathfinding
Diffstat (limited to 'src/location.rs')
-rw-r--r-- | src/location.rs | 56 |
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() } } |