Pre-bevy minigame commit
This commit is contained in:
22
mini-game/Cargo.toml
Normal file
22
mini-game/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "g13-game"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
g13-driver.workspace = true
|
||||
embedded-graphics = "0.8.1"
|
||||
# time = { version = "0.3.47", features = ["formatting", "macros"] }
|
||||
tokio = { version = "1.49.0", features = [
|
||||
"rt",
|
||||
"rt-multi-thread",
|
||||
"sync",
|
||||
"macros",
|
||||
"net",
|
||||
"signal",
|
||||
"time",
|
||||
] }
|
||||
|
||||
# bevy = { version = "0.18", default-features = false }
|
||||
# bevy_ecs = { version = "0.18", default-features = false }
|
||||
# bevy_platform = { version = "0.18", default-features = false }
|
||||
94
mini-game/src/main.rs
Normal file
94
mini-game/src/main.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use embedded_graphics::{
|
||||
image::{Image, ImageRaw},
|
||||
mono_font::{MonoTextStyle, ascii::*},
|
||||
pixelcolor::BinaryColor,
|
||||
prelude::*,
|
||||
text::{Alignment, Text, TextStyleBuilder},
|
||||
};
|
||||
use tokio::time::Instant;
|
||||
|
||||
use g13_driver::{
|
||||
G13, G13_LCD_COLUMNS, G13_LCD_ROWS,
|
||||
joystick::{Axis, Button},
|
||||
};
|
||||
|
||||
const DEADZONE: i32 = 30;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut g13 = G13::new()?;
|
||||
let _g13 = g13.clone();
|
||||
tokio::spawn(async move {
|
||||
// background thread to update button state
|
||||
loop {
|
||||
if let Err(e) = _g13.read() {
|
||||
eprintln!("{:?}", e);
|
||||
break; // probably due to unplug
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
g13.set_lcd_color(4, 8, 96)?;
|
||||
|
||||
// let character_style = MonoTextStyle::new(&FONT_5X7, BinaryColor::On);
|
||||
// let textstyle = TextStyleBuilder::new()
|
||||
// .baseline(embedded_graphics::text::Baseline::Top)
|
||||
// .alignment(Alignment::Left)
|
||||
// .build();
|
||||
|
||||
let mut dt = 0.0;
|
||||
|
||||
let mut x = 20.0;
|
||||
let mut y = (G13_LCD_ROWS as f64 / 2.0) - 3.0;
|
||||
|
||||
let speed = 20.0;
|
||||
|
||||
let player_sprite = ImageRaw::<BinaryColor>::new(DATA, 16);
|
||||
|
||||
loop {
|
||||
let start = Instant::now();
|
||||
let mut jx = g13.state().x;
|
||||
if (-DEADZONE..=DEADZONE).contains(&jx) {
|
||||
jx = 0;
|
||||
}
|
||||
|
||||
let mut jy: i32 = g13.state().y;
|
||||
if (-DEADZONE..=DEADZONE).contains(&jy) {
|
||||
jy = 0;
|
||||
}
|
||||
|
||||
let joyx = jx as f64 / 502.0;
|
||||
let joyy = jy as f64 / 502.0;
|
||||
|
||||
x += joyx * speed * dt;
|
||||
y += joyy * speed * dt;
|
||||
|
||||
// Render
|
||||
g13.clear(BinaryColor::Off).unwrap();
|
||||
|
||||
// Player
|
||||
let image = Image::new(&player_sprite, Point::new(x as i32, y as i32));
|
||||
image.draw(&mut g13)?;
|
||||
|
||||
// Terrain Generation
|
||||
|
||||
g13.render().unwrap();
|
||||
|
||||
// Calculate delta time
|
||||
let delta = Instant::now() - start;
|
||||
tokio::time::sleep(Duration::from_millis(33) - delta).await;
|
||||
dt = (Instant::now() - start).as_secs_f64();
|
||||
}
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
const DATA: &[u8] = &[
|
||||
0b11100000, 0b00000000,
|
||||
0b11111111, 0b00000000,
|
||||
0b10000000, 0b11000000,
|
||||
0b10011111, 0b00111000,
|
||||
0b10000000, 0b00000110,
|
||||
0b01111111, 0b11111111,
|
||||
];
|
||||
Reference in New Issue
Block a user