Skip to content

Commit

Permalink
feat: Position.dir() returns the position in a Direction
Browse files Browse the repository at this point in the history
refactor: de-invent the wheel and use built-in abs_diff()
  • Loading branch information
wtfrank committed Apr 7, 2024
1 parent c2d084c commit afc11ca
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/game/pathfinder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::constants::Direction;
use js_sys::{Array, Object};
use serde::{Deserialize, Serialize};
use std::fmt;
Expand All @@ -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,
},
}
}
}

Expand Down

0 comments on commit afc11ca

Please sign in to comment.