diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/consts.rs | 3 | ||||
-rw-r--r-- | src/state.rs | 31 |
2 files changed, 18 insertions, 16 deletions
diff --git a/src/consts.rs b/src/consts.rs index 6ebc9af..69c9fe8 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -7,5 +7,8 @@ pub const SPEED_BOOST: usize = 15; pub const BOOST_DURATION: usize = 5; +pub const MIN_Y: usize = 1; pub const HEIGHT: usize = 4; +pub const MAX_Y: usize = MIN_Y + HEIGHT; + pub const WIDTH: usize = 1500; 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, |