diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-10-23 15:01:10 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-10-23 15:01:10 -0400 |
| commit | bdede767f3e5208eb78103f5fbdb83ec1255f293 (patch) | |
| tree | 3c6ce828d8b1c8ddb02380a592a4210c9d3b97d9 /dungeon | |
| parent | graphics: refactor Assets, and add tile drawing! (diff) | |
| download | DungeonCrawl-bdede767f3e5208eb78103f5fbdb83ec1255f293.tar.gz DungeonCrawl-bdede767f3e5208eb78103f5fbdb83ec1255f293.tar.bz2 DungeonCrawl-bdede767f3e5208eb78103f5fbdb83ec1255f293.zip | |
dungon: use EnumIter from strum to add values methods to enums
Diffstat (limited to 'dungeon')
| -rw-r--r-- | dungeon/Cargo.toml | 2 | ||||
| -rw-r--r-- | dungeon/src/map.rs | 17 | ||||
| -rw-r--r-- | dungeon/src/pos.rs | 11 |
3 files changed, 23 insertions, 7 deletions
diff --git a/dungeon/Cargo.toml b/dungeon/Cargo.toml index 5548248..b50412b 100644 --- a/dungeon/Cargo.toml +++ b/dungeon/Cargo.toml @@ -5,6 +5,8 @@ edition = "2024" [dependencies] rand = "0.9" +strum = { version = "0.27", features = ["derive"] } +strum_macros = "0.27" [lints] workspace = true diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index a2cdd9a..fb42a77 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -1,6 +1,9 @@ //! The `map` module contains structures of the dungeon game map //! including the current `Floor`, and map `Tile`. +use strum::IntoEnumIterator; +use strum_macros::EnumIter; + use crate::wfc::Wfc; use std::{ cell::RefCell, @@ -20,7 +23,7 @@ pub const TILE_COUNT: usize = MAP_SIZE_USIZE * MAP_SIZE_USIZE; /// The `Tile` enum represents what is (or is not) at /// any given spot in the dungeon grid. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, EnumIter)] pub enum Tile { /// `Wall` represents an impassible wall Wall, @@ -29,13 +32,15 @@ pub enum Tile { /// `Stairs` represents stairs to another floor Stairs, } - impl Tile { /// Returns a list of all possible tiles - /// TODO! Use a crate for enum itterator - #[must_use] - pub fn all_tiles() -> Vec<Tile> { - vec![Tile::Wall, Tile::Air, Tile::Stairs] + pub fn values() -> impl Iterator<Item = Self> { + Self::iter() + } +} +impl Default for Tile { + fn default() -> Self { + Self::Air } } diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index dc1bd2d..27d9b8f 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -2,6 +2,9 @@ //! entity or objects position and facing direction inside the //! dungeon grid. +use strum::IntoEnumIterator; +use strum_macros::EnumIter; + use std::ops::{AddAssign, SubAssign}; use crate::{MAP_SIZE_USIZE, map::MAP_SIZE}; @@ -29,13 +32,19 @@ macro_rules! const_pos { /// or any position object is facing inside the dungeon map. /// Since the dungeon lives on a grid, there are only four /// possible directions. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, EnumIter)] pub enum Direction { North, South, East, West, } +impl Direction { + /// Returns an iterator over all possible directions + pub fn values() -> impl Iterator<Item = Self> { + Self::iter() + } +} /// The `Pos` type represents a 2D position inside the dungeon grid. /// |