use crate::controls::direction::Direction;
use crate::controls::main_menu::{ENTER_KEYS, NEXT_KEYS, PREVIOUS_KEYS};
use crate::game_logic::state::{GameOverMenu, GameState, GameStatus};
use crossterm::event;
use crossterm::event::{KeyCode, KeyEventKind};
use std::sync::{Arc, RwLock};
pub const QUIT_KEYS: [KeyCode; 2] = [KeyCode::Char('q'), KeyCode::Char('Q')];
pub const START_KEYS: [KeyCode; 2] = [KeyCode::Char('r'), KeyCode::Char('R')];
pub const MAIN_MENU_KEYS: [KeyCode; 2] = [KeyCode::Char('m'), KeyCode::Char('M')];
pub const PAUSE_KEYS: [KeyCode; 4] = [
KeyCode::Char('p'),
KeyCode::Char('P'),
KeyCode::Char(' '),
KeyCode::Home,
];
pub const RESET_KEYS: [KeyCode; 2] = [KeyCode::Char('r'), KeyCode::Char('R')];
pub fn playing_input_loop(direction: &Arc<RwLock<Direction>>, gs: &Arc<RwLock<GameState>>) {
loop {
if let Ok(event::Event::Key(key)) = event::read() {
if key.kind == KeyEventKind::Press {
let mut gs_guard = gs.write().unwrap();
if let GameStatus::GameOver(selection) = gs_guard.status {
if NEXT_KEYS.contains(&key.code) {
gs_guard.status = GameStatus::GameOver(selection.next());
} else if PREVIOUS_KEYS.contains(&key.code) {
gs_guard.status = GameStatus::GameOver(selection.previous());
} else if ENTER_KEYS.contains(&key.code) {
match selection {
GameOverMenu::Restart => gs_guard.status = GameStatus::Restarting,
GameOverMenu::Menu => {
gs_guard.status = GameStatus::Menu;
break;
}
GameOverMenu::Quit => {
gs_guard.status = GameStatus::ByeBye;
break;
}
}
}
}
if PAUSE_KEYS.contains(&key.code) {
if gs_guard.status == GameStatus::Playing {
gs_guard.status = GameStatus::Paused;
} else if gs_guard.status == GameStatus::Paused {
gs_guard.status = GameStatus::Playing;
}
} else if QUIT_KEYS.contains(&key.code) {
gs_guard.status = GameStatus::ByeBye;
break;
} else if MAIN_MENU_KEYS.contains(&key.code) {
gs_guard.status = GameStatus::Menu;
break;
} else if RESET_KEYS.contains(&key.code) {
gs_guard.status = GameStatus::Restarting;
} else {
let direction_input = match key.code {
KeyCode::Left => Some(Direction::Left),
KeyCode::Right => Some(Direction::Right),
KeyCode::Down => Some(Direction::Down),
KeyCode::Up => Some(Direction::Up),
_ => None,
};
if let Some(dir) = direction_input {
*direction.write().unwrap() = dir;
}
}
}
}
}
}