use qilin::game::context::GameContext;
use qilin::game::game::Game;
use qilin::game::store::{PlayerPrefs, Storable};
use qilin::render::canvas::Canvas;
use std::path::PathBuf;
use std::time::Duration;
use qilin::scene::Scene;
use qilin::types::{GameConfig, TimeStamp, FPS30};
use qilin::Key;
use qilin::ScaleMode;
use qilin::WindowOptions;
struct PrefsScene {
prefs: PlayerPrefs,
}
impl Scene for PrefsScene {
fn new() -> Self
where
Self: Sized,
{
Self {
prefs: PlayerPrefs::new(PathBuf::from("player_prefs.json".to_string())),
}
}
fn enter(&mut self) {
println!("Press 'q' to increase decrease the count key of player prefs.");
println!("Press 'e' to increase increase the count key of player prefs.");
println!("Press 's' to save player prefs.");
self.prefs
.insert("count".to_string(), Storable::Int(0))
.unwrap();
}
fn update(&mut self, _canvas: &mut Canvas, _ctx: &mut GameContext) {
}
fn fixed_update(&mut self, _canvas: &mut Canvas, ctx: &mut GameContext) {
let count = self
.prefs
.get("count".to_string())
.unwrap()
.as_int()
.unwrap();
if ctx.is_key_down(Key::Q) {
self.prefs
.insert("count".to_string(), Storable::Int(*count - 1))
.unwrap();
} else if ctx.is_key_down(Key::E) {
self.prefs
.insert("count".to_string(), Storable::Int(*count + 1))
.unwrap();
} else if ctx.is_key_down(Key::S) {
self.prefs.save();
}
}
fn exit(&mut self) { println!("Exiting!") }
}
fn main() {
Game::new::<PrefsScene>() .with_config(GameConfig {
title: "Player Preferences".to_string(), update_rate_limit: FPS30, width: 800, height: 600, fixed_time_step: TimeStamp(Duration::from_secs_f32(1.0 / 10.0)), window: WindowOptions {
scale_mode: ScaleMode::AspectRatioStretch, resize: true, ..Default::default()
},
})
.play()
.expect("Failed to play game");
}