From a542e4b1c0730cfdf8c6c6529d5d8bf6871052d6 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Wed, 6 Nov 2024 14:29:30 +0100 Subject: [PATCH] add mechanism for level gain --- src/game.rs | 12 +++++++- src/level.rs | 63 ++++++++++++++++++++++++++++-------------- src/level_generator.rs | 4 +-- src/level_ladder.rs | 30 ++++++++++++++++++++ src/main.rs | 1 + src/player.rs | 28 +++++++++++++++++-- 6 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 src/level_ladder.rs diff --git a/src/game.rs b/src/game.rs index a96cfe8..725d26f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -167,7 +167,7 @@ impl Game { let player_dmg = self.player.damage(); m.decrease_life(player_dmg); if m.is_dead() { - self.player.gain_experience(m.get_experience_gain()); + let level_up: bool = self.player.gain_experience(m.get_experience_gain()); self.messages.insert( 0, format!( @@ -177,6 +177,16 @@ impl Game { ) .to_string(), ); + if level_up { + self.messages.insert( + 0, + format!( + "you reach a new experience level, it's now {}", + self.player.get_experience_level(), + ) + .to_string(), + ); + } } else { self.messages.insert( 0, diff --git a/src/level.rs b/src/level.rs index 67946da..b7bc2e1 100644 --- a/src/level.rs +++ b/src/level.rs @@ -149,41 +149,64 @@ impl Level { { let monster_dmg = self.monsters[index].damage() as i16; player.change_life(-monster_dmg); - messages.insert( - 0, - format!( - "{} hits you for {} damage.", - self.monsters[index].get_name(), - monster_dmg - ) - .to_string(), - ); - - let player_dmg = player.damage(); - self.monsters[index].decrease_life(player_dmg); - - // if the attack did not kill the opponent, back down - if !player.is_dead() { + if player.is_dead() { messages.insert( 0, format!( - "you hit {} for {} damage and kill it.", + "{} hits you for {} damage and kills you.", self.monsters[index].get_name(), - player_dmg + monster_dmg ) .to_string(), ); - self.monsters[index].get_position().change(-dx, -dy); } else { messages.insert( 0, format!( - "you hit {} for {} damage.", + "{} hits you for {} damage.", self.monsters[index].get_name(), - player_dmg + monster_dmg ) .to_string(), ); + // player not dead => monster can occupy new position + self.monsters[index].get_position().change(-dx, -dy); + // player fights back + let player_dmg = player.damage(); + self.monsters[index].decrease_life(player_dmg); + if self.monsters[index].is_dead() { + let level_up: bool = player + .gain_experience(self.monsters[index].get_experience_gain()); + messages.insert( + 0, + format!( + "you hit {} for {} damage and kill it.", + self.monsters[index].get_name(), + player_dmg + ) + .to_string(), + ); + if level_up { + messages.insert( + 0, + format!( + "you reach a new experience level, it's now {}", + player.get_experience_level(), + ) + .to_string(), + ); + } + } else { + messages.insert( + 0, + format!( + "you hit {} for {} damage.", + self.monsters[index].get_name(), + player_dmg + ) + .to_string(), + ); + } } } break; diff --git a/src/level_generator.rs b/src/level_generator.rs index 4ce4883..530eb98 100644 --- a/src/level_generator.rs +++ b/src/level_generator.rs @@ -540,12 +540,12 @@ fn test_create_mst() { rooms[3][3].kind = RoomType::BasicRoom; let res = LevelGenerator::create_mst(&rooms); assert_eq!(res.node_count(), 4); - assert_eq!(res.edge_count(), 3); + assert_eq!(res.edge_count(), 4); rooms[3][5].kind = RoomType::BasicRoom; let res = LevelGenerator::create_mst(&rooms); assert_eq!(res.node_count(), 5); - assert_eq!(res.edge_count(), 4); + assert_eq!(res.edge_count(), 5); } #[test] diff --git a/src/level_ladder.rs b/src/level_ladder.rs new file mode 100644 index 0000000..c012091 --- /dev/null +++ b/src/level_ladder.rs @@ -0,0 +1,30 @@ +pub struct LevelLadder { + curr: usize, + next: usize, +} + +impl Iterator for LevelLadder { + type Item = usize; + + fn next(&mut self) -> Option { + let current = self.curr; + + self.curr = self.next; + self.next = current + self.next; + + // Since there's no endpoint to a Fibonacci sequence, the `Iterator` + // will never return `None`, and `Some` is always returned. + Some(current * 30) + } +} + +// Returns a Fibonacci sequence generator +pub fn get_level_ladder() -> LevelLadder { + LevelLadder { curr: 1, next: 2 } +} + +#[test] +fn test_level_ladder() { + let mut iter = get_level_ladder(); + assert_eq!(iter.next(), Some(30)); +} diff --git a/src/main.rs b/src/main.rs index 213db9e..c26e377 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ mod monster; mod player; mod position; mod room; +mod level_ladder; // fn main() -> Result<()> { diff --git a/src/player.rs b/src/player.rs index c554f5c..4a97ffc 100644 --- a/src/player.rs +++ b/src/player.rs @@ -2,6 +2,7 @@ use rand::Rng; use std::cmp::{max, min}; use crate::artifacts::Potion; +use crate::level_ladder::get_level_ladder; use crate::position::{HasPosition, Position}; pub struct Player { @@ -13,6 +14,7 @@ pub struct Player { experience: usize, inventory: Vec, inventory_slots: usize, + level: usize, } impl Player { @@ -26,6 +28,7 @@ impl Player { experience: 0, inventory: vec![], inventory_slots: 2, + level: 1, } } pub fn get_name(&self) -> String { @@ -59,8 +62,26 @@ impl Player { self.gold } - pub fn gain_experience(&mut self, amount: usize) { - self.experience += amount + pub fn gain_experience(&mut self, amount: usize) -> bool { + let mut result = false; + for (i, level_step) in get_level_ladder().enumerate() { + if self.experience <= level_step && self.experience + amount > level_step { + self.level += 1; + self.max_life += 5; + self.life = self.max_life; + result = true; + break; + } + if i > self.level { + break; + } + } + self.experience += amount; + result + } + + pub fn get_experience_level(&self) -> usize { + self.level } pub fn get_experience(&self) -> usize { @@ -116,6 +137,7 @@ fn test_get_name() { experience: 0, inventory: vec![], inventory_slots: 1, + level: 0, }; assert_eq!(p.get_name(), "Teddy Tester"); } @@ -141,6 +163,7 @@ fn test_change_life() { experience: 0, inventory: vec![], inventory_slots: 1, + level: 0, }; assert_eq!(p.get_life(), 5); p.change_life(-2); @@ -176,6 +199,7 @@ fn test_max_life() { experience: 0, inventory: vec![], inventory_slots: 1, + level: 0, }; assert_eq!(p.get_max_life(), 10); }