add mechanism for level gain
This commit is contained in:
parent
3249529f94
commit
a542e4b1c0
12
src/game.rs
12
src/game.rs
|
@ -167,7 +167,7 @@ impl Game {
|
||||||
let player_dmg = self.player.damage();
|
let player_dmg = self.player.damage();
|
||||||
m.decrease_life(player_dmg);
|
m.decrease_life(player_dmg);
|
||||||
if m.is_dead() {
|
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(
|
self.messages.insert(
|
||||||
0,
|
0,
|
||||||
format!(
|
format!(
|
||||||
|
@ -177,6 +177,16 @@ impl Game {
|
||||||
)
|
)
|
||||||
.to_string(),
|
.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 {
|
} else {
|
||||||
self.messages.insert(
|
self.messages.insert(
|
||||||
0,
|
0,
|
||||||
|
|
63
src/level.rs
63
src/level.rs
|
@ -149,41 +149,64 @@ impl Level {
|
||||||
{
|
{
|
||||||
let monster_dmg = self.monsters[index].damage() as i16;
|
let monster_dmg = self.monsters[index].damage() as i16;
|
||||||
player.change_life(-monster_dmg);
|
player.change_life(-monster_dmg);
|
||||||
messages.insert(
|
if player.is_dead() {
|
||||||
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() {
|
|
||||||
messages.insert(
|
messages.insert(
|
||||||
0,
|
0,
|
||||||
format!(
|
format!(
|
||||||
"you hit {} for {} damage and kill it.",
|
"{} hits you for {} damage and kills you.",
|
||||||
self.monsters[index].get_name(),
|
self.monsters[index].get_name(),
|
||||||
player_dmg
|
monster_dmg
|
||||||
)
|
)
|
||||||
.to_string(),
|
.to_string(),
|
||||||
);
|
);
|
||||||
self.monsters[index].get_position().change(-dx, -dy);
|
|
||||||
} else {
|
} else {
|
||||||
messages.insert(
|
messages.insert(
|
||||||
0,
|
0,
|
||||||
format!(
|
format!(
|
||||||
"you hit {} for {} damage.",
|
"{} hits you for {} damage.",
|
||||||
self.monsters[index].get_name(),
|
self.monsters[index].get_name(),
|
||||||
player_dmg
|
monster_dmg
|
||||||
)
|
)
|
||||||
.to_string(),
|
.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;
|
break;
|
||||||
|
|
|
@ -540,12 +540,12 @@ fn test_create_mst() {
|
||||||
rooms[3][3].kind = RoomType::BasicRoom;
|
rooms[3][3].kind = RoomType::BasicRoom;
|
||||||
let res = LevelGenerator::create_mst(&rooms);
|
let res = LevelGenerator::create_mst(&rooms);
|
||||||
assert_eq!(res.node_count(), 4);
|
assert_eq!(res.node_count(), 4);
|
||||||
assert_eq!(res.edge_count(), 3);
|
assert_eq!(res.edge_count(), 4);
|
||||||
|
|
||||||
rooms[3][5].kind = RoomType::BasicRoom;
|
rooms[3][5].kind = RoomType::BasicRoom;
|
||||||
let res = LevelGenerator::create_mst(&rooms);
|
let res = LevelGenerator::create_mst(&rooms);
|
||||||
assert_eq!(res.node_count(), 5);
|
assert_eq!(res.node_count(), 5);
|
||||||
assert_eq!(res.edge_count(), 4);
|
assert_eq!(res.edge_count(), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
pub struct LevelLadder {
|
||||||
|
curr: usize,
|
||||||
|
next: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for LevelLadder {
|
||||||
|
type Item = usize;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
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));
|
||||||
|
}
|
|
@ -37,6 +37,7 @@ mod monster;
|
||||||
mod player;
|
mod player;
|
||||||
mod position;
|
mod position;
|
||||||
mod room;
|
mod room;
|
||||||
|
mod level_ladder;
|
||||||
|
|
||||||
//
|
//
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
|
|
@ -2,6 +2,7 @@ use rand::Rng;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
use crate::artifacts::Potion;
|
use crate::artifacts::Potion;
|
||||||
|
use crate::level_ladder::get_level_ladder;
|
||||||
use crate::position::{HasPosition, Position};
|
use crate::position::{HasPosition, Position};
|
||||||
|
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
|
@ -13,6 +14,7 @@ pub struct Player {
|
||||||
experience: usize,
|
experience: usize,
|
||||||
inventory: Vec<Potion>,
|
inventory: Vec<Potion>,
|
||||||
inventory_slots: usize,
|
inventory_slots: usize,
|
||||||
|
level: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
|
@ -26,6 +28,7 @@ impl Player {
|
||||||
experience: 0,
|
experience: 0,
|
||||||
inventory: vec![],
|
inventory: vec![],
|
||||||
inventory_slots: 2,
|
inventory_slots: 2,
|
||||||
|
level: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
|
@ -59,8 +62,26 @@ impl Player {
|
||||||
self.gold
|
self.gold
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gain_experience(&mut self, amount: usize) {
|
pub fn gain_experience(&mut self, amount: usize) -> bool {
|
||||||
self.experience += amount
|
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 {
|
pub fn get_experience(&self) -> usize {
|
||||||
|
@ -116,6 +137,7 @@ fn test_get_name() {
|
||||||
experience: 0,
|
experience: 0,
|
||||||
inventory: vec![],
|
inventory: vec![],
|
||||||
inventory_slots: 1,
|
inventory_slots: 1,
|
||||||
|
level: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_name(), "Teddy Tester");
|
assert_eq!(p.get_name(), "Teddy Tester");
|
||||||
}
|
}
|
||||||
|
@ -141,6 +163,7 @@ fn test_change_life() {
|
||||||
experience: 0,
|
experience: 0,
|
||||||
inventory: vec![],
|
inventory: vec![],
|
||||||
inventory_slots: 1,
|
inventory_slots: 1,
|
||||||
|
level: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_life(), 5);
|
assert_eq!(p.get_life(), 5);
|
||||||
p.change_life(-2);
|
p.change_life(-2);
|
||||||
|
@ -176,6 +199,7 @@ fn test_max_life() {
|
||||||
experience: 0,
|
experience: 0,
|
||||||
inventory: vec![],
|
inventory: vec![],
|
||||||
inventory_slots: 1,
|
inventory_slots: 1,
|
||||||
|
level: 0,
|
||||||
};
|
};
|
||||||
assert_eq!(p.get_max_life(), 10);
|
assert_eq!(p.get_max_life(), 10);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue