Merge branch 'input_enum'
This commit is contained in:
commit
d8b9a656dc
4 changed files with 78 additions and 35 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
@ -530,6 +530,17 @@ dependencies = [
|
||||||
"num-traits",
|
"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]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.45"
|
version = "0.1.45"
|
||||||
|
@ -613,6 +624,8 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libm",
|
"libm",
|
||||||
"nalgebra",
|
"nalgebra",
|
||||||
|
"num-derive",
|
||||||
|
"num-traits",
|
||||||
"rayon",
|
"rayon",
|
||||||
"spin",
|
"spin",
|
||||||
"wee_alloc",
|
"wee_alloc",
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use minifb::{Key, ScaleMode, Window, WindowOptions, Scale};
|
use minifb::{Key, ScaleMode, Window, WindowOptions, Scale};
|
||||||
extern crate pirates;
|
extern crate pirates;
|
||||||
use pirates::{WIDTH, HEIGHT};
|
use pirates::{WIDTH, HEIGHT, Input};
|
||||||
#[cfg(feature = "gilrs")]
|
#[cfg(feature = "gilrs")]
|
||||||
use gilrs::{Axis, Gilrs, Button};
|
use gilrs::{Axis, Gilrs, Button};
|
||||||
|
use pirates::Input::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
#[cfg(feature = "gilrs")]
|
#[cfg(feature = "gilrs")]
|
||||||
|
@ -27,17 +28,17 @@ fn main() {
|
||||||
|
|
||||||
let mut buffer: Vec<u32> = Vec::with_capacity(WIDTH * HEIGHT);
|
let mut buffer: Vec<u32> = Vec::with_capacity(WIDTH * HEIGHT);
|
||||||
|
|
||||||
fn keyboard_input(key: u8) {
|
fn keyboard_input(key: Input) {
|
||||||
unsafe {
|
unsafe {
|
||||||
pirates::KEYCODE[0] = key;
|
pirates::KEYCODE[0] = key as u8;
|
||||||
pirates::keyboard_input();
|
pirates::keyboard_input();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn analog_input(chan: u8, val: f32) {
|
fn analog_input(chan: Input, val: f32) {
|
||||||
let val = (val * 127.0) as i8;
|
let val = (val * 127.0) as i8;
|
||||||
unsafe {
|
unsafe {
|
||||||
pirates::KEYCODE[0] = chan;
|
pirates::KEYCODE[0] = chan as u8;
|
||||||
pirates::KEYCODE[1] = (val + 127) as u8;
|
pirates::KEYCODE[1] = (val + 127) as u8;
|
||||||
pirates::keyboard_input();
|
pirates::keyboard_input();
|
||||||
}
|
}
|
||||||
|
@ -75,7 +76,7 @@ fn main() {
|
||||||
if let Some(gamepad) = gamepad_handle.map(|h| gilrs.gamepad(h)) {
|
if let Some(gamepad) = gamepad_handle.map(|h| gilrs.gamepad(h)) {
|
||||||
// see [ref:input_handler] for mapping info
|
// see [ref:input_handler] for mapping info
|
||||||
gamepad.axis_data(Axis::LeftStickX).map(|axis| {
|
gamepad.axis_data(Axis::LeftStickX).map(|axis| {
|
||||||
analog_input(1, axis.value());
|
analog_input(AxisRudder, axis.value());
|
||||||
});
|
});
|
||||||
if gamepad.is_pressed(Button::LeftTrigger) {
|
if gamepad.is_pressed(Button::LeftTrigger) {
|
||||||
gamepad.axis_data(Axis::LeftStickY).map(|axis| {
|
gamepad.axis_data(Axis::LeftStickY).map(|axis| {
|
||||||
|
@ -90,14 +91,14 @@ fn main() {
|
||||||
}
|
}
|
||||||
if gamepad.is_pressed(Button::RightTrigger) {
|
if gamepad.is_pressed(Button::RightTrigger) {
|
||||||
gamepad.axis_data(Axis::RightStickY).map(|axis| {
|
gamepad.axis_data(Axis::RightStickY).map(|axis| {
|
||||||
analog_input(3, axis.value());
|
analog_input(AxisPanY, axis.value());
|
||||||
});
|
});
|
||||||
gamepad.axis_data(Axis::RightStickX).map(|axis| {
|
gamepad.axis_data(Axis::RightStickX).map(|axis| {
|
||||||
analog_input(4, axis.value());
|
analog_input(AxisPanX, axis.value());
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
gamepad.axis_data(Axis::RightStickY).map(|axis| {
|
gamepad.axis_data(Axis::RightStickY).map(|axis| {
|
||||||
analog_input(0, axis.value());
|
analog_input(AxisZoom, axis.value());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if gamepad.is_pressed(Button::West) {
|
if gamepad.is_pressed(Button::West) {
|
||||||
|
@ -107,29 +108,29 @@ fn main() {
|
||||||
keyboard_input(82)
|
keyboard_input(82)
|
||||||
}
|
}
|
||||||
if gamepad.is_pressed(Button::DPadLeft) {
|
if gamepad.is_pressed(Button::DPadLeft) {
|
||||||
keyboard_input(65)
|
keyboard_input(PanLeft)
|
||||||
}
|
}
|
||||||
if gamepad.is_pressed(Button::DPadRight) {
|
if gamepad.is_pressed(Button::DPadRight) {
|
||||||
keyboard_input(68)
|
keyboard_input(PanRight)
|
||||||
}
|
}
|
||||||
if gamepad.is_pressed(Button::DPadUp) {
|
if gamepad.is_pressed(Button::DPadUp) {
|
||||||
keyboard_input(61)
|
keyboard_input(ZoomIn)
|
||||||
}
|
}
|
||||||
if gamepad.is_pressed(Button::DPadDown) {
|
if gamepad.is_pressed(Button::DPadDown) {
|
||||||
keyboard_input(173)
|
keyboard_input(ZoomOut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// see [ref:input_handler] for mapping info
|
// see [ref:input_handler] for mapping info
|
||||||
window.get_keys().iter().for_each(|key| match key {
|
window.get_keys().iter().for_each(|key| match key {
|
||||||
Key::A => keyboard_input(65),
|
Key::A => keyboard_input(RudderLeft),
|
||||||
Key::D => keyboard_input(68),
|
Key::D => keyboard_input(RudderRight),
|
||||||
Key::Equal => keyboard_input(61),
|
Key::Equal => keyboard_input(ZoomIn),
|
||||||
Key::Minus => keyboard_input(173),
|
Key::Minus => keyboard_input(ZoomOut),
|
||||||
Key::Up => keyboard_input(38),
|
Key::Up => keyboard_input(PanUp),
|
||||||
Key::Down => keyboard_input(40),
|
Key::Down => keyboard_input(PanDown),
|
||||||
Key::Left => keyboard_input(37),
|
Key::Left => keyboard_input(PanLeft),
|
||||||
Key::Right => keyboard_input(39),
|
Key::Right => keyboard_input(PanRight),
|
||||||
Key::R => keyboard_input(82),
|
Key::R => keyboard_input(82),
|
||||||
Key::Slash => keyboard_input(191),
|
Key::Slash => keyboard_input(191),
|
||||||
Key::E => keyboard_input(69),
|
Key::E => keyboard_input(69),
|
||||||
|
|
|
@ -12,6 +12,8 @@ crate-type = ["lib", "cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libm = "0.2.7"
|
libm = "0.2.7"
|
||||||
|
num-derive = "0.4.0"
|
||||||
|
num-traits = "0.2.16"
|
||||||
spin = "0.9.8"
|
spin = "0.9.8"
|
||||||
|
|
||||||
[dependencies.rayon]
|
[dependencies.rayon]
|
||||||
|
|
|
@ -15,6 +15,8 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use noise::PerlinBuf;
|
use noise::PerlinBuf;
|
||||||
|
use num_traits::FromPrimitive;
|
||||||
|
use num_derive::FromPrimitive;
|
||||||
use core::sync::atomic::{AtomicU32, Ordering};
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
use libm;
|
use libm;
|
||||||
extern crate nalgebra as na;
|
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
|
LAST_FRAME_TIME / (1000.0 / 20.0) // normalize to 20fps, cap simulation at 10fps
|
||||||
};
|
};
|
||||||
while let Some(key) = INPUTS.lock().pop() {
|
while let Some(key) = INPUTS.lock().pop() {
|
||||||
match key[0] { // [tag:input_handler]
|
use Input::*;
|
||||||
38 => camera[1] -= gain*10.0*camera[2], // up
|
match FromPrimitive::from_u32(key[0]).unwrap() { // [tag:input_handler]
|
||||||
40 => camera[1] += gain*10.0*camera[2], // down
|
PanUp => camera[1] -= gain*10.0*camera[2], // up
|
||||||
37 => camera[0] -= gain*10.0*camera[2], // left
|
PanDown => camera[1] += gain*10.0*camera[2], // down
|
||||||
39 => camera[0] += gain*10.0*camera[2], // right
|
PanLeft => camera[0] -= gain*10.0*camera[2], // left
|
||||||
|
PanRight => camera[0] += gain*10.0*camera[2], // right
|
||||||
191 => {
|
191 => {
|
||||||
*camera = [
|
*camera = [
|
||||||
noise::lerp(camera[0], 0.00, (gain * 0.25).min(1.0)),
|
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 (/)
|
}, // reset camera (/)
|
||||||
82 => boat.set_pos(Vector2::zeros()), // reset boat (r)
|
82 => boat.set_pos(Vector2::zeros()), // reset boat (r)
|
||||||
61 => camera[2] *= 1.0 - 0.1*gain, // +
|
ZoomIn => camera[2] *= 1.0 - 0.1*gain, // +
|
||||||
173 => camera[2] *= 1.0 + 0.1*gain, // -
|
ZoomOut => camera[2] *= 1.0 + 0.1*gain, // -
|
||||||
65 => boat.theta -= gain*10.0, // A
|
RudderLeft => boat.theta -= gain*10.0, // A
|
||||||
68 => boat.theta += gain*10.0, // D
|
RudderRight => boat.theta += gain*10.0, // D
|
||||||
0 => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004 * gain, // analog zoom
|
AxisZoom => 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
|
AxisRudder => 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]
|
AxisPanY => 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]
|
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
|
5 => boat.sail += gain * (key[1] as f32 - 127.0) * 0.0013, // sail
|
||||||
69 => boat.sail += gain * 0.062, // E
|
69 => boat.sail += gain * 0.062, // E
|
||||||
81 => boat.sail -= gain * 0.062, // Q
|
81 => boat.sail -= gain * 0.062, // Q
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boat.sail = boat.sail.clamp(0.0, 1.5);
|
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,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue