summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-20 19:32:24 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-20 19:32:24 +0200
commitad2190963e8c6ccece6259212d2143981448535d (patch)
treec88f2446a7ef2fd6c193c0b25c03ab6257bc9194
parent7162aa803abd69a24f9e85c6681561d97fa79c15 (diff)
Removed weird temporary variable from state
-rw-r--r--src/global_json.rs1
-rw-r--r--src/json.rs2
-rw-r--r--src/state.rs97
3 files changed, 57 insertions, 43 deletions
diff --git a/src/global_json.rs b/src/global_json.rs
index 101a085..ce440ea 100644
--- a/src/global_json.rs
+++ b/src/global_json.rs
@@ -140,7 +140,6 @@ impl JsonPlayer {
fn to_player(&self) -> Result<Player> {
Ok(Player {
position: self.position.to_position(),
- next_position: self.position.to_position(),
speed: self.speed,
boost_remaining: self.boost_counter,
oils: self
diff --git a/src/json.rs b/src/json.rs
index 6cdc48f..d610c0f 100644
--- a/src/json.rs
+++ b/src/json.rs
@@ -147,7 +147,6 @@ impl JsonPlayer {
fn to_player(&self) -> Result<Player> {
Ok(Player {
position: self.position.to_position(),
- next_position: self.position.to_position(),
speed: self.speed,
boost_remaining: self.boost_counter,
oils: self
@@ -172,7 +171,6 @@ impl JsonOpponent {
fn to_player(&self) -> Player {
Player {
position: self.position.to_position(),
- next_position: self.position.to_position(),
speed: self.speed,
boost_remaining: 0,
oils: 0,
diff --git a/src/state.rs b/src/state.rs
index 6590d5e..9b90f89 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -25,7 +25,6 @@ pub struct GameState {
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Player {
pub position: Position,
- pub next_position: Position,
pub speed: u16,
pub boost_remaining: u8,
pub oils: u16,
@@ -45,10 +44,12 @@ impl GameState {
return;
}
- self.do_command(0, &commands[0]);
- self.do_command(1, &commands[1]);
- self.update_player_collisions();
- self.update_player_travel();
+ let next_positions = [
+ self.do_command(0, &commands[0]),
+ self.do_command(1, &commands[1]),
+ ];
+ let next_positions = self.update_player_collisions(next_positions);
+ self.update_player_travel(next_positions);
self.status = if self.players[0].finished && self.players[1].finished {
if self.players[0].speed > self.players[1].speed {
@@ -79,16 +80,17 @@ impl GameState {
}
}
- fn do_command(&mut self, player_index: usize, command: &Command) {
+ fn do_command(&mut self, player_index: usize, command: &Command) -> Position {
use Command::*;
self.players[player_index].tick_boost();
+ let mut next_y = self.players[player_index].position.y;
match command {
Nothing => {}
Accelerate => self.players[player_index].accelerate(),
Decelerate => self.players[player_index].decelerate(),
- TurnLeft => self.players[player_index].turn_left(),
- TurnRight => self.players[player_index].turn_right(),
+ TurnLeft => next_y = next_y.saturating_sub(1).max(MIN_Y),
+ TurnRight => next_y = next_y.saturating_add(1).min(MAX_Y),
UseBoost => self.players[player_index].boost(),
UseOil => {
debug_assert!(self.players[player_index].oils > 0);
@@ -108,31 +110,58 @@ impl GameState {
_ => false,
};
- self.players[player_index].set_next_position_x(turning);
+ let next_x = self.players[player_index].next_position_x(turning);
+ Position {
+ x: next_x,
+ y: next_y,
+ }
}
- fn update_player_collisions(&mut self) {
+ fn update_player_collisions(&mut self, next_positions: [Position; 2]) -> [Position; 2] {
let same_lanes_before = self.players[0].position.y == self.players[1].position.y;
- let same_lanes_after = self.players[0].next_position.y == self.players[1].next_position.y;
+ let same_lanes_after = next_positions[0].y == next_positions[1].y;
let first_passing_second = self.players[0].position.x < self.players[1].position.x
- && self.players[0].next_position.x >= self.players[1].next_position.x;
+ && next_positions[0].x >= next_positions[1].x;
let second_passing_first = self.players[1].position.x < self.players[0].position.x
- && self.players[1].next_position.x >= self.players[0].next_position.x;
- let same_x_after = self.players[0].next_position.x == self.players[1].next_position.x;
+ && next_positions[1].x >= next_positions[0].x;
+ let same_x_after = next_positions[0].x == next_positions[1].x;
if same_lanes_before && same_lanes_after && first_passing_second {
- self.players[0].next_position.x = self.players[1].next_position.x.saturating_sub(1);
+ [
+ Position {
+ y: next_positions[0].y,
+ x: next_positions[1].x.saturating_sub(1),
+ },
+ next_positions[1],
+ ]
} else if same_lanes_before && same_lanes_after && second_passing_first {
- self.players[1].next_position.x = self.players[0].next_position.x.saturating_sub(1);
+ [
+ next_positions[0],
+ Position {
+ y: next_positions[1].y,
+ x: next_positions[0].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();
+ [
+ Position {
+ y: self.players[0].position.y,
+ x: self.players[0].next_position_x(true),
+ },
+ Position {
+ y: self.players[1].position.y,
+ x: self.players[1].next_position_x(true),
+ },
+ ]
+ } else {
+ next_positions
}
}
- fn update_player_travel(&mut self) {
- for player in &mut self.players {
+ fn update_player_travel(&mut self, next_positions: [Position; 2]) {
+ for (player, next_position) in self.players.iter_mut().zip(next_positions.iter()) {
player.move_along(
+ *next_position,
&self.obstacles,
&self.powerup_oils,
&self.powerup_boosts,
@@ -197,14 +226,6 @@ impl Player {
self.boost_remaining = 0;
}
- fn turn_left(&mut self) {
- self.next_position.y = self.position.y.saturating_sub(1).max(MIN_Y);
- }
-
- fn turn_right(&mut self) {
- self.next_position.y = self.position.y.saturating_add(1).min(MAX_Y);
- }
-
fn boost(&mut self) {
debug_assert!(self.boosts > 0);
self.speed = SPEED_BOOST;
@@ -219,21 +240,17 @@ impl Player {
}
}
- fn set_next_position_x(&mut self, turning: bool) {
+ fn next_position_x(&mut self, turning: bool) -> u16 {
if turning {
- self.next_position.x = self.position.x.saturating_add(self.speed.saturating_sub(1));
+ self.position.x.saturating_add(self.speed.saturating_sub(1))
} else {
- self.next_position.x = self.position.x.saturating_add(self.speed);
+ self.position.x.saturating_add(self.speed)
}
}
- fn bonked_while_changing_lanes(&mut self) {
- self.next_position.y = self.position.y;
- self.next_position.x = self.position.x.saturating_add(self.speed.saturating_sub(1));
- }
-
fn move_along(
&mut self,
+ next_position: Position,
obstacles: &BTreeSet<Position>,
powerup_oils: &BTreeSet<Position>,
powerup_boosts: &BTreeSet<Position>,
@@ -241,12 +258,12 @@ impl Player {
) {
let range = (
Included(Position {
- y: self.next_position.y,
+ y: next_position.y,
x: self.position.x.saturating_add(1),
}),
Excluded(Position {
- y: self.next_position.y,
- x: self.next_position.x.saturating_add(1),
+ y: next_position.y,
+ x: next_position.x.saturating_add(1),
}),
);
@@ -264,6 +281,6 @@ impl Player {
self.finished = true;
}
- self.position = self.next_position;
+ self.position = next_position;
}
}