const UPDATE_MS: u32 = 100; // 10 updates/sec
pub struct MyGame { base: GameBase, player_x: usize, player_y: usize }
impl MyGame {
pub fn new(width: usize, height: usize) -> Self {
let base = GameBase::with_controls("My Game", width, height, b"WASD - Move");
let area_h = base.game_height.saturating_sub(INFO_BAR_HEIGHT);
Self { base, player_x: base.game_width / 2, player_y: area_h / 2 }
}
pub fn set_needs_redraw(&mut self) { self.base.needs_redraw = true; }
pub fn cleanup(&mut self) {}
}
impl AppContext for MyGame {
fn update(&mut self) -> AppAction {
if !self.base.should_update(UPDATE_MS) { return AppAction::Continue; }
// ...advance game state... (only runs while playing)
AppAction::Continue
}
fn handle_keyboard(&mut self, key: u8) -> AppAction {
// F1 pause, SPACE/ENTER start, ESC minimize, Shift+ESC quit:
if let Some(action) = self.base.handle_common_input(key) { return action; }
if self.base.game_over || self.base.game_won {
if GameBase::is_restart_key(key) { self.reset(); }
return AppAction::Continue;
}
if !self.base.paused && !self.base.showing_start_menu {
match key {
b'W' | b'w' => self.player_y = self.player_y.saturating_sub(4),
b'S' | b's' => self.player_y += 4,
b'A' | b'a' => self.player_x = self.player_x.saturating_sub(4),
b'D' | b'd' => self.player_x += 4,
_ => {}
}
}
AppAction::Continue
}
fn handle_mouse(&mut self, _mouse: &MouseState) -> AppAction { AppAction::Continue }
fn needs_redraw(&self) -> bool { true }
}