From cca9e8f285db979db7e3115801c197dc273d8ae1 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Fri, 22 Dec 2023 22:53:58 +0100 Subject: [PATCH] moving monsters I --- src/game.rs | 4 ++-- src/level.rs | 11 ++++++++++- src/main.rs | 12 ++++++++---- src/monster.rs | 2 -- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/game.rs b/src/game.rs index c95e1b5..8de19d6 100644 --- a/src/game.rs +++ b/src/game.rs @@ -190,11 +190,11 @@ impl Game { } } /// updates the player's current level. This will remove collected artifacts and dead monsters. - pub fn update_level(&mut self) { + pub fn update_level(&mut self, ticks: u64) { let player_pos = &self.player.get_immutable_position(); let player_level = player_pos.get_level(); let level = &mut self.levels[player_level]; - level.update(); + level.update(ticks); } } diff --git a/src/level.rs b/src/level.rs index 8e3e6f2..66b8158 100644 --- a/src/level.rs +++ b/src/level.rs @@ -152,7 +152,7 @@ impl Level { } } - pub fn update(&mut self) { + pub fn update(&mut self, ticks: u64) { for (index, a) in &mut self.artifacts.iter().enumerate() { if a.was_collected() { self.artifacts.remove(index); @@ -165,6 +165,15 @@ impl Level { break; } } + for index in 0..self.monsters.len() { + let mut dx = 0; + let speed = 50; + if ticks % speed == 0 { + dx = if (ticks / speed) % 2 == 0 { -1 } else { 1 }; + } + + self.monsters[index].get_position().change(dx, 0); + } } } diff --git a/src/main.rs b/src/main.rs index 4daca28..f61e8bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ fn main() -> Result<()> { let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?; terminal.clear()?; + let mut ticks: u64 = 0; loop { terminal.draw(|frame| { let mut area = frame.size(); @@ -67,19 +68,21 @@ fn main() -> Result<()> { height: 25, }; frame.render_widget( - Paragraph::new(format!("{}\nHealth: {}/{}\nExp: {}\nGold: {}\nLevel: {}", + Paragraph::new(format!("{}\nHealth: {}/{}\nExp: {}\nGold: {}\nLevel: {}\nticks: {}", game.get_player().get_name(), game.get_player().get_life(), game.get_player().get_max_life(), game.get_player().get_experience(), game.get_player().get_gold(), - game.get_player().get_immutable_position().get_level())) + game.get_player().get_immutable_position().get_level(), + ticks + )) .white() .on_blue(), stats_area, ); })?; - if event::poll(std::time::Duration::from_millis(16))? { + if event::poll(std::time::Duration::from_millis(50))? { if let event::Event::Key(key) = event::read()? { if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') { break; @@ -97,13 +100,14 @@ fn main() -> Result<()> { game.move_player(new_pos.0, new_pos.1); } game.player_collects_artifact(); - game.update_level(); if game.get_game_state() != GameState::RUNNING { break; } } } } + game.update_level(ticks); + ticks+=1; } loop { let _ = terminal.draw(|frame| { diff --git a/src/monster.rs b/src/monster.rs index 02e24c4..95816eb 100644 --- a/src/monster.rs +++ b/src/monster.rs @@ -8,7 +8,6 @@ pub trait Monster { fn decrease_life(&mut self, by: usize); fn get_immutable_position(&self) -> &Position; fn get_experience_gain(&self) -> usize; - #[cfg(test)] fn get_position(&mut self) -> &mut Position; #[cfg(test)] fn get_life(&self) -> usize; @@ -26,7 +25,6 @@ macro_rules! default_monster { fn get_immutable_position(&self) -> &Position { &self.position } - #[cfg(test)] fn get_position(&mut self) -> &mut Position { &mut self.position }