1
Fork 0

no-std enum from u8

This commit is contained in:
Andy Killorin 2023-09-16 22:23:58 -05:00
parent d8b9a656dc
commit 0c6e3a9e54
Signed by: ank
GPG key ID: B6241CA3B552BCA4
4 changed files with 56 additions and 37 deletions

17
Cargo.lock generated
View file

@ -58,7 +58,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "client"
version = "0.1.0"
version = "1.0.0"
dependencies = [
"gilrs",
"minifb",
@ -530,17 +530,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e6a0fd4f737c707bd9086cc16c925f294943eb62eb71499e9fd4cf71f8b9f4e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@ -620,12 +609,10 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pirates"
version = "0.1.0"
version = "1.0.0"
dependencies = [
"libm",
"nalgebra",
"num-derive",
"num-traits",
"rayon",
"spin",
"wee_alloc",

View file

@ -74,20 +74,19 @@ fn main() {
#[cfg(feature = "gamepad")]
if let Some(gamepad) = gamepad_handle.map(|h| gilrs.gamepad(h)) {
// see [ref:input_handler] for mapping info
gamepad.axis_data(Axis::LeftStickX).map(|axis| {
analog_input(AxisRudder, axis.value());
});
if gamepad.is_pressed(Button::LeftTrigger) {
gamepad.axis_data(Axis::LeftStickY).map(|axis| {
analog_input(5, axis.value());
analog_input(AxisSail, axis.value());
});
}
if gamepad.is_pressed(Button::South) {
keyboard_input(69)
keyboard_input(RaiseSail)
}
if gamepad.is_pressed(Button::East) {
keyboard_input(81)
keyboard_input(LowerSail)
}
if gamepad.is_pressed(Button::RightTrigger) {
gamepad.axis_data(Axis::RightStickY).map(|axis| {
@ -102,10 +101,10 @@ fn main() {
});
}
if gamepad.is_pressed(Button::West) {
keyboard_input(191)
keyboard_input(ResetCamera)
}
if gamepad.is_pressed(Button::North) {
keyboard_input(82)
keyboard_input(ResetBoat)
}
if gamepad.is_pressed(Button::DPadLeft) {
keyboard_input(PanLeft)
@ -121,7 +120,6 @@ fn main() {
}
}
// see [ref:input_handler] for mapping info
window.get_keys().iter().for_each(|key| match key {
Key::A => keyboard_input(RudderLeft),
Key::D => keyboard_input(RudderRight),
@ -131,10 +129,10 @@ fn main() {
Key::Down => keyboard_input(PanDown),
Key::Left => keyboard_input(PanLeft),
Key::Right => keyboard_input(PanRight),
Key::R => keyboard_input(82),
Key::Slash => keyboard_input(191),
Key::E => keyboard_input(69),
Key::Q => keyboard_input(81),
Key::R => keyboard_input(ResetBoat),
Key::Slash => keyboard_input(ResetCamera),
Key::E => keyboard_input(RaiseSail),
Key::Q => keyboard_input(LowerSail),
_ => (),
});

View file

@ -12,8 +12,6 @@ crate-type = ["lib", "cdylib"]
[dependencies]
libm = "0.2.7"
num-derive = "0.4.0"
num-traits = "0.2.16"
spin = "0.9.8"
[dependencies.rayon]

View file

@ -15,8 +15,6 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
extern crate alloc;
use alloc::vec::Vec;
use noise::PerlinBuf;
use num_traits::FromPrimitive;
use num_derive::FromPrimitive;
use core::sync::atomic::{AtomicU32, Ordering};
use libm;
extern crate nalgebra as na;
@ -118,19 +116,21 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
};
while let Some(key) = INPUTS.lock().pop() {
use Input::*;
match FromPrimitive::from_u32(key[0]).unwrap() { // [tag:input_handler]
let input = (key[0] as u8).try_into().ok();
if input.is_none() { continue; }
match input.unwrap() {
PanUp => camera[1] -= gain*10.0*camera[2], // up
PanDown => camera[1] += gain*10.0*camera[2], // down
PanLeft => camera[0] -= gain*10.0*camera[2], // left
PanRight => camera[0] += gain*10.0*camera[2], // right
191 => {
ResetCamera => {
*camera = [
noise::lerp(camera[0], 0.00, (gain * 0.25).min(1.0)),
noise::lerp(camera[1], 0.00, (gain * 0.25).min(1.0)),
noise::lerp(camera[2], 0.18, (gain * 0.25).min(1.0)),
]
}, // reset camera (/)
82 => boat.set_pos(Vector2::zeros()), // reset boat (r)
ResetBoat => boat.set_pos(Vector2::zeros()), // reset boat (r)
ZoomIn => camera[2] *= 1.0 - 0.1*gain, // +
ZoomOut => camera[2] *= 1.0 + 0.1*gain, // -
RudderLeft => boat.theta -= gain*10.0, // A
@ -139,9 +139,9 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
AxisRudder => boat.theta += gain * (key[1] as f32 - 127.0) * 0.062, // analog rudder
AxisPanY => camera[1] -= gain * (key[1] as f32 - 127.0) * 0.1 * camera[2], // pan[y]
AxisPanX => camera[0] += gain * (key[1] as f32 - 127.0) * 0.1 * camera[2], // pan[x]
5 => boat.sail += gain * (key[1] as f32 - 127.0) * 0.0013, // sail
69 => boat.sail += gain * 0.062, // E
81 => boat.sail -= gain * 0.062, // Q
AxisSail => boat.sail += gain * (key[1] as f32 - 127.0) * 0.0013, // sail
RaiseSail => boat.sail += gain * 0.062, // E
LowerSail => boat.sail -= gain * 0.062, // Q
}
}
boat.sail = boat.sail.clamp(0.0, 1.5);
@ -316,7 +316,34 @@ impl Boat {
}
#[derive(FromPrimitive)]
impl TryFrom<u8> for Input {
type Error = &'static str;
fn try_from(input: u8) -> Result<Self, Self::Error> {
use Input::*;
match input {
38 => Ok(PanUp),
40 => Ok(PanDown),
37 => Ok(PanLeft),
39 => Ok(PanRight),
61 => Ok(ZoomIn),
173 => Ok(ZoomOut),
65 => Ok(RudderLeft),
68 => Ok(RudderRight),
69 => Ok(RaiseSail),
81 => Ok(LowerSail),
82 => Ok(ResetBoat),
191 => Ok(ResetCamera),
0 => Ok(AxisZoom),
1 => Ok(AxisRudder),
3 => Ok(AxisPanY),
4 => Ok(AxisPanX),
5 => Ok(AxisSail),
_ => Err("unmapped")
}
}
}
#[repr(u8)]
pub enum Input {
/// Up Arrow
@ -335,8 +362,17 @@ pub enum Input {
RudderLeft = 65,
/// D
RudderRight = 68,
/// "/"
ResetCamera = 191,
/// R
ResetBoat = 82,
/// E
RaiseSail = 69,
/// Q
LowerSail = 81,
AxisZoom = 0,
AxisRudder = 1,
AxisPanY = 3,
AxisPanX = 4,
AxisSail = 5,
}