add defense attribute to player and monster

This commit is contained in:
Joachim Lusiardi 2024-11-24 13:24:39 +01:00
parent 51123ecd8e
commit 62e13ace4c
6 changed files with 27 additions and 8 deletions

View File

@ -24,6 +24,7 @@ pub fn create_monsters(input: TokenStream) -> TokenStream {
experience_gain: usize, experience_gain: usize,
ticks_between_steps: u128, ticks_between_steps: u128,
damage_range: RangeInclusive<usize>, damage_range: RangeInclusive<usize>,
defense: usize,
} }
impl Monster for #variant_name { impl Monster for #variant_name {
fn get_name(&self) -> &str { &self.name } fn get_name(&self) -> &str { &self.name }
@ -35,7 +36,7 @@ pub fn create_monsters(input: TokenStream) -> TokenStream {
} }
fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps } fn get_ticks_between_steps(&self) -> u128 { self.ticks_between_steps }
fn damage(&self) -> usize { rand::thread_rng().gen_range(self.damage_range.clone()) } fn damage(&self) -> usize { rand::thread_rng().gen_range(self.damage_range.clone()) }
fn defense(&self) -> usize { self.defense }
#[cfg(test)] #[cfg(test)]
fn get_life(&self) -> usize { self.life } fn get_life(&self) -> usize { self.life }
} }

View File

