summaryrefslogtreecommitdiff
path: root/dungeon/src/map.rs
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-07 18:34:12 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-07 18:34:12 -0500
commitdfee38af9faffb54822300836bcc55a406410426 (patch)
tree00ae700549cbfd8c0135c393d3315f301319b269 /dungeon/src/map.rs
parentgraphics: add direction based entity rendering (diff)
downloadDungeonCrawl-dfee38af9faffb54822300836bcc55a406410426.tar.gz
DungeonCrawl-dfee38af9faffb54822300836bcc55a406410426.tar.bz2
DungeonCrawl-dfee38af9faffb54822300836bcc55a406410426.zip
dungeon: fix placeholder zombie from spawning in walls
Diffstat (limited to 'dungeon/src/map.rs')
-rw-r--r--dungeon/src/map.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs
index f4a8729..6daa3c7 100644
--- a/dungeon/src/map.rs
+++ b/dungeon/src/map.rs
@@ -1,6 +1,7 @@
//! The `map` module contains structures of the dungeon game map
//! including the current `Floor`, and map `Tile`.
+use rand::{Rng, SeedableRng, rngs::StdRng};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
@@ -64,16 +65,20 @@ pub struct Floor {
hash: RefCell<u64>,
/// If the tiles are dirty (hash needs to be recomputed)
dirty: RefCell<bool>,
+ /// Custom rng
+ rng: StdRng,
}
impl Floor {
/// Internal constructor for `Floor`
fn new(tiles: Box<[Tile; TILE_COUNT]>, player_start: Pos, seed: u64) -> Self {
+ let rng = rand::rngs::StdRng::seed_from_u64(seed);
Self {
tiles,
player_start,
seed,
hash: RefCell::new(0),
dirty: RefCell::new(true),
+ rng,
}
}
@@ -210,6 +215,21 @@ impl Floor {
}
output
}
+
+ /// Returns a random open (no wall) position
+ pub fn random_pos(&mut self) -> Pos {
+ loop {
+ let x = self.rng.random_range(0..MAP_SIZE);
+ let y = self.rng.random_range(0..MAP_SIZE);
+ let Some(pos) = Pos::new(x, y) else {
+ continue;
+ };
+ if self.get(pos) != Tile::Air {
+ continue;
+ }
+ break pos;
+ }
+ }
}
impl Default for Floor {