From 27c0795fb70739ce5609a0f424d80491d4a8c5a1 Mon Sep 17 00:00:00 2001 From: troido Date: Mon, 2 Mar 2020 12:02:54 +0100 Subject: added monster ai --- src/pos.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/pos.rs') 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 for Pos { } } +impl Sub for Pos { + type Output = Pos; + + fn sub(self, other: Pos) -> Pos { + Pos { + x: self.x - other.x, + y: self.y - other.y + } + } +} + -- cgit