Skip to main content

asterion_core/
lib.rs

1//! Pure game logic for asterion - data and rules with no I/O, UI, or SSH.
2
3mod alarm_level;
4mod direction;
5mod entity;
6mod game;
7mod hero;
8mod maze;
9mod minotaur;
10mod power_up;
11mod utils;
12mod view;
13
14pub use alarm_level::AlarmLevel;
15pub use direction::Direction;
16pub use entity::Entity;
17pub use game::{Game, MAX_MAZE_ID, POWER_UPS_PER_ROOM};
18pub use hero::{GameCommand, Hero};
19pub use maze::Maze;
20pub use minotaur::Minotaur;
21pub use power_up::PowerUp;
22pub use utils::{GameColors, PlayerId, MAX_USERNAME_LEN};
23pub use view::View;
24
25pub type Position = (usize, usize);
26
27pub trait IntoDirection {
28    fn into_direction(self, direction: &Direction) -> Self;
29    fn distance(&self, other: Position) -> f64;
30    fn distance_squared(&self, other: Position) -> usize;
31}
32
33impl IntoDirection for Position {
34    fn into_direction(self, direction: &Direction) -> Self {
35        let (x, y) = self;
36        let (new_x, new_y) = match direction {
37            Direction::North => (x, y.saturating_sub(1)),
38            Direction::South => (x, y + 1),
39            Direction::West => (x.saturating_sub(1), y),
40            Direction::East => (x + 1, y),
41            _ => (x, y),
42        };
43
44        (new_x, new_y)
45    }
46
47    fn distance(&self, other: Position) -> f64 {
48        (self.distance_squared(other) as f64).sqrt()
49    }
50
51    fn distance_squared(&self, other: Position) -> usize {
52        ((self.0 as isize - other.0 as isize).pow(2) + (self.1 as isize - other.1 as isize).pow(2))
53            as usize
54    }
55}