From 4978b4f27d7a0e6058d04e7184600bf835070a8b Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 25 Apr 2019 17:21:09 +0200 Subject: Implement move and dig commands --- src/game.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/game.rs') diff --git a/src/game.rs b/src/game.rs index dad72cd..0d57028 100644 --- a/src/game.rs +++ b/src/game.rs @@ -98,7 +98,49 @@ impl GameBoard { } pub fn simulate(&mut self, moves: [Command; 2]) -> SimulationOutcome { + // TODO: Command validation + // TODO: Move collision case + /*if let [Command::Move(x1, y1), Command::Move(x2, y2)] = moves { + + }*/ + + for player_index in 0..moves.len() { + if let Command::Move(x, y) = moves[player_index] { + let worm = self.players[player_index].active_worm_mut(); + worm.position.x = x; + worm.position.y = y; + } + } + + for player_index in 0..moves.len() { + if let Command::Dig(x, y) = moves[player_index] { + if let Some(c) = self.map.at(Point2d::new(x, y)) { + *c = CellType::Air; + } + } + } + + for player_index in 0..moves.len() { + if let Command::Shoot(dir) = moves[player_index] { + // TODO: Shoot + } + } + for player in &mut self.players { + // Remove dead worms + for worm_index in (0..player.worms.len()).rev() { + if player.worms[worm_index].health <= 0 { + player.worms.remove(worm_index); + if player.active_worm >= worm_index { + if player.active_worm > 0 { + player.active_worm -= 1; + } else { + player.active_worm = player.worms.len()-1; + } + } + } + } + // Update the active worm player.active_worm = (player.active_worm + 1) % player.worms.len(); } SimulationOutcome::Continue @@ -111,4 +153,18 @@ impl Player { .iter() .find(|w| w.id == id) } + + fn active_worm_mut(&mut self) -> &mut Worm { + &mut self.worms[self.active_worm] + } +} + +impl Map { + fn at(&mut self, p: Point2d) -> Option<&mut CellType> { + if p.y < 0 || p.x < 0 || p.y as u8 >= self.size || p.x as u8 >= self.size { + None + } else { + Some(&mut self.cells[p.y as usize * self.size as usize + p.x as usize]) + } + } } -- cgit v1.2.3