From b380ad94647c9bc446d5a76bb16dcc286077275a Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:22 +0100 Subject: feat(input): virtual input mapping and codegen --- examples/input-codegen/Cargo.toml | 9 ++++ examples/input-codegen/src/main.rs | 86 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/input-codegen/Cargo.toml create mode 100644 examples/input-codegen/src/main.rs (limited to 'examples') diff --git a/examples/input-codegen/Cargo.toml b/examples/input-codegen/Cargo.toml new file mode 100644 index 0000000..3fc010f --- /dev/null +++ b/examples/input-codegen/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "input-codegen" +version = "0.1.0" +authors = ["Oscar Shrimpton "] +edition = "2018" + +[dependencies] +stockton-input = { path = "../../stockton-input" } +stockton-input-codegen = { path = "../../stockton-input-codegen" } diff --git a/examples/input-codegen/src/main.rs b/examples/input-codegen/src/main.rs new file mode 100644 index 0000000..8835f2e --- /dev/null +++ b/examples/input-codegen/src/main.rs @@ -0,0 +1,86 @@ +#[macro_use] +extern crate stockton_input_codegen; + +use std::collections::BTreeMap; +use stockton_input::Action; +use stockton_input::{Axis, Button, InputManager, InputMutation}; + +#[derive(InputManager, Default, Debug, Clone)] +struct MovementInputs { + #[axis] + vertical: Axis, + + #[axis] + horizontal: Axis, + + #[button] + jump: Button, +} + +const TEST_ACTIONS: [Action; 10] = [ + Action::KeyPress(1), + Action::KeyRelease(1), + Action::KeyPress(2), + Action::KeyPress(3), + Action::KeyRelease(2), + Action::KeyRelease(3), + Action::KeyPress(4), + Action::KeyPress(5), + Action::KeyRelease(4), + Action::KeyRelease(5), +]; + +// For testing, 1 = w 2 = a +// 3 = s 4 = d +// 5 = jump +fn main() { + let mut action_schema = BTreeMap::new(); + action_schema.insert( + 1, + (MovementInputsFields::Vertical, InputMutation::PositiveAxis), + ); + action_schema.insert( + 3, + (MovementInputsFields::Vertical, InputMutation::NegativeAxis), + ); + action_schema.insert( + 4, + ( + MovementInputsFields::Horizontal, + InputMutation::PositiveAxis, + ), + ); + action_schema.insert( + 2, + ( + MovementInputsFields::Horizontal, + InputMutation::NegativeAxis, + ), + ); + action_schema.insert(5, (MovementInputsFields::Jump, InputMutation::MapToButton)); + + let mut manager = MovementInputsManager::new(action_schema); + + for action in TEST_ACTIONS.iter() { + pretty_print_state(&manager.inputs); + manager.handle_frame(std::iter::once(action)); + } + pretty_print_state(&manager.inputs); +} + +fn pretty_print_state(inputs: &MovementInputs) { + if *inputs.vertical != 0 { + print!("vertical = {} ", *inputs.vertical); + } + if *inputs.horizontal != 0 { + print!("horizontal = {} ", *inputs.horizontal); + } + if inputs.jump.is_down() { + if inputs.jump.is_hot { + print!("jump!") + } else { + print!("jump") + } + } + println!(); +} -- cgit v1.2.3