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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::{constants::Direction, objects::HasPosition};
use super::Position;
impl Position {
pub fn get_direction_to<T>(self, target: &T) -> Option<Direction>
where
T: ?Sized + HasPosition,
{
let (dx, dy) = self - target.pos();
if dx.abs() > dy.abs() * 2 {
if dx > 0 {
Some(Direction::Right)
} else {
Some(Direction::Left)
}
} else if dy.abs() > dx.abs() * 2 {
if dy > 0 {
Some(Direction::Bottom)
} else {
Some(Direction::Top)
}
} else {
if dx > 0 && dy > 0 {
Some(Direction::BottomRight)
} else if dx > 0 && dy < 0 {
Some(Direction::TopRight)
} else if dx < 0 && dy > 0 {
Some(Direction::BottomLeft)
} else if dx < 0 && dy < 0 {
Some(Direction::TopLeft)
} else {
None
}
}
}
#[inline]
pub fn get_range_to<T>(self, target: &T) -> u32
where
T: ?Sized + HasPosition,
{
let (dx, dy) = self - target.pos();
dx.abs().max(dy.abs()) as u32
}
#[inline]
pub fn in_range_to<T>(self, target: &T, range: u32) -> bool
where
T: ?Sized + HasPosition,
{
self.get_range_to(target) < range
}
#[inline]
pub fn is_equal_to<T>(self, target: &T) -> bool
where
T: ?Sized + HasPosition,
{
self == target.pos()
}
#[inline]
pub fn is_near_to<T>(self, target: &T) -> bool
where
T: ?Sized + HasPosition,
{
let pos = target.pos();
self.room_name() == pos.room_name()
&& (self.x() as i32 - pos.x() as i32).abs() <= 1
&& (self.y() as i32 - pos.y() as i32).abs() <= 1
}
}