From 72ea9deb1cf959602a038e5141a86228186a35b3 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Thu, 23 Nov 2017 02:58:34 -0600 Subject: -added pathfinding --- src/location.rs | 56 ++++++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 36 deletions(-) (limited to 'src/location.rs') 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 { - 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() } } -- cgit v1.2.3