summaryrefslogtreecommitdiff
path: root/src/state.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-18 17:45:28 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-18 17:45:28 +0200
commit5009074b5ba323e5312e999d766a01120990a61e (patch)
treecaf5c02671bd36b376be0d6d61bc9817d072d9d4 /src/state.rs
parent80485b855c5628adc2fa619de21164ded1a0cae1 (diff)
Tests comparing against a replay, and fixing the found bugs
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs31
1 files changed, 15 insertions, 16 deletions
diff --git a/src/state.rs b/src/state.rs
index 99239ae..e05a884 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -86,7 +86,12 @@ impl GameState {
}
}
- self.players[player_index].set_next_position_x();
+ let turning = match command {
+ TurnLeft | TurnRight => true,
+ _ => false,
+ };
+
+ self.players[player_index].set_next_position_x(turning);
}
fn update_player_collisions(&mut self) {
@@ -129,10 +134,10 @@ impl GameState {
if player.speed > SPEED_0 {
result.push(Command::Decelerate);
}
- if player.position.y > 0 {
+ if player.position.y > MIN_Y {
result.push(Command::TurnLeft);
}
- if player.position.y < HEIGHT - 1 {
+ if player.position.y < MAX_Y - 1 {
result.push(Command::TurnRight);
}
if player.boosts > 0 {
@@ -177,13 +182,11 @@ impl Player {
}
fn turn_left(&mut self) {
- debug_assert!(self.position.y > 0);
- self.next_position.y = self.position.y.saturating_sub(1);
+ self.next_position.y = self.position.y.saturating_sub(1).max(MIN_Y);
}
fn turn_right(&mut self) {
- debug_assert!(self.position.y < HEIGHT - 1);
- self.next_position.y = self.position.y.saturating_add(1);
+ self.next_position.y = self.position.y.saturating_add(1).min(MAX_Y);
}
fn boost(&mut self) {
@@ -200,11 +203,11 @@ impl Player {
}
}
- fn set_next_position_x(&mut self) {
- if self.position.y == self.next_position.y {
- self.next_position.x = self.position.x.saturating_add(self.speed);
- } else {
+ fn set_next_position_x(&mut self, turning: bool) {
+ if turning {
self.next_position.x = self.position.x.saturating_add(self.speed.saturating_sub(1));
+ } else {
+ self.next_position.x = self.position.x.saturating_add(self.speed);
}
}
@@ -220,11 +223,7 @@ impl Player {
powerup_boosts: &[Position],
finish_lines: &[Position],
) {
- let start_x = if self.position.y == self.next_position.y {
- self.position.x.saturating_add(1)
- } else {
- self.position.x
- };
+ let start_x = self.position.x.saturating_add(1);
for x in start_x..=self.next_position.x {
let position = Position {
x,