From 063c4f606778307f1199ee541d5adc3b74a85278 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 26 Nov 2023 19:48:16 +0100 Subject: [PATCH] monsters can be attack but do not die --- src/level.rs | 18 ++++++++---------- src/level_generator.rs | 6 +++--- src/main.rs | 1 + src/monster.rs | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 src/monster.rs diff --git a/src/level.rs b/src/level.rs index bcf0836..468d4e4 100644 --- a/src/level.rs +++ b/src/level.rs @@ -1,4 +1,5 @@ use std::cmp::{max, min}; +use crate::monster::Monster; use crate::player::Player; use crate::position::Position; @@ -21,18 +22,12 @@ pub enum Artifact { Chest { gold: usize }, } -#[derive(Copy, Clone, Debug, PartialEq)] -pub enum Enemy { - Rat -} - - #[derive(Copy, Clone, Debug, PartialEq)] pub struct Level { pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) artifacts: [[Option; LEVEL_HEIGHT]; LEVEL_WIDTH], - pub(crate) enemies: [[Option; LEVEL_HEIGHT]; LEVEL_WIDTH], + pub(crate) enemies: [[Option; LEVEL_HEIGHT]; LEVEL_WIDTH], pub(crate) start_x: usize, pub(crate) start_y: usize, pub(crate) end_x: usize, @@ -63,9 +58,12 @@ impl Level { return false; } - match self.enemies[x][y] { - Some(enemy) => { + match &self.enemies[x][y] { + Some(mut enemy) => { player.change_life(-1); + enemy.decrease_life(1); + println!("{}",enemy.get_life()); + return enemy.get_life() == 0 } _ => {} } @@ -82,7 +80,7 @@ impl Level { } self.artifacts[x][y] = None; } - pub fn get_element(&self, x: i16, y: i16) -> (Option, Option, Option) { + pub fn get_element(&self, x: i16, y: i16) -> (Option, Option, Option) { if x < 0 || y < 0 { return (None, None, None); } diff --git a/src/level_generator.rs b/src/level_generator.rs index d7b6853..b77f15d 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -9,7 +9,8 @@ use rand::prelude::SliceRandom; use rand::Rng; use rand::rngs::ThreadRng; -use crate::level::{Artifact, Enemy, Level, StructureElement}; +use crate::level::{Artifact, Level, StructureElement}; +use crate::monster::Monster; const ROOMS_VERTICAL: usize = 7; const ROOMS_HORIZONTAL: usize = 4; @@ -111,7 +112,6 @@ impl LevelGenerator { 1..=33 => { room_types.push(RoomType::EmptyRoom) } 34..=66 => { room_types.push(RoomType::TreasureRoom) } 67..=90 => { room_types.push(RoomType::MonsterRoom) } - 67..=90 => { room_types.push(RoomType::MonsterRoom) } _ => { room_types.push(RoomType::BasicRoom) } } } @@ -266,7 +266,7 @@ impl LevelGenerator { let t_x = left + room.offset_x + rng.gen_range(0..room.width); let t_y = top + room.offset_y + rng.gen_range(0..room.height); // TODO randomize enemies here - enemies[t_x][t_y] = Some(Enemy::Rat); + enemies[t_x][t_y] = Some(Monster::new(2)); } if room.kind == RoomType::StairDown { end_x = left + room.offset_x + rng.gen_range(0..room.width); diff --git a/src/main.rs b/src/main.rs index a80c530..a1acbf7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ mod level; mod position; mod level_widget; mod level_generator; +mod monster; fn main() -> Result<()> { let mut levels: [Level; 25] = [Level::new(); 25]; diff --git a/src/monster.rs b/src/monster.rs new file mode 100644 index 0000000..ec9b9be --- /dev/null +++ b/src/monster.rs @@ -0,0 +1,20 @@ +use std::cmp::{max, min}; + +use crate::position::Position; + +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct Monster { + life: usize, +} + +impl Monster { + pub fn new(life: usize) -> Monster { + Monster { + life, + } + } + pub fn get_life(&self) -> usize { self.life } + pub fn decrease_life(&mut self, by: i16) { + self.life = max(0, (self.life as i16 - by) as usize); + } +}