summaryrefslogtreecommitdiff
path: root/src/pos.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/pos.rs')
-rw-r--r--src/pos.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/pos.rs b/src/pos.rs
index b6e54ad..d95c6f7 100644
--- a/src/pos.rs
+++ b/src/pos.rs
@@ -1,6 +1,6 @@
-use std::ops::Add;
+use std::ops::{Add, Sub};
use serde_json::Value;
use serde::{Serialize, Serializer, ser::SerializeTuple};
use crate::util::clamp;
@@ -31,6 +31,11 @@ impl Pos {
y: val.get(1)?.as_i64()?
})
}
+
+ pub fn distance_to(&self, other: Pos) -> i64 {
+ let d = other - *self;
+ d.x.abs() + d.y.abs()
+ }
}
@@ -57,3 +62,14 @@ impl Add<Pos> for Pos {
}
}
+impl Sub<Pos> for Pos {
+ type Output = Pos;
+
+ fn sub(self, other: Pos) -> Pos {
+ Pos {
+ x: self.x - other.x,
+ y: self.y - other.y
+ }
+ }
+}
+