@ -164,7 +164,7 @@ impl Game {
match m { match m {
None => {} None => {}
Some(m) => { Some(m) => {
let player_dmg = self.player.damage(); let player_dmg = self.player.damage().saturating_sub(m.defense());
m.decrease_life(player_dmg); m.decrease_life(player_dmg);
if m.is_dead() { if m.is_dead() {
let level_up: bool = self.player.gain_experience(m.get_experience_gain()); let level_up: bool = self.player.gain_experience(m.get_experience_gain());
@ -192,8 +192,8 @@ impl Game {
0, 0,
format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(), format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(),
); );
let monster_dmg = m.damage() as i16; let monster_dmg =m.damage().saturating_sub(self.player.defense());
self.player.change_life(-monster_dmg); self.player.change_life(-(monster_dmg as i16));
self.messages.insert( self.messages.insert(
0, 0,

View File

@ -147,8 +147,8 @@ impl Level {
if player.get_immutable_position().get_x() == new_x if player.get_immutable_position().get_x() == new_x
&& player.get_immutable_position().get_y() == new_y && player.get_immutable_position().get_y() == new_y
{ {
let monster_dmg = self.monsters[index].damage() as i16; let monster_dmg =self.monsters[index].damage().saturating_sub(player.defense());
player.change_life(-monster_dmg); player.change_life(-(monster_dmg as i16));
if player.is_dead() { if player.is_dead() {
messages.insert( messages.insert(
0, 0,
@ -172,7 +172,7 @@ impl Level {
// player not dead => monster can occupy new position // player not dead => monster can occupy new position
self.monsters[index].get_position().change(-dx, -dy); self.monsters[index].get_position().change(-dx, -dy);
// player fights back // player fights back
let player_dmg = player.damage(); let player_dmg = player.damage().saturating_sub(self.monsters[index].defense());
self.monsters[index].decrease_life(player_dmg); self.monsters[index].decrease_life(player_dmg);
if self.monsters[index].is_dead() { if self.monsters[index].is_dead() {
let level_up: bool = player let level_up: bool = player

View File

@ -109,9 +109,10 @@ fn main() -> Result<()> {
.style(Style::default().bg(Color::Blue)); .style(Style::default().bg(Color::Blue));
frame.render_widget( frame.render_widget(
Paragraph::new(format!( Paragraph::new(format!(
"Health: {} of {}\nExp: {}\nGold: {}\nLevel: {} of {}\nInventory used: {} of {}", "Health: {} of {}\nDefense: {}\nExp: {}\nGold: {}\nLevel: {} of {}\nInventory used: {} of {}",
game.get_player().get_life(), game.get_player().get_life(),
game.get_player().get_max_life(), game.get_player().get_max_life(),
game.get_player().defense(),
game.get_player().get_experience(), game.get_player().get_experience(),
game.get_player().get_gold(), game.get_player().get_gold(),
game.get_player().get_immutable_position().get_level() + 1, game.get_player().get_immutable_position().get_level() + 1,

View File

@ -16,6 +16,7 @@ pub trait Monster: HasPosition {
#[cfg(test)] #[cfg(test)]
fn get_life(&self) -> usize; fn get_life(&self) -> usize;
fn damage(&self) -> usize; fn damage(&self) -> usize;
fn defense(&self) -> usize;
} }
#[derive(CreateMonsters, PartialEq, Eq, Hash, Clone, Copy)] #[derive(CreateMonsters, PartialEq, Eq, Hash, Clone, Copy)]
@ -49,6 +50,7 @@ create_monster!(
experience_gain: 5, experience_gain: 5,
ticks_between_steps: 5, ticks_between_steps: 5,
damage_range: 1..=2, damage_range: 1..=2,
defense: 0,
); );
create_monster!( create_monster!(
@ -60,6 +62,7 @@ create_monster!(
experience_gain: 7, experience_gain: 7,
ticks_between_steps: 7, ticks_between_steps: 7,
damage_range: 2..=3, damage_range: 2..=3,
defense: 0,
); );
create_monster!( create_monster!(
@ -71,6 +74,7 @@ create_monster!(
experience_gain: 10, experience_gain: 10,
ticks_between_steps: 10, ticks_between_steps: 10,
damage_range: 2..=3, damage_range: 2..=3,
defense: 0,
); );
create_monster!( create_monster!(
@ -82,6 +86,7 @@ create_monster!(
experience_gain: 10, experience_gain: 10,
ticks_between_steps: 20, ticks_between_steps: 20,
damage_range: 1..=4, damage_range: 1..=4,
defense: 0,
); );
create_monster!( create_monster!(
@ -93,6 +98,7 @@ create_monster!(
experience_gain: 20, experience_gain: 20,
ticks_between_steps: 10, ticks_between_steps: 10,
damage_range: 1..=5, damage_range: 1..=5,
defense: 0,
); );
pub fn create_monster_by_type(monster_type: &MonsterTypes, position: Position) -> Box<dyn Monster> { pub fn create_monster_by_type(monster_type: &MonsterTypes, position: Position) -> Box<dyn Monster> {

View File

@ -15,6 +15,7 @@ pub struct Player {
inventory: Vec<Potion>, inventory: Vec<Potion>,
inventory_slots: usize, inventory_slots: usize,
level: usize, level: usize,
defense: usize,
} }
impl Player { impl Player {
@ -29,6 +30,7 @@ impl Player {
inventory: vec![], inventory: vec![],
inventory_slots: 2, inventory_slots: 2,
level: 1, level: 1,
defense: 0,
} }
} }
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {
@ -67,7 +69,9 @@ impl Player {
for (i, level_step) in get_level_ladder().enumerate() { for (i, level_step) in get_level_ladder().enumerate() {
if self.experience <= level_step && self.experience + amount > level_step { if self.experience <= level_step && self.experience + amount > level_step {
self.level += 1; self.level += 1;
// make life gain depend on level?
self.max_life += 5; self.max_life += 5;
self.defense += 1;
self.life = self.max_life; self.life = self.max_life;
result = true; result = true;
break; break;
@ -92,6 +96,10 @@ impl Player {
rand::thread_rng().gen_range(1..4) rand::thread_rng().gen_range(1..4)
} }
pub fn defense(&self) -> usize {
self.defense
}
pub fn add_to_inventory(&mut self, potion: &Potion) -> bool { pub fn add_to_inventory(&mut self, potion: &Potion) -> bool {
if self.inventory.len() < self.inventory_slots { if self.inventory.len() < self.inventory_slots {
self.inventory.push(*potion); self.inventory.push(*potion);
@ -138,6 +146,7 @@ fn test_get_name() {
inventory: vec![], inventory: vec![],
inventory_slots: 1, inventory_slots: 1,
level: 0, level: 0,
defense: 0,
}; };
assert_eq!(p.get_name(), "Teddy Tester"); assert_eq!(p.get_name(), "Teddy Tester");
} }
@ -164,6 +173,7 @@ fn test_change_life() {
inventory: vec![], inventory: vec![],
inventory_slots: 1, inventory_slots: 1,
level: 0, level: 0,
defense: 0,
}; };
assert_eq!(p.get_life(), 5); assert_eq!(p.get_life(), 5);
p.change_life(-2); p.change_life(-2);
@ -200,6 +210,7 @@ fn test_max_life() {
inventory: vec![], inventory: vec![],
inventory_slots: 1, inventory_slots: 1,
level: 0, level: 0,
defense: 0,
}; };
assert_eq!(p.get_max_life(), 10); assert_eq!(p.get_max_life(), 10);
} }