diff options
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/src/state.rs b/src/state.rs index 78f8267..b898e4b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,5 +1,6 @@ use crate::command::Command; use crate::consts::*; +use std::collections::BTreeSet; use std::rc::Rc; #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] @@ -14,27 +15,27 @@ pub enum GameStatus { pub struct GameState { pub status: GameStatus, pub players: [Player; 2], - pub obstacles: Rc<Vec<Position>>, - pub powerup_oils: Rc<Vec<Position>>, - pub powerup_boosts: Rc<Vec<Position>>, - pub finish_lines: Rc<Vec<Position>>, + pub obstacles: Rc<BTreeSet<Position>>, + pub powerup_oils: Rc<BTreeSet<Position>>, + pub powerup_boosts: Rc<BTreeSet<Position>>, + pub finish_lines: Rc<BTreeSet<Position>>, } #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct Player { pub position: Position, pub next_position: Position, - pub speed: usize, - pub boost_remaining: usize, - pub oils: usize, - pub boosts: usize, + pub speed: u16, + pub boost_remaining: u8, + pub oils: u16, + pub boosts: u16, pub finished: bool, } -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct Position { - pub x: usize, - pub y: usize, + pub y: u8, + pub x: u16, } impl GameState { @@ -93,7 +94,7 @@ impl GameState { self.players[player_index].oils = self.players[player_index].oils.saturating_sub(1); let player_position = self.players[player_index].position; let mut obstacles = (*self.obstacles).clone(); - obstacles.push(Position { + obstacles.insert(Position { x: player_position.x.saturating_sub(1), y: player_position.y, }); @@ -233,10 +234,10 @@ impl Player { fn move_along( &mut self, - obstacles: &[Position], - powerup_oils: &[Position], - powerup_boosts: &[Position], - finish_lines: &[Position], + obstacles: &BTreeSet<Position>, + powerup_oils: &BTreeSet<Position>, + powerup_boosts: &BTreeSet<Position>, + finish_lines: &BTreeSet<Position>, ) { let start_x = self.position.x.saturating_add(1); for x in start_x..=self.next_position.x { |