From 970904c41111bbde9c5b749a7984b5ed4216a0ba Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Fri, 19 Jan 2024 10:39:05 +0100 Subject: [PATCH] Monsters move after the player moved this allows direct counter attack or fleeing --- src/game.rs | 6 ++++-- src/level.rs | 5 +++-- src/main.rs | 23 ++++++++++++++++++----- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/game.rs b/src/game.rs index 1fadfef..fe4c02b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -199,11 +199,13 @@ impl Game { } } /// updates the player's current level. This will remove collected artifacts and dead monsters. - pub fn update_level(&mut self, ticks: u128) { + /// ticks is measuring the progress of the game even if the player is not moving, player_moved + /// indicates the player moved + pub fn update_level(&mut self, ticks: u128, player_moved: bool) { let player_pos = &self.player.get_immutable_position(); let player_level = player_pos.get_level(); let level = &mut self.levels[player_level]; - level.update(ticks, &mut self.player, &mut self.messages); + level.update(ticks, player_moved, &mut self.player, &mut self.messages); } } diff --git a/src/level.rs b/src/level.rs index ba8f056..aab2faa 100644 --- a/src/level.rs +++ b/src/level.rs @@ -109,7 +109,7 @@ impl Level { } } } - pub fn update(&mut self, ticks: u128, player: &mut Player, messages: &mut Vec) { + pub fn update(&mut self, ticks: u128, player_moved: bool, player: &mut Player, messages: &mut Vec) { for (index, a) in &mut self.artifacts.iter().enumerate() { if a.was_collected() { self.artifacts.remove(index); @@ -120,7 +120,8 @@ impl Level { if self.monsters[index].is_dead() { continue; } - if ticks % self.monsters[index].get_ticks_between_steps() != 0 { + // if the player moved, the monsters move as well all the time + if !player_moved && ticks % self.monsters[index].get_ticks_between_steps() != 0 { continue; } loop { diff --git a/src/main.rs b/src/main.rs index af17b53..6cd4240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,6 +132,7 @@ fn main() -> Result<()> { messages_area, ); })?; + let mut player_moved = false; if event::poll(std::time::Duration::from_millis(FRAME_LENGTH))? { if let event::Event::Key(key) = event::read()? { if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('v') { @@ -149,10 +150,22 @@ fn main() -> Result<()> { } if key.kind == KeyEventKind::Press { let new_pos = match key.code { - KeyCode::Left => { game.move_player(-1, 0) } - KeyCode::Right => { game.move_player(1, 0) } - KeyCode::Up => { game.move_player(0, -1) } - KeyCode::Down => { game.move_player(0, 1) } + KeyCode::Left => { + player_moved = true; + game.move_player(-1, 0) + } + KeyCode::Right => { + player_moved = true; + game.move_player(1, 0) + } + KeyCode::Up => { + player_moved = true; + game.move_player(0, -1) + } + KeyCode::Down => { + player_moved = true; + game.move_player(0, 1) + } _ => { (0, 0) } }; if !game.player_fights_monster() { @@ -163,7 +176,7 @@ fn main() -> Result<()> { } } } - game.update_level(ticks); + game.update_level(ticks, player_moved); if game.get_game_state() != GameState::RUNNING { break; }