add defense attribute to player and monster
This commit is contained in:
parent
51123ecd8e
commit
62e13ace4c
|
@ -24,6 +24,7 @@ pub fn create_monsters(input: TokenStream) -> TokenStream {
|
|||
experience_gain: usize,
|
||||
ticks_between_steps: u128,
|
||||
damage_range: RangeInclusive<usize>,
|
||||
defense: usize,
|
||||
}
|
||||
impl Monster for #variant_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 damage(&self) -> usize { rand::thread_rng().gen_range(self.damage_range.clone()) }
|
||||
|
||||
fn defense(&self) -> usize { self.defense }
|
||||
#[cfg(test)]
|
||||
fn get_life(&self) -> usize { self.life }
|
||||
}
|
||||
|
|
|
@ -164,7 +164,7 @@ impl Game {
|
|||
match m {
|
||||
None => {}
|
||||
Some(m) => {
|
||||
let player_dmg = self.player.damage();
|
||||
let player_dmg = self.player.damage().saturating_sub(m.defense());
|
||||
m.decrease_life(player_dmg);
|
||||
if m.is_dead() {
|
||||
let level_up: bool = self.player.gain_experience(m.get_experience_gain());
|
||||
|
@ -192,8 +192,8 @@ impl Game {
|
|||
0,
|
||||
format!("you hit {} for {} damage.", m.get_name(), player_dmg).to_string(),
|
||||
);
|
||||
let monster_dmg = m.damage() as i16;
|
||||
self.player.change_life(-monster_dmg);
|
||||
let monster_dmg =m.damage().saturating_sub(self.player.defense());
|
||||
self.player.change_life(-(monster_dmg as i16));
|
||||
|
||||
self.messages.insert(
|
||||
0,
|
||||
|
|
|
@ -147,8 +147,8 @@ impl Level {
|
|||
if player.get_immutable_position().get_x() == new_x
|
||||
&& player.get_immutable_position().get_y() == new_y
|
||||
{
|
||||
let monster_dmg = self.monsters[index].damage() as i16;
|
||||
player.change_life(-monster_dmg);
|
||||
let monster_dmg =self.monsters[index].damage().saturating_sub(player.defense());
|
||||
player.change_life(-(monster_dmg as i16));
|
||||
if player.is_dead() {
|
||||
messages.insert(
|
||||
0,
|
||||
|
@ -172,7 +172,7 @@ impl Level {
|
|||
// player not dead => monster can occupy new position
|
||||
self.monsters[index].get_position().change(-dx, -dy);
|
||||
// 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);
|
||||
if self.monsters[index].is_dead() {
|
||||
let level_up: bool = player
|
||||
|
|
|
@ -109,9 +109,10 @@ fn main() -> Result<()> {
|
|||
.style(Style::default().bg(Color::Blue));
|
||||
frame.render_widget(
|
||||
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_max_life(),
|
||||
game.get_player().defense(),
|
||||
game.get_player().get_experience(),
|
||||
game.get_player().get_gold(),
|
||||
game.get_player().get_immutable_position().get_level() + 1,
|
||||
|
|
|
@ -16,6 +16,7 @@ pub trait Monster: HasPosition {
|
|||
#[cfg(test)]
|
||||
fn get_life(&self) -> usize;
|
||||
fn damage(&self) -> usize;
|
||||
fn defense(&self) -> usize;
|
||||
}
|
||||
|
||||
#[derive(CreateMonsters, PartialEq, Eq, Hash, Clone, Copy)]
|
||||
|
@ -49,6 +50,7 @@ create_monster!(
|
|||
experience_gain: 5,
|
||||
ticks_between_steps: 5,
|
||||
damage_range: 1..=2,
|
||||
defense: 0,
|
||||
);
|
||||
|
||||
create_monster!(
|
||||
|
@ -60,6 +62,7 @@ create_monster!(
|
|||
experience_gain: 7,
|
||||
ticks_between_steps: 7,
|
||||
damage_range: 2..=3,
|
||||
defense: 0,
|
||||
);
|
||||
|
||||
create_monster!(
|
||||
|
@ -71,6 +74,7 @@ create_monster!(
|
|||
experience_gain: 10,
|
||||
ticks_between_steps: 10,
|
||||
damage_range: 2..=3,
|
||||
defense: 0,
|
||||
);
|
||||
|
||||
create_monster!(
|
||||
|
@ -82,6 +86,7 @@ create_monster!(
|
|||
experience_gain: 10,
|
||||
ticks_between_steps: 20,
|
||||
damage_range: 1..=4,
|
||||
defense: 0,
|
||||
);
|
||||
|
||||
create_monster!(
|
||||
|
@ -93,6 +98,7 @@ create_monster!(
|
|||
experience_gain: 20,
|
||||
ticks_between_steps: 10,
|
||||
damage_range: 1..=5,
|
||||
defense: 0,
|
||||
);
|
||||
|
||||
pub fn create_monster_by_type(monster_type: &MonsterTypes, position: Position) -> Box<dyn Monster> {
|
||||
|
|
|
@ -15,6 +15,7 @@ pub struct Player {
|
|||
inventory: Vec<Potion>,
|
||||
inventory_slots: usize,
|
||||
level: usize,
|
||||
defense: usize,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
|
@ -29,6 +30,7 @@ impl Player {
|
|||
inventory: vec![],
|
||||
inventory_slots: 2,
|
||||
level: 1,
|
||||
defense: 0,
|
||||
}
|
||||
}
|
||||
pub fn get_name(&self) -> String {
|
||||
|
@ -67,7 +69,9 @@ impl Player {
|
|||
for (i, level_step) in get_level_ladder().enumerate() {
|
||||
if self.experience <= level_step && self.experience + amount > level_step {
|
||||
self.level += 1;
|
||||
// make life gain depend on level?
|
||||
self.max_life += 5;
|
||||
self.defense += 1;
|
||||
self.life = self.max_life;
|
||||
result = true;
|
||||
break;
|
||||
|
@ -92,6 +96,10 @@ impl Player {
|
|||
rand::thread_rng().gen_range(1..4)
|
||||
}
|
||||
|
||||
pub fn defense(&self) -> usize {
|
||||
self.defense
|
||||
}
|
||||
|
||||
pub fn add_to_inventory(&mut self, potion: &Potion) -> bool {
|
||||
if self.inventory.len() < self.inventory_slots {
|
||||
self.inventory.push(*potion);
|
||||
|
@ -138,6 +146,7 @@ fn test_get_name() {
|
|||
inventory: vec![],
|
||||
inventory_slots: 1,
|
||||
level: 0,
|
||||
defense: 0,
|
||||
};
|
||||
assert_eq!(p.get_name(), "Teddy Tester");
|
||||
}
|
||||
|
@ -164,6 +173,7 @@ fn test_change_life() {
|
|||
inventory: vec![],
|
||||
inventory_slots: 1,
|
||||
level: 0,
|
||||
defense: 0,
|
||||
};
|
||||
assert_eq!(p.get_life(), 5);
|
||||
p.change_life(-2);
|
||||
|
@ -200,6 +210,7 @@ fn test_max_life() {
|
|||
inventory: vec![],
|
||||
inventory_slots: 1,
|
||||
level: 0,
|
||||
defense: 0,
|
||||
};
|
||||
assert_eq!(p.get_max_life(), 10);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue