moving monsters I

This commit is contained in:
Joachim Lusiardi 2023-12-22 22:53:58 +01:00
parent de5ea76913
commit cca9e8f285
4 changed files with 20 additions and 9 deletions

View File

@ -190,11 +190,11 @@ impl Game {
} }
} }
/// updates the player's current level. This will remove collected artifacts and dead monsters. /// 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_pos = &self.player.get_immutable_position();
let player_level = player_pos.get_level(); let player_level = player_pos.get_level();
let level = &mut self.levels[player_level]; let level = &mut self.levels[player_level];
level.update(); level.update(ticks);
} }
} }

View File

@ -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() { for (index, a) in &mut self.artifacts.iter().enumerate() {
if a.was_collected() { if a.was_collected() {
self.artifacts.remove(index); self.artifacts.remove(index);
@ -165,6 +165,15 @@ impl Level {
break; 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);
}
} }
} }

View File

@ -38,6 +38,7 @@ fn main() -> Result<()> {
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?; let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?; terminal.clear()?;
let mut ticks: u64 = 0;
loop { loop {
terminal.draw(|frame| { terminal.draw(|frame| {
let mut area = frame.size(); let mut area = frame.size();
@ -67,19 +68,21 @@ fn main() -> Result<()> {
height: 25, height: 25,
}; };
frame.render_widget( 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_name(),
game.get_player().get_life(), game.get_player().get_life(),
game.get_player().get_max_life(), game.get_player().get_max_life(),
game.get_player().get_experience(), game.get_player().get_experience(),
game.get_player().get_gold(), game.get_player().get_gold(),
game.get_player().get_immutable_position().get_level())) game.get_player().get_immutable_position().get_level(),
ticks
))
.white() .white()
.on_blue(), .on_blue(),
stats_area, 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 let event::Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') { if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
break; break;
@ -97,13 +100,14 @@ fn main() -> Result<()> {
game.move_player(new_pos.0, new_pos.1); game.move_player(new_pos.0, new_pos.1);
} }
game.player_collects_artifact(); game.player_collects_artifact();
game.update_level();
if game.get_game_state() != GameState::RUNNING { if game.get_game_state() != GameState::RUNNING {
break; break;
} }
} }
} }
} }
game.update_level(ticks);
ticks+=1;
} }
loop { loop {
let _ = terminal.draw(|frame| { let _ = terminal.draw(|frame| {

View File

@ -8,7 +8,6 @@ pub trait Monster {
fn decrease_life(&mut self, by: usize); fn decrease_life(&mut self, by: usize);
fn get_immutable_position(&self) -> &Position; fn get_immutable_position(&self) -> &Position;
fn get_experience_gain(&self) -> usize; fn get_experience_gain(&self) -> usize;
#[cfg(test)]
fn get_position(&mut self) -> &mut Position; fn get_position(&mut self) -> &mut Position;
#[cfg(test)] #[cfg(test)]
fn get_life(&self) -> usize; fn get_life(&self) -> usize;
@ -26,7 +25,6 @@ macro_rules! default_monster {
fn get_immutable_position(&self) -> &Position { fn get_immutable_position(&self) -> &Position {
&self.position &self.position
} }
#[cfg(test)]
fn get_position(&mut self) -> &mut Position { fn get_position(&mut self) -> &mut Position {
&mut self.position &mut self.position
} }