Created
May 7, 2023 20:05
-
-
Save kluzny/bca73918f128eaae4e5da3ffb3a6b160 to your computer and use it in GitHub Desktop.
A bevy system for the konami code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::input::keyboard::KeyboardInput; | |
use bevy::prelude::{EventReader, KeyCode, ResMut, Resource}; | |
use std::process::exit; | |
#[derive(Resource)] | |
pub struct KonamiCombo(pub usize); | |
pub fn keyboard_events( | |
mut key_evr: EventReader<KeyboardInput>, | |
mut konami_combo: ResMut<KonamiCombo>, | |
) { | |
use bevy::input::ButtonState; | |
for ev in key_evr.iter() { | |
match ev.state { | |
ButtonState::Pressed => { | |
// println!("Key press: {:?} ({})", ev.key_code, ev.scan_code); | |
} | |
ButtonState::Released => { | |
// println!("Key release: {:?} ({})", ev.key_code, ev.scan_code); | |
if ev.key_code.unwrap() == KONAMI_CODE[konami_combo.0] { | |
konami_combo.0 += 1; | |
} else { | |
konami_combo.0 = 0; | |
} | |
} | |
} | |
if konami_combo.0 == KONAMI_CODE.len() - 1 { | |
println!("You Win"); | |
exit(0); // TODO: switch to a 'win' AppState | |
} | |
} | |
} | |
// ↑↑↓↓←→←→BA Start Select | |
const KONAMI_CODE: [KeyCode; 12] = [ | |
KeyCode::Up, | |
KeyCode::Up, | |
KeyCode::Down, | |
KeyCode::Down, | |
KeyCode::Left, | |
KeyCode::Right, | |
KeyCode::Left, | |
KeyCode::Right, | |
KeyCode::B, | |
KeyCode::A, | |
KeyCode::Return, | |
KeyCode::RShift, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment