summaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2019-05-20 14:49:07 +0200
committerJustin Worthe <justin@worthe-it.co.za>2019-05-20 14:49:07 +0200
commitb93a9c643485c720a0711ddaf90872b7c6f006c8 (patch)
treeaa3504a02bd713314db260b44a84c8544e999668 /src/game
parentb3d6b7cb77a660fc8a8e96645627da16c6b7c059 (diff)
Extracted functionality for clearing out dead worms
Diffstat (limited to 'src/game')
-rw-r--r--src/game/player.rs155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/game/player.rs b/src/game/player.rs
index acfc494..3a9d36c 100644
--- a/src/game/player.rs
+++ b/src/game/player.rs
@@ -44,6 +44,161 @@ impl Player {
.sum()
}
+ pub fn clear_dead_worms(&mut self) {
+ for worm_index in (0..self.worms.len()).rev() {
+ if self.worms[worm_index].health <= 0 {
+ self.worms.remove(worm_index);
+ if self.active_worm >= worm_index {
+ self.active_worm = if self.active_worm > 0 {
+ self.active_worm - 1
+ } else if self.worms.len() > 0 {
+ self.worms.len() - 1
+ } else {
+ 0
+ };
+ }
+ }
+ }
+ }
// TODO: Cycle to next worm
}
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn clear_dead_worms_after_active_worm() {
+ let mut worms = ArrayVec::new();
+ worms.push(Worm {
+ id: 1,
+ health: 50,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ worms.push(Worm {
+ id: 2,
+ health: 10,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ worms.push(Worm {
+ id: 3,
+ health: -2,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ let mut player = Player {
+ active_worm: 1,
+ worms: worms
+ };
+
+ player.clear_dead_worms();
+
+ assert_eq!(2, player.worms.len());
+ assert_eq!(1, player.active_worm);
+
+ assert_eq!(1, player.worms[0].id);
+ assert_eq!(2, player.worms[1].id);
+ }
+
+ #[test]
+ fn clear_dead_worms_before_active_worm() {
+ let mut worms = ArrayVec::new();
+ worms.push(Worm {
+ id: 1,
+ health: 0,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ worms.push(Worm {
+ id: 2,
+ health: 10,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ worms.push(Worm {
+ id: 3,
+ health: 2,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ let mut player = Player {
+ active_worm: 1,
+ worms: worms
+ };
+
+ player.clear_dead_worms();
+
+ assert_eq!(2, player.worms.len());
+ assert_eq!(0, player.active_worm);
+
+ assert_eq!(2, player.worms[0].id);
+ assert_eq!(3, player.worms[1].id);
+ }
+
+ #[test]
+ fn clear_dead_worms_before_active_worm_wrapping() {
+ let mut worms = ArrayVec::new();
+ worms.push(Worm {
+ id: 1,
+ health: 0,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ worms.push(Worm {
+ id: 2,
+ health: 10,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ worms.push(Worm {
+ id: 3,
+ health: 2,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ let mut player = Player {
+ active_worm: 0,
+ worms: worms
+ };
+
+ player.clear_dead_worms();
+
+ assert_eq!(2, player.worms.len());
+ assert_eq!(1, player.active_worm);
+
+ assert_eq!(2, player.worms[0].id);
+ assert_eq!(3, player.worms[1].id);
+ }
+
+ #[test]
+ fn clear_last_dead_worm() {
+ let mut worms = ArrayVec::new();
+ worms.push(Worm {
+ id: 1,
+ health: -10,
+ position: Point2d::new(0, 0),
+ weapon_damage: 5,
+ weapon_range: 5
+ });
+ let mut player = Player {
+ active_worm: 0,
+ worms: worms
+ };
+
+ player.clear_dead_worms();
+
+ assert_eq!(0, player.worms.len());
+ // active worm is undefined in this case, but clearing the worms must not panic.
+ }
+}