Work on more references

This commit is contained in:
Joachim Lusiardi 2023-11-26 20:14:33 +01:00
parent 0ef034f14f
commit 227e44dd43
3 changed files with 13 additions and 5 deletions

View File

@ -1,7 +1,7 @@
use crate::level::Level; use crate::level::Level;
use crate::player::Player; use crate::player::Player;
pub struct Game { pub struct Game<'game> {
pub player: Player, pub player: &'game mut Player,
pub levels: [Level; 25] pub levels: Vec<&'game mut Level>,
} }

View File

@ -8,9 +8,14 @@ mod level;
mod position; mod position;
fn main() { fn main() {
let mut p = Player::new("Teddy Tester", 10);
let g = Game { let g = Game {
player: Player::new("Teddy Tester", 10), player: &mut p,
levels: [Level::new();25] levels: Vec::new()
}; };
println!("{}",g.player.get_name()); println!("{}",g.player.get_name());
println!("{}",g.player.get_life());
g.player.change_life(-1);
println!("{}",g.player.get_life());
} }

View File

@ -23,6 +23,9 @@ impl Player {
pub fn change_life(&mut self, by: i16) { pub fn change_life(&mut self, by: i16) {
self.life = max(0, min(self.max_life, self.life + by)); self.life = max(0, min(self.max_life, self.life + by));
} }
pub fn get_life(&self) -> i16 {
self.life
}
} }
#[test] #[test]