Something went wrong. Try again.
A video game where you play as a misaligned AI, deceiving and building power. An experiment in spec-driven development.
Something went wrong. Try again.
4.3 kB · 136 lines
Rust
at commit df80fa89
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137use crossterm::event::{KeyCode, KeyEvent};
#[derive(Debug, Clone, PartialEq)]pub enum Command { Quit, MoveUp, MoveDown, MoveLeft, MoveRight, EnterBuild, ExitBuild, CursorUp, CursorDown, CursorLeft, CursorRight, CycleCategory, CycleItemNext, CycleItemPrev, PlaceTile, DemolishTile, OpenResearch, ResearchUp, ResearchDown, ResearchBuy, ResearchClose, NextWave, Wait, // Defend phase controls SpeedUp, SpeedDown, TogglePause, // Title screen AnyKey, // World map OpenWorldMap, WorldMapUp, WorldMapDown, WorldMapSelect, WorldMapClose, // Minion hiring OpenHireMenu, HireUp, HireDown, HireSelect, HireClose,}
pub fn handle_key( key: KeyEvent, build_mode: bool, research_menu: bool, defend_phase: bool, world_map: bool, hire_menu: bool,) -> Option<Command> { // Research menu keys if research_menu { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::ResearchUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::ResearchDown), KeyCode::Enter => Some(Command::ResearchBuy), KeyCode::Esc => Some(Command::ResearchClose), _ => None, }; }
// Hire menu keys if hire_menu { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::HireUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::HireDown), KeyCode::Enter => Some(Command::HireSelect), KeyCode::Esc | KeyCode::Char('f') => Some(Command::HireClose), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; }
// World map keys if world_map { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::WorldMapUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::WorldMapDown), KeyCode::Enter => Some(Command::WorldMapSelect), KeyCode::Esc | KeyCode::Char('m') => Some(Command::WorldMapClose), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; }
// Build mode keys if build_mode { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::CursorUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::CursorDown), KeyCode::Left => Some(Command::CursorLeft), KeyCode::Right | KeyCode::Char('l') => Some(Command::CursorRight), KeyCode::Tab => Some(Command::CycleCategory), KeyCode::Char('n') => Some(Command::CycleItemNext), KeyCode::Char('p') => Some(Command::CycleItemPrev), KeyCode::Char(' ') => Some(Command::PlaceTile), KeyCode::Char('x') => Some(Command::DemolishTile), KeyCode::Char('f') => Some(Command::OpenHireMenu), KeyCode::Esc => Some(Command::ExitBuild), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; }
// Defend phase keys (limited controls) if defend_phase { return match key.code { KeyCode::Char('+') | KeyCode::Char('=') => Some(Command::SpeedUp), KeyCode::Char('-') => Some(Command::SpeedDown), KeyCode::Char(' ') | KeyCode::Char('p') => Some(Command::TogglePause), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; }
// Normal (build) mode keys match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::MoveUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::MoveDown), KeyCode::Left | KeyCode::Char('h') => Some(Command::MoveLeft), KeyCode::Right | KeyCode::Char('l') => Some(Command::MoveRight), KeyCode::Char('b') => Some(Command::EnterBuild), KeyCode::Char('r') => Some(Command::OpenResearch), KeyCode::Char('f') => Some(Command::OpenHireMenu), KeyCode::Char('m') => Some(Command::OpenWorldMap), KeyCode::Char(' ') => Some(Command::NextWave), KeyCode::Char('.') => Some(Command::Wait), KeyCode::Char('q') => Some(Command::Quit), _ => Some(Command::AnyKey), }}