diff options
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/src/state.rs b/src/state.rs index 7cc1ab4..be42228 100644 --- a/src/state.rs +++ b/src/state.rs @@ -12,6 +12,8 @@ pub enum GameStatus { Draw, // Until I add score I guess } +// TODO: Maintain sorted vecs for these BTrees? Do the range counts +// with binary searches to find indices only. #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct GameState { pub status: GameStatus, @@ -43,27 +45,26 @@ pub struct GameStateUpdateEvents { } #[derive(Debug, Clone, Default)] pub struct GameStatePlayerUpdateEvents { - pub boosts_collected: u16, pub boosts_used: u16, + pub boosts_collected: u16, pub boosts_maintained: u8, pub mud_hit: u16, pub oil_collected: u16, pub distance_travelled: u16, - pub rounds_to_finish: Option<u16>, } impl GameState { - pub fn update(&mut self, commands: [Command; 2]) -> GameStateUpdateEvents { + pub fn update(&mut self, commands: [Command; 2], events: &mut GameStateUpdateEvents) { if self.status != GameStatus::Continue { - return GameStateUpdateEvents::default(); + return; } let next_positions = [ - self.do_command(0, &commands[0]), - self.do_command(1, &commands[1]), + self.do_command(0, &commands[0], &mut events.players[0]), + self.do_command(1, &commands[1], &mut events.players[1]), ]; - let next_positions = self.update_player_collisions(next_positions); - self.update_player_travel(next_positions); + let next_positions = self.update_player_collisions(next_positions, events); + self.update_player_travel(next_positions, events); self.status = if self.players[0].finished() && self.players[1].finished() { if self.players[0].speed > self.players[1].speed { @@ -80,9 +81,6 @@ impl GameState { } else { GameStatus::Continue }; - - // TODO - GameStateUpdateEvents::default() } pub fn reset_players_to_start(&mut self) { @@ -96,7 +94,12 @@ impl GameState { } } - fn do_command(&mut self, player_index: usize, command: &Command) -> Position { + fn do_command( + &mut self, + player_index: usize, + command: &Command, + events: &mut GameStatePlayerUpdateEvents, + ) -> Position { use Command::*; self.players[player_index].tick_boost(); let mut next_y = self.players[player_index].position.y; @@ -107,7 +110,10 @@ impl GameState { Decelerate => self.players[player_index].decelerate(), 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(), + UseBoost => { + events.boosts_used += 1; + self.players[player_index].boost(); + } UseOil => { debug_assert!(self.players[player_index].oils > 0); self.players[player_index].oils = self.players[player_index].oils.saturating_sub(1); @@ -133,7 +139,11 @@ impl GameState { } } - fn update_player_collisions(&mut self, next_positions: [Position; 2]) -> [Position; 2] { + fn update_player_collisions( + &mut self, + next_positions: [Position; 2], + _events: &mut GameStateUpdateEvents, + ) -> [Position; 2] { let same_lanes_before = self.players[0].position.y == self.players[1].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 @@ -174,14 +184,24 @@ impl GameState { } } - fn update_player_travel(&mut self, next_positions: [Position; 2]) { - for (player, next_position) in self.players.iter_mut().zip(next_positions.iter()) { + fn update_player_travel( + &mut self, + next_positions: [Position; 2], + events: &mut GameStateUpdateEvents, + ) { + for (i, (player, next_position)) in self + .players + .iter_mut() + .zip(next_positions.iter()) + .enumerate() + { player.move_along( *next_position, &self.muds, &self.oil_spills, &self.powerup_oils, &self.powerup_boosts, + &mut events.players[i], ); } } @@ -290,6 +310,7 @@ impl Player { oil_spills: &BTreeSet<Position>, powerup_oils: &BTreeSet<Position>, powerup_boosts: &BTreeSet<Position>, + events: &mut GameStatePlayerUpdateEvents, ) { let range = ( Included(Position { @@ -302,18 +323,28 @@ impl Player { }), ); - for _ in muds.range(range) { + let mud_hit = muds + .range(range) + .count() + .saturating_add(oil_spills.range(range).count()); + for _ in 0..mud_hit { self.decelerate_from_obstacle(); } - for _ in oil_spills.range(range) { - self.decelerate_from_obstacle(); + + let oil_collected = powerup_oils.range(range).count() as u16; + self.oils = self.oils.saturating_add(oil_collected); + let boosts_collected = powerup_boosts.range(range).count() as u16; + self.boosts = self.boosts.saturating_add(boosts_collected); + + events.mud_hit = events.mud_hit.saturating_add(mud_hit as u16); + events.boosts_collected = events.boosts_collected.saturating_add(boosts_collected); + events.oil_collected = events.oil_collected.saturating_add(oil_collected); + events.distance_travelled = events + .distance_travelled + .saturating_add(next_position.x.saturating_sub(self.position.x)); + if self.speed == SPEED_BOOST { + events.boosts_maintained = events.boosts_maintained.saturating_add(1); } - self.oils = self - .oils - .saturating_add(powerup_oils.range(range).count() as u16); - self.boosts = self - .boosts - .saturating_add(powerup_boosts.range(range).count() as u16); self.position = next_position; } |