summaryrefslogtreecommitdiff
path: root/src/location.rs
blob: a6ac623449a2b6c8fcade2462c01a219fbadf402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd)]
pub struct Location(pub i32, pub i32);

impl Location {
    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()
    }
}