From dfee38af9faffb54822300836bcc55a406410426 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 7 Nov 2025 18:34:12 -0500 Subject: dungeon: fix placeholder zombie from spawning in walls --- dungeon/src/lib.rs | 8 ++------ dungeon/src/map.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'dungeon/src') diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index cb57bf0..e85b718 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -65,17 +65,13 @@ impl Default for Dungeon { } } impl From for Dungeon { - fn from(floor: Floor) -> Self { + fn from(mut floor: Floor) -> Self { let player = Player::new(floor.player_start()); // TODO: initalize rest of game state // TODO: Randomize enemy positions/types - let enemies = vec![Enemy::new( - EnemyType::Zombie, - Pos::new(player.entity.pos.x() + 4, player.entity.pos.y()) - .unwrap_or(const_pos!(1, 1)), - )]; + let enemies = vec![Enemy::new(EnemyType::Zombie, floor.random_pos())]; Self { floor, 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, /// If the tiles are dirty (hash needs to be recomputed) dirty: RefCell, + /// 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 { -- cgit v1.2.3-freya