From afc11ca549c45ddc8c3f724b14a1a50ef2761d99 Mon Sep 17 00:00:00 2001 From: Frankie Fisher Date: Sun, 7 Apr 2024 22:05:33 +0100 Subject: [PATCH] feat: Position.dir() returns the position in a Direction refactor: de-invent the wheel and use built-in abs_diff() --- src/game/pathfinder.rs | 51 ++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/src/game/pathfinder.rs b/src/game/pathfinder.rs index 0d1ba89..cc299d4 100644 --- a/src/game/pathfinder.rs +++ b/src/game/pathfinder.rs @@ -1,3 +1,4 @@ +use crate::constants::Direction; use js_sys::{Array, Object}; use serde::{Deserialize, Serialize}; use std::fmt; @@ -17,18 +18,44 @@ impl fmt::Display for Position { impl Position { pub fn range_to(&self, pos: &Position) -> u8 { - let diff_x = if self.x > pos.x { - self.x - pos.x - } else { - pos.x - self.x - }; - let diff_y = if self.y > pos.y { - self.y - pos.y - } else { - pos.y - self.y - }; - - std::cmp::max(diff_x, diff_y) + std::cmp::max(self.x.abs_diff(pos.x), self.y.abs_diff(pos.y)) + } + + pub fn dir(&self, dir: Direction) -> Position { + match dir { + Direction::Top => Position { + x: self.x, + y: self.y - 1, + }, + Direction::TopRight => Position { + x: self.x + 1, + y: self.y - 1, + }, + Direction::Right => Position { + x: self.x + 1, + y: self.y, + }, + Direction::BottomRight => Position { + x: self.x + 1, + y: self.y + 1, + }, + Direction::Bottom => Position { + x: self.x, + y: self.y + 1, + }, + Direction::BottomLeft => Position { + x: self.x - 1, + y: self.y + 1, + }, + Direction::Left => Position { + x: self.x - 1, + y: self.y, + }, + Direction::TopLeft => Position { + x: self.x - 1, + y: self.y - 1, + }, + } } }