diff options
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/state.rs b/src/state.rs index 1dfa21d..6285e75 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,6 +1,7 @@ use crate::command::Command; use crate::consts::*; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum GameStatus { Continue, PlayerOneWon, @@ -8,6 +9,7 @@ pub enum GameStatus { Draw, // Until I add score I guess } +#[derive(Debug, Clone, PartialEq, Eq)] pub struct GameState { pub status: GameStatus, pub players: [Player; 2], @@ -17,6 +19,7 @@ pub struct GameState { pub finish_lines: Vec<Position>, } +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Player { pub position: Position, pub next_position: Position, @@ -110,6 +113,31 @@ impl GameState { ); } } + + pub fn valid_moves(&self, player_index: usize) -> Vec<Command> { + let player = &self.players[player_index]; + let mut result = Vec::with_capacity(7); + result.push(Command::Nothing); + if player.speed < SPEED_4 { + result.push(Command::Accelerate); + } + if player.speed > SPEED_0 { + result.push(Command::Decelerate); + } + if player.position.y > 0 { + result.push(Command::TurnLeft); + } + if player.position.y < WIDTH { + result.push(Command::TurnRight); + } + if player.boosts > 0 { + result.push(Command::UseBoost); + } + if player.oils > 0 { + result.push(Command::UseOil); + } + result + } } impl Player { |