veloren_common/comp/
compass.rs

1use common_i18n::Content;
2use vek::Vec2;
3// TODO: Move this to common/src/, it's not a component
4
5/// Cardinal directions
6pub enum Direction {
7    North,
8    Northeast,
9    East,
10    Southeast,
11    South,
12    Southwest,
13    West,
14    Northwest,
15}
16
17impl Direction {
18    /// Convert a direction vector to a cardinal direction
19    /// Direction vector can be trivially calculated by doing (target - source)
20    pub fn from_dir(dir: Vec2<f32>) -> Self {
21        if let Some(dir) = dir.try_normalized() {
22            let mut angle = dir.angle_between(Vec2::unit_y()).to_degrees();
23            if dir.x < 0.0 {
24                angle = -angle;
25            }
26            match angle as i32 {
27                -360..=-157 => Direction::South,
28                -156..=-112 => Direction::Southwest,
29                -111..=-67 => Direction::West,
30                -66..=-22 => Direction::Northwest,
31                -21..=22 => Direction::North,
32                23..=67 => Direction::Northeast,
33                68..=112 => Direction::East,
34                113..=157 => Direction::Southeast,
35                158..=360 => Direction::South,
36                _ => Direction::North, // should never happen
37            }
38        } else {
39            Direction::North // default value, should never happen
40        }
41    }
42
43    // TODO: Remove this in favour of `Direction::localize_npc`?
44    pub fn name(&self) -> &'static str {
45        match self {
46            Direction::North => "North",
47            Direction::Northeast => "Northeast",
48            Direction::East => "East",
49            Direction::Southeast => "Southeast",
50            Direction::South => "South",
51            Direction::Southwest => "Southwest",
52            Direction::West => "West",
53            Direction::Northwest => "Northwest",
54        }
55    }
56
57    /// Should be only used with care with npc-tell_monster and npc-tell_site
58    ///
59    /// If you add new usages for it, please consult i18n team.
60    pub fn localize_npc(&self) -> Content {
61        Content::localized(match self {
62            Direction::North => "npc-speech-dir_north",
63            Direction::Northeast => "npc-speech-dir_north_east",
64            Direction::East => "npc-speech-dir_east",
65            Direction::Southeast => "npc-speech-dir_south_east",
66            Direction::South => "npc-speech-dir_south",
67            Direction::Southwest => "npc-speech-dir_south_west",
68            Direction::West => "npc-speech-dir_west",
69            Direction::Northwest => "npc-speech-dir_north_west",
70        })
71    }
72}
73
74/// Arbitrarily named Distances
75pub enum Distance {
76    VeryFar,
77    Far,
78    Ahead,
79    Near,
80    NextTo,
81}
82
83impl Distance {
84    /// Convert a length to a Distance
85    pub fn from_length(length: i32) -> Self {
86        match length {
87            0..=100 => Distance::NextTo,
88            101..=500 => Distance::Near,
89            501..=3000 => Distance::Ahead,
90            3001..=10000 => Distance::Far,
91            _ => Distance::VeryFar,
92        }
93    }
94
95    /// Convert a vector to a Distance
96    pub fn from_dir(dir: Vec2<f32>) -> Self { Self::from_length(dir.magnitude() as i32) }
97
98    // TODO: localization
99    pub fn name(&self) -> &'static str {
100        match self {
101            Distance::VeryFar => "very far",
102            Distance::Far => "far",
103            Distance::Ahead => "ahead",
104            Distance::Near => "near",
105            Distance::NextTo => "just around",
106        }
107    }
108
109    /// Should be only used with care with npc-tell_monster and npc-tell_site
110    ///
111    /// If you add new usages for it, please consult i18n team.
112    pub fn localize_npc(&self) -> Content {
113        Content::localized(match self {
114            Self::VeryFar => "npc-speech-dist_very_far",
115            Self::Far => "npc-speech-dist_far",
116            Self::Ahead => "npc-speech-dist_ahead",
117            Self::Near => "npc-speech-dist_near",
118            Self::NextTo => "npc-speech-dist_near_to",
119        })
120    }
121}