monsters can be attack but do not die

This commit is contained in:
Joachim Lusiardi 2023-11-26 19:48:16 +01:00
parent d02ef5e046
commit 063c4f6067
4 changed files with 32 additions and 13 deletions

View File

@ -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<Artifact>; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) enemies: [[Option<Enemy>; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) enemies: [[Option<Monster>; 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<StructureElement>, Option<Artifact>, Option<Enemy>) {
pub fn get_element(&self, x: i16, y: i16) -> (Option<StructureElement>, Option<Artifact>, Option<Monster>) {
if x < 0 || y < 0 {
return (None, None, None);
}

View File

@ -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);

View File

@ -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];

20
src/monster.rs Normal file
View File

@ -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);
}
}