summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-11 11:41:57 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-11 11:41:57 +0200
commit8e8ffe4b5d516e01ee7480e6989c3a309dee8b91 (patch)
treebd7b96214b76725b7a6c35f4e3f9b19759c82788
parent5277e8b28bbe77d77986848af4b21c619473bb11 (diff)
Debugging assertions on commands and use of saturating sub / add
-rw-r--r--src/state.rs32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/state.rs b/src/state.rs
index 8d2bb50..ecbd408 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -73,10 +73,11 @@ impl GameState {
TurnRight => self.players[player_index].turn_right(),
UseBoost => self.players[player_index].boost(),
UseOil => {
- self.players[player_index].oils -= 1;
+ debug_assert!(self.players[player_index].oils > 0);
+ self.players[player_index].oils = self.players[player_index].oils.saturating_sub(1);
let player_position = self.players[player_index].position;
self.obstacles.push(Position {
- x: player_position.x - 1,
+ x: player_position.x.saturating_sub(1),
y: player_position.y,
});
}
@@ -95,9 +96,9 @@ impl GameState {
let same_x_after = self.players[0].next_position.x == self.players[1].next_position.x;
if same_lanes_before && same_lanes_after && first_passing_second {
- self.players[0].next_position.x = self.players[1].next_position.x - 1;
+ self.players[0].next_position.x = self.players[1].next_position.x.saturating_sub(1);
} else if same_lanes_before && same_lanes_after && second_passing_first {
- self.players[1].next_position.x = self.players[0].next_position.x - 1;
+ self.players[1].next_position.x = self.players[0].next_position.x.saturating_sub(1);
} else if same_lanes_after && same_x_after {
self.players[0].bonked_while_changing_lanes();
self.players[1].bonked_while_changing_lanes();
@@ -128,7 +129,7 @@ impl GameState {
if player.position.y > 0 {
result.push(Command::TurnLeft);
}
- if player.position.y < HEIGHT {
+ if player.position.y < HEIGHT - 1 {
result.push(Command::TurnRight);
}
if player.boosts > 0 {
@@ -173,17 +174,20 @@ impl Player {
}
fn turn_left(&mut self) {
- self.next_position.y = self.position.y - 1;
+ debug_assert!(self.position.y > 0);
+ self.next_position.y = self.position.y.saturating_sub(1);
}
fn turn_right(&mut self) {
- self.next_position.y = self.position.y + 1;
+ debug_assert!(self.position.y < HEIGHT - 1);
+ self.next_position.y = self.position.y.saturating_add(1);
}
fn boost(&mut self) {
+ debug_assert!(self.boosts > 0);
self.speed = SPEED_BOOST;
self.boost_remaining = BOOST_DURATION;
- self.boosts -= 1;
+ self.boosts = self.boosts.saturating_sub(1);
}
fn tick_boost(&mut self) {
@@ -195,15 +199,15 @@ impl Player {
fn set_next_position_x(&mut self) {
if self.position.y == self.next_position.y {
- self.next_position.x = self.position.x + self.speed;
+ self.next_position.x = self.position.x.saturating_add(self.speed);
} else {
- self.next_position.x = self.position.x + self.speed - 1;
+ self.next_position.x = self.position.x.saturating_add(self.speed.saturating_sub(1));
}
}
fn bonked_while_changing_lanes(&mut self) {
self.next_position.y = self.position.y;
- self.next_position.x = self.position.x + self.speed - 1;
+ self.next_position.x = self.position.x.saturating_add(self.speed.saturating_sub(1));
}
fn move_along(
@@ -214,7 +218,7 @@ impl Player {
finish_lines: &[Position],
) {
let start_x = if self.position.y == self.next_position.y {
- self.position.x + 1
+ self.position.x.saturating_add(1)
} else {
self.position.x
};
@@ -227,10 +231,10 @@ impl Player {
self.decelerate_from_obstacle();
}
if powerup_oils.contains(&position) {
- self.oils += 1;
+ self.oils = self.oils.saturating_add(1);
}
if powerup_boosts.contains(&position) {
- self.boosts += 1;
+ self.boosts = self.boosts.saturating_add(1);
}
if finish_lines.contains(&position) {
self.finished = true;