2 Commits

4 changed files with 16 additions and 4 deletions

7
build.rs Normal file
View File

@@ -0,0 +1,7 @@
use std::process::Command;
fn main() {
let output = Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output().unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
}

View File

@@ -68,8 +68,11 @@ impl Artifact for Potion {
}
fn get_immutable_position(&self) -> &Position { &self.position }
fn collect(&mut self, player: &mut Player) {
player.change_life(self.health.try_into().unwrap());
self.health = 0;
// only consume potion of the player can gain at least one health point
if !player.is_healthy() {
player.change_life(self.health.try_into().unwrap());
self.health = 0;
}
}
fn was_collected(&self) -> bool {

View File

@@ -160,6 +160,7 @@ fn main() -> Result<()> {
text += format!("\nYou gained {} experience.", game.get_player().get_experience()).as_str();
text += format!("\nYou collected {} gold.", game.get_player().get_gold()).as_str();
text += format!("\nYou played {} seconds.", playtime.as_secs()).as_str();
text += format!("\nYou played game version '{}'.", env!("GIT_HASH")).as_str();
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
frame.render_widget(paragraph, area);
});
@@ -171,7 +172,6 @@ fn main() -> Result<()> {
}
}
}
stdout().execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())

View File

@@ -28,11 +28,13 @@ impl Player {
pub fn change_life(&mut self, by: i16) {
self.life = max(0, min(self.max_life, self.life + by));
}
/// returns true if the player is dead (life <= 0)
pub fn get_life(&self) -> i16 {
self.life
}
/// returns true if the player is dead (life <= 0)
pub fn is_dead(&self) -> bool { self.life <= 0 }
/// returns true if the player's life is at maximum
pub fn is_healthy(&self) -> bool { self.life == self.max_life }
pub fn get_max_life(&self) -> i16 {
self.max_life
}