diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-14 09:51:12 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-14 09:51:12 -0500 |
| commit | 7b78c39cb604961564b1d4e0a491eafacc85e8cb (patch) | |
| tree | 8618f2a4fc76a389bcabdeb94ff46ab6d74b1d77 /dungeon | |
| parent | dungeon: refactor manual drop (diff) | |
| download | DungeonCrawl-7b78c39cb604961564b1d4e0a491eafacc85e8cb.tar.gz DungeonCrawl-7b78c39cb604961564b1d4e0a491eafacc85e8cb.tar.bz2 DungeonCrawl-7b78c39cb604961564b1d4e0a491eafacc85e8cb.zip | |
Enable more clippy lints
Diffstat (limited to 'dungeon')
| -rw-r--r-- | dungeon/src/bsp.rs | 28 | ||||
| -rw-r--r-- | dungeon/src/entity.rs | 9 | ||||
| -rw-r--r-- | dungeon/src/lib.rs | 6 | ||||
| -rw-r--r-- | dungeon/src/map.rs | 2 | ||||
| -rw-r--r-- | dungeon/src/pos.rs | 2 |
5 files changed, 22 insertions, 25 deletions
diff --git a/dungeon/src/bsp.rs b/dungeon/src/bsp.rs index db9ff99..971f5fe 100644 --- a/dungeon/src/bsp.rs +++ b/dungeon/src/bsp.rs @@ -4,7 +4,7 @@ use core::panic; use rand::prelude::IndexedRandom; use rand::{Rng, rngs::SmallRng}; -use std::cmp; // for min/max +use std::cmp::{self, Ordering}; // for min/max use crate::Floor; use crate::map::{MAP_SIZE, TILE_COUNT, Tile}; @@ -28,19 +28,19 @@ struct Rect { } // since this is all "internal", you likely have to use unit tests and not doctests impl Rect { - fn new(x: u16, y: u16, w: u16, h: u16) -> Self { + const fn new(x: u16, y: u16, w: u16, h: u16) -> Self { Self { x, y, w, h } } /// Returns the center point (cx, cy) of the rectangle as a Pos. - fn center(&self) -> Pos { + fn center(self) -> Pos { let cx = self.x + self.w / 2; let cy = self.y + self.h / 2; Pos::new(cx, cy).unwrap_or(const_pos!(1, 1)) } /// Returns a random point in this rectangle. - fn random_point(&self, rng: &mut SmallRng) -> Pos { + fn random_point(self, rng: &mut SmallRng) -> Pos { let rx = rng.random_range(self.x..(self.x + self.w)); let ry = rng.random_range(self.y..(self.y + self.h)); Pos::new(rx, ry).unwrap_or(self.center()) @@ -58,7 +58,7 @@ struct Node { } impl Node { - fn new(rect: Rect) -> Self { + const fn new(rect: Rect) -> Self { Self { rect, left: None, @@ -85,12 +85,10 @@ impl Node { } // Choose orientation: prefer the longer side - let split_h = if self.rect.w > self.rect.h { - false - } else if self.rect.h > self.rect.w { - true - } else { - rng.random_bool(0.5) + let split_h = match self.rect.w.cmp(&self.rect.h) { + Ordering::Greater => false, + Ordering::Less => true, + Ordering::Equal => rng.random(), }; // Choose split coordinate with margin so each side can contain a room @@ -251,7 +249,7 @@ impl Node { } /// Carve a room rectangle into the map (tile array) by setting tiles inside to Room. -fn carve_room(tiles: &mut [Tile; TILE_COUNT], room: &Rect) { +fn carve_room(tiles: &mut [Tile; TILE_COUNT], room: Rect) { for y in room.y..(room.y + room.h) { for x in room.x..(room.x + room.w) { let idx = x + y * MAP_SIZE; @@ -327,7 +325,7 @@ pub fn generate(rng: &mut SmallRng) -> Floor { let mut splitted_any = false; // Try splitting each leaf in order - for node_ptr in leaves.iter_mut() { + for node_ptr in &mut leaves { // Attempt to split if possible if node_ptr.split(rng) { splitted_any = true; @@ -346,8 +344,8 @@ pub fn generate(rng: &mut SmallRng) -> Floor { // Carve all rooms into the tile array let mut leaves = vec![]; root.collect_leaves(&mut leaves); - for leaf in leaves.iter() { - if let Some(room) = &leaf.room { + for leaf in &leaves { + if let Some(room) = leaf.room { carve_room(&mut tiles_box, room); } } diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs index e3b8fbd..818328c 100644 --- a/dungeon/src/entity.rs +++ b/dungeon/src/entity.rs @@ -46,7 +46,7 @@ pub enum EntityKind { impl EntityKind { /// Returns the move speed value for this type of entity in tiles/s - pub fn move_speed(&self) -> f32 { + pub const fn move_speed(&self) -> f32 { match &self { Self::Player => 5., Self::Zombie(_) => 4., @@ -190,6 +190,7 @@ impl Entity { /// let pos = Pos::new(0, 0).unwrap(); /// let player = Entity::zombie(pos); /// ``` + #[must_use] pub const fn zombie(pos: Pos) -> Self { let dir = Direction::East; let kind = EntityKind::Zombie(EnemyMoveState::new_idle()); @@ -252,9 +253,7 @@ impl Entity { EntityKind::Zombie(EnemyMoveState::new_roam(self.pos, floor, rng)); } EnemyMoveState::Roam(mut moves) => { - let p = if let Some(p) = moves.last() { - p - } else { + let Some(p) = moves.last() else { self.kind = EntityKind::Zombie(EnemyMoveState::new_idle()); return; }; @@ -340,7 +339,7 @@ impl Player { /// let pos = Pos::new(1, 2).unwrap(); /// let player = Player::new(pos); /// ``` - pub fn new(pos: Pos) -> Self { + pub const fn new(pos: Pos) -> Self { let entity = Entity::player(pos); let inventory = vec![]; Self { diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index 0021e81..b96cb83 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -74,7 +74,7 @@ impl Dungeon { /// Returns the current position of the camera (viewer) #[must_use] - pub fn camera(&self) -> FPos { + pub const fn camera(&self) -> FPos { self.player.entity.fpos } @@ -86,7 +86,7 @@ impl Dungeon { /// Returns the random number gen for the `Floor` #[must_use] - pub fn rng(&mut self) -> &mut SmallRng { + pub const fn rng(&mut self) -> &mut SmallRng { &mut self.rng } @@ -136,7 +136,7 @@ impl Dungeon { } fn act_non_players(&mut self, delta_time: f32) { - for enemy in self.enemies.iter_mut() { + for enemy in &mut self.enemies { enemy.handle_movement( self.player.entity.pos, &self.floor, diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index a28248b..4928a29 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -83,7 +83,7 @@ pub struct Floor { } impl Floor { /// Construct a floor from its components - pub fn new(tiles: Box<[Tile; TILE_COUNT]>, player_start: Pos) -> Self { + pub const fn new(tiles: Box<[Tile; TILE_COUNT]>, player_start: Pos) -> Self { Self { tiles, player_start, diff --git a/dungeon/src/pos.rs b/dungeon/src/pos.rs index 59daaa2..16f1fe5 100644 --- a/dungeon/src/pos.rs +++ b/dungeon/src/pos.rs @@ -541,7 +541,7 @@ impl FPos { D::East => x.add_assign(amt), D::West if x >= amt => x.sub_assign(amt), _ => return None, - }; + } Some(Self(x, y)) } |