diff --git a/Cargo.lock b/Cargo.lock index 8e5509a..e4bbd32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -530,6 +530,17 @@ 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" @@ -613,6 +624,8 @@ version = "0.1.0" dependencies = [ "libm", "nalgebra", + "num-derive", + "num-traits", "rayon", "spin", "wee_alloc", diff --git a/client/src/main.rs b/client/src/main.rs index 264ecd7..fcf3401 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,9 +1,10 @@ use std::time::SystemTime; use minifb::{Key, ScaleMode, Window, WindowOptions, Scale}; extern crate pirates; -use pirates::{WIDTH, HEIGHT}; +use pirates::{WIDTH, HEIGHT, Input}; #[cfg(feature = "gilrs")] use gilrs::{Axis, Gilrs, Button}; +use pirates::Input::*; fn main() { #[cfg(feature = "gilrs")] @@ -27,17 +28,17 @@ fn main() { let mut buffer: Vec = Vec::with_capacity(WIDTH * HEIGHT); - fn keyboard_input(key: u8) { + fn keyboard_input(key: Input) { unsafe { - pirates::KEYCODE[0] = key; + pirates::KEYCODE[0] = key as u8; pirates::keyboard_input(); } } - fn analog_input(chan: u8, val: f32) { + fn analog_input(chan: Input, val: f32) { let val = (val * 127.0) as i8; unsafe { - pirates::KEYCODE[0] = chan; + pirates::KEYCODE[0] = chan as u8; pirates::KEYCODE[1] = (val + 127) as u8; pirates::keyboard_input(); } @@ -75,7 +76,7 @@ fn main() { 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(1, axis.value()); + analog_input(AxisRudder, axis.value()); }); if gamepad.is_pressed(Button::LeftTrigger) { gamepad.axis_data(Axis::LeftStickY).map(|axis| { @@ -90,14 +91,14 @@ fn main() { } if gamepad.is_pressed(Button::RightTrigger) { gamepad.axis_data(Axis::RightStickY).map(|axis| { - analog_input(3, axis.value()); + analog_input(AxisPanY, axis.value()); }); gamepad.axis_data(Axis::RightStickX).map(|axis| { - analog_input(4, axis.value()); + analog_input(AxisPanX, axis.value()); }); } else { gamepad.axis_data(Axis::RightStickY).map(|axis| { - analog_input(0, axis.value()); + analog_input(AxisZoom, axis.value()); }); } if gamepad.is_pressed(Button::West) { @@ -107,29 +108,29 @@ fn main() { keyboard_input(82) } if gamepad.is_pressed(Button::DPadLeft) { - keyboard_input(65) + keyboard_input(PanLeft) } if gamepad.is_pressed(Button::DPadRight) { - keyboard_input(68) + keyboard_input(PanRight) } if gamepad.is_pressed(Button::DPadUp) { - keyboard_input(61) + keyboard_input(ZoomIn) } if gamepad.is_pressed(Button::DPadDown) { - keyboard_input(173) + keyboard_input(ZoomOut) } } // see [ref:input_handler] for mapping info window.get_keys().iter().for_each(|key| match key { - Key::A => keyboard_input(65), - Key::D => keyboard_input(68), - Key::Equal => keyboard_input(61), - Key::Minus => keyboard_input(173), - Key::Up => keyboard_input(38), - Key::Down => keyboard_input(40), - Key::Left => keyboard_input(37), - Key::Right => keyboard_input(39), + Key::A => keyboard_input(RudderLeft), + Key::D => keyboard_input(RudderRight), + Key::Equal => keyboard_input(ZoomIn), + Key::Minus => keyboard_input(ZoomOut), + Key::Up => keyboard_input(PanUp), + 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), diff --git a/pirates/Cargo.toml b/pirates/Cargo.toml index 3461c3c..f3e99a0 100644 --- a/pirates/Cargo.toml +++ b/pirates/Cargo.toml @@ -12,6 +12,8 @@ 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] diff --git a/pirates/src/lib.rs b/pirates/src/lib.rs index bd1a8e5..adf71b6 100644 --- a/pirates/src/lib.rs +++ b/pirates/src/lib.rs @@ -15,6 +15,8 @@ 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; @@ -115,11 +117,12 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { LAST_FRAME_TIME / (1000.0 / 20.0) // normalize to 20fps, cap simulation at 10fps }; while let Some(key) = INPUTS.lock().pop() { - match key[0] { // [tag:input_handler] - 38 => camera[1] -= gain*10.0*camera[2], // up - 40 => camera[1] += gain*10.0*camera[2], // down - 37 => camera[0] -= gain*10.0*camera[2], // left - 39 => camera[0] += gain*10.0*camera[2], // right + use Input::*; + match FromPrimitive::from_u32(key[0]).unwrap() { // [tag:input_handler] + 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 => { *camera = [ noise::lerp(camera[0], 0.00, (gain * 0.25).min(1.0)), @@ -128,18 +131,17 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { ] }, // reset camera (/) 82 => boat.set_pos(Vector2::zeros()), // reset boat (r) - 61 => camera[2] *= 1.0 - 0.1*gain, // + - 173 => camera[2] *= 1.0 + 0.1*gain, // - - 65 => boat.theta -= gain*10.0, // A - 68 => boat.theta += gain*10.0, // D - 0 => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004 * gain, // analog zoom - 1 => boat.theta += gain * (key[1] as f32 - 127.0) * 0.062, // analog rudder - 3 => camera[1] -= gain * (key[1] as f32 - 127.0) * 0.1 * camera[2], // pan[y] - 4 => camera[0] += gain * (key[1] as f32 - 127.0) * 0.1 * camera[2], // pan[x] + ZoomIn => camera[2] *= 1.0 - 0.1*gain, // + + ZoomOut => camera[2] *= 1.0 + 0.1*gain, // - + RudderLeft => boat.theta -= gain*10.0, // A + RudderRight => boat.theta += gain*10.0, // D + AxisZoom => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004 * gain, // analog zoom + 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 - _ => {} } } boat.sail = boat.sail.clamp(0.0, 1.5); @@ -313,3 +315,28 @@ impl Boat { } } + +#[derive(FromPrimitive)] +#[repr(u8)] +pub enum Input { + /// Up Arrow + PanUp = 38, + /// Down Arrow + PanDown = 40, + /// Left Arrow + PanLeft = 37, + /// Right Arrow + PanRight = 39, + /// + + ZoomIn = 61, + /// - + ZoomOut = 173, + /// A + RudderLeft = 65, + /// D + RudderRight = 68, + AxisZoom = 0, + AxisRudder = 1, + AxisPanY = 3, + AxisPanX = 4, +}