use std::fs;
use std::path::PathBuf;
use bevy::app::{Startup, Update};
use bevy::prelude::*;
use bevy_ineffable::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(IneffablePlugin)
.register_input_action::<CheatCodes>()
.add_systems(Startup, init)
.add_systems(Update, listen_for_cheat_codes)
.run();
}
#[derive(Debug, InputAction)]
pub enum CheatCodes {
#[ineffable(pulse)]
Password,
#[ineffable(pulse)]
KonamiCode,
}
pub fn init(mut ineffable: IneffableCommands) {
ineffable.set_config(&load_keybindings_from_file());
}
fn listen_for_cheat_codes(bindings: Res<Ineffable>) {
if bindings.just_pulsed(ineff!(CheatCodes::Password)) {
println!("Hello world! Password accepted.");
}
if bindings.just_pulsed(ineff!(CheatCodes::KonamiCode)) {
println!("Something spectacular happens!");
}
}
#[must_use]
fn load_keybindings_from_file() -> InputConfig {
let path = PathBuf::new()
.join("examples/assets")
.join("cheat_code.ron");
let data = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Unable to read InputConfig file at path: {path:?}"));
ron::de::from_str::<InputConfig>(&data).expect("Unable to deserialise InputConfig")
}