updated deps
fixed clippy remarks
This commit is contained in:
61
src/level.rs
61
src/level.rs
@@ -1,14 +1,14 @@
|
||||
use std::cmp::{max, min};
|
||||
|
||||
use rand::Rng;
|
||||
use rand::rngs::ThreadRng;
|
||||
use rand::Rng;
|
||||
|
||||
#[cfg(test)]
|
||||
use crate::artifacts::{Chest, Potion};
|
||||
use crate::artifacts::Artifact;
|
||||
#[cfg(test)]
|
||||
use crate::monster::{Orc, Rat};
|
||||
use crate::artifacts::{Chest, Potion};
|
||||
use crate::monster::Monster;
|
||||
#[cfg(test)]
|
||||
use crate::monster::{Orc, Rat};
|
||||
use crate::player::Player;
|
||||
use crate::position::HasPosition;
|
||||
use crate::position::Position;
|
||||
@@ -27,6 +27,9 @@ pub enum StructureElement {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
type PossibleArtifact<'a> = Option<&'a mut Box<(dyn Artifact + 'static)>>;
|
||||
type PossibleMonster<'a> = Option<&'a mut Box<(dyn Monster + 'static)>>;
|
||||
|
||||
pub struct Level {
|
||||
pub(crate) level: usize,
|
||||
pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
|
||||
@@ -41,7 +44,15 @@ pub struct Level {
|
||||
}
|
||||
|
||||
impl Level {
|
||||
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Box<(dyn Monster + 'static)>>, Option<&mut Box<(dyn Artifact + 'static)>>) {
|
||||
pub fn get_element(
|
||||
&mut self,
|
||||
x: i16,
|
||||
y: i16,
|
||||
) -> (
|
||||
Option<StructureElement>,
|
||||
PossibleMonster,
|
||||
PossibleArtifact
|
||||
) {
|
||||
if x < 0 || y < 0 {
|
||||
return (None, None, None);
|
||||
}
|
||||
@@ -129,19 +140,27 @@ impl Level {
|
||||
loop {
|
||||
// calculate the direction the monster will try to walk
|
||||
let (dx, dy) = match self.rng.gen_range(0..5) {
|
||||
1 => { (1, 0) }
|
||||
2 => { (-1, 0) }
|
||||
3 => { (0, 1) }
|
||||
4 => { (0, -1) }
|
||||
_ => { (0, 0) }
|
||||
1 => (1, 0),
|
||||
2 => (-1, 0),
|
||||
3 => (0, 1),
|
||||
4 => (0, -1),
|
||||
_ => (0, 0),
|
||||
};
|
||||
if self.can_monster_move(self.monsters[index].as_ref(), dx, dy) {
|
||||
let (new_x, new_y) = self.monsters[index].get_position().change(dx, dy);
|
||||
if player.get_immutable_position().get_x() == new_x && player.get_immutable_position().get_y() == new_y {
|
||||
if player.get_immutable_position().get_x() == new_x
|
||||
&& player.get_immutable_position().get_y() == new_y
|
||||
{
|
||||
self.monsters[index].decrease_life(1);
|
||||
player.change_life(-1);
|
||||
messages.insert(0, format!("{} hits you.", self.monsters[index].get_name()).to_string());
|
||||
messages.insert(0, format!("you hit {}.", self.monsters[index].get_name()).to_string());
|
||||
messages.insert(
|
||||
0,
|
||||
format!("{} hits you.", self.monsters[index].get_name()).to_string(),
|
||||
);
|
||||
messages.insert(
|
||||
0,
|
||||
format!("you hit {}.", self.monsters[index].get_name()).to_string(),
|
||||
);
|
||||
// if the attack did not kill the opponent, back down
|
||||
if !player.is_dead() {
|
||||
self.monsters[index].get_position().change(-dx, -dy);
|
||||
@@ -171,7 +190,9 @@ impl Level {
|
||||
|
||||
for index in 0..self.monsters.len() {
|
||||
let pos = self.monsters[index].get_immutable_position();
|
||||
if pos.get_x() == new_x && pos.get_y() == new_y { return false; }
|
||||
if pos.get_x() == new_x && pos.get_y() == new_y {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self.structure[new_x][new_y] != StructureElement::Wall
|
||||
}
|
||||
@@ -239,8 +260,16 @@ fn test_get_element() {
|
||||
let mut l = Level::new(0);
|
||||
assert_eq!(l.get_element(-1, -1).0, None);
|
||||
assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Unknown);
|
||||
assert_eq!(l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1).0.unwrap(), StructureElement::Unknown);
|
||||
assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0, None);
|
||||
assert_eq!(
|
||||
l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1)
|
||||
.0
|
||||
.unwrap(),
|
||||
StructureElement::Unknown
|
||||
);
|
||||
assert_eq!(
|
||||
l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0,
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user