diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-10-08 19:24:16 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-10-08 19:37:16 -0400 |
| commit | fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a (patch) | |
| tree | 6fa99679e697828347af1e15d41cbd2ebb2106e3 /dungeon/src | |
| parent | add some functionality to Pos (diff) | |
| download | DungeonCrawl-fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a.tar.gz DungeonCrawl-fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a.tar.bz2 DungeonCrawl-fd038ef32f51ff2b19cc2331a9d6f319cd55eb5a.zip | |
further improve const fns for Pos
Diffstat (limited to 'dungeon/src')
| -rw-r--r-- | dungeon/src/map.rs | 7 | ||||
| -rw-r--r-- | dungeon/src/pos.rs | 23 |
2 files changed, 20 insertions, 10 deletions
diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index bb83ba9..f5c1184 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -1,7 +1,10 @@ //! The `map` module contains structures of the dungeon game map //! including the current `Floor`, map `Tile`, and `Entity`. -use crate::pos::{Direction, Pos}; +use crate::{ + const_pos, + pos::{Direction, Pos}, +}; /// `MAP_SIZE` is the size of the size of the dungeon grid. pub const MAP_SIZE: u16 = 100; @@ -193,7 +196,7 @@ impl Floor { impl Default for Floor { /// Returns a floor with a single set of walls on the map border fn default() -> Self { - const PLAYER_START: Pos = Pos::new_const::<1, 1>(); + const_pos!(PLAYER_START, 1, 1); let mut tiles = Box::new([Tile::Air; TILE_COUNT]); let seed = 0u64; diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 1d4526b..8695d0b 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -15,6 +15,13 @@ macro_rules! downcast { }; } +#[macro_export] +macro_rules! const_pos { + ($name:ident, $x:expr, $y:expr) => { + const $name: Pos = Pos::new_unchecked($x, $y); + }; +} + /// The `Direction` type represents a direction an entity /// or any position object is facing inside the dungeon map. /// Since the dungeon lives on a grid, there are only four @@ -62,23 +69,23 @@ impl Pos { } } - /// Creates a new position from a given x and y position at compile time. + /// Creates a new position from a given x and y position. /// - /// Bounds checks are run at compile time so it is gurenteeed to return a valid `Pos`. + /// Bounds checks are asserted at runtime and will panic if out of bounds. /// /// # Examples /// /// ``` /// use dungeon::Pos; /// - /// let pos = Pos::new_const::<1,1>(); + /// let pos = Pos::new_unchecked(1, 1); /// assert_eq!(pos.xy(), (1,1)); /// ``` #[must_use] - pub const fn new_const<const X: u16, const Y: u16>() -> Self { - assert!(X < MAP_SIZE); - assert!(Y < MAP_SIZE); - Self(X, Y) + pub const fn new_unchecked(x: u16, y: u16) -> Self { + assert!(x < MAP_SIZE, "Positions must be smaller then MAP_SIZE"); + assert!(y < MAP_SIZE, "Positions must be smaller then MAP_SIZE"); + Self(x, y) } /// Returns the x and y positions of `Pos`. @@ -220,7 +227,7 @@ impl Default for Pos { /// ``` /// fn default() -> Self { - const DEFAULT: Pos = Pos::new_const::<0, 0>(); + const_pos!(DEFAULT, 0, 0); DEFAULT } } |