summaryrefslogtreecommitdiff
path: root/src/game.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.rs')
-rw-r--r--src/game.rs35
1 files changed, 17 insertions, 18 deletions
diff --git a/src/game.rs b/src/game.rs
index 5f6ab33..769de73 100644
--- a/src/game.rs
+++ b/src/game.rs
@@ -228,21 +228,12 @@ impl GameBoard {
}
}
- for player in &mut self.players {
- player.clear_dead_worms();
- }
+ self.clear_dead_worms();
self.players[0].active_worm = json.active_worm_index().unwrap_or(0);
self.players[1].active_worm = json.opponent_active_worm_index().unwrap_or(0);
self.round += 1;
debug_assert_eq!(json.current_round, self.round);
-
- self.occupied_cells = self
- .players
- .iter()
- .flat_map(|p| p.worms.iter())
- .map(|w| w.position)
- .collect();
}
pub fn simulate(&mut self, moves: [Command; 2]) {
@@ -263,8 +254,9 @@ impl GameBoard {
let actions = self.identify_actions(moves);
self.simulate_shoots(actions);
+ self.clear_dead_worms();
+
for player in &mut self.players {
- player.clear_dead_worms();
player.next_active_worm();
}
@@ -296,6 +288,7 @@ impl GameBoard {
self.players
.iter_mut()
.flat_map(|p| p.worms.iter_mut())
+ .filter(|w| w.health > 0)
.for_each(|ref mut w| {
w.rounds_until_unfrozen = w.rounds_until_unfrozen.saturating_sub(1)
});
@@ -374,9 +367,6 @@ impl GameBoard {
p.y
);
- self.occupied_cells.remove(&worm.position);
- self.occupied_cells.insert(p);
-
worm.position = p;
self.powerups.retain(|power| {
@@ -451,7 +441,6 @@ impl GameBoard {
weapon_damage * ATTACK_SCORE_MULTIPLIER;
if target_worm.health <= 0 {
self.players[player_index].moves_score -= KILL_SCORE;
- self.occupied_cells.remove(&target_worm.position);
}
}
@@ -466,7 +455,6 @@ impl GameBoard {
weapon_damage * ATTACK_SCORE_MULTIPLIER;
if target_worm.health <= 0 {
self.players[player_index].moves_score += KILL_SCORE;
- self.occupied_cells.remove(&target_worm.position);
}
}
}
@@ -547,7 +535,6 @@ impl GameBoard {
weapon_damage * ATTACK_SCORE_MULTIPLIER;
if target_worm.health <= 0 {
self.players[player_index].moves_score -= KILL_SCORE;
- self.occupied_cells.remove(&target_worm.position);
}
continue 'players_loop;
}
@@ -564,7 +551,6 @@ impl GameBoard {
weapon_damage * ATTACK_SCORE_MULTIPLIER;
if target_worm.health <= 0 {
self.players[player_index].moves_score += KILL_SCORE;
- self.occupied_cells.remove(&target_worm.position);
}
continue 'players_loop;
@@ -581,6 +567,19 @@ impl GameBoard {
}
}
+ fn clear_dead_worms(&mut self) {
+ for player in &mut self.players {
+ player.clear_dead_worms();
+ }
+
+ self.occupied_cells = self
+ .players
+ .iter()
+ .flat_map(|p| p.worms.iter())
+ .map(|w| w.position)
+ .collect();
+ }
+
pub fn opponent(player_index: usize) -> usize {
(player_index + 1) % 2
}