72 lines
2.9 KiB
Rust
72 lines
2.9 KiB
Rust
use bevy::math::Vec2;
|
|
use bevy::prelude::*;
|
|
|
|
use crate::input::map::GamepadBinding;
|
|
use crate::input::{BaseInputMap, FighterInputFrame, InputMapAction};
|
|
use crate::math::int::FGi32;
|
|
use crate::math::vec::FGVec2;
|
|
|
|
pub fn sample_gamepad(pad: &Gamepad, input_map: &BaseInputMap) -> FighterInputFrame {
|
|
let mut input_frame = FighterInputFrame::default();
|
|
let mut movement = Vec2::ZERO;
|
|
let mut directional_attack = Vec2::ZERO;
|
|
|
|
for gamepad_element in &input_map.gamepad {
|
|
match &gamepad_element.binding {
|
|
GamepadBinding::Button(button) => {
|
|
let pressed = pad.pressed(*button);
|
|
if let Some(action) = gamepad_element.action.digital_action() {
|
|
input_frame
|
|
.bindings
|
|
.set(action, gamepad_element.binding_slot, pressed);
|
|
}
|
|
match gamepad_element.action {
|
|
InputMapAction::Jump | InputMapAction::Shield | InputMapAction::Attack => {}
|
|
// Digital buttons driving movement isn't used by the
|
|
// default bindings (the stick covers that), but stay
|
|
// consistent with the keyboard backend if one is added.
|
|
InputMapAction::MovementXDir(sign) => {
|
|
if pressed {
|
|
movement.x += sign as f32
|
|
}
|
|
}
|
|
InputMapAction::MovementYDir(sign) => {
|
|
if pressed {
|
|
movement.y += sign as f32
|
|
}
|
|
}
|
|
InputMapAction::AttackXDir(sign) => {
|
|
if pressed {
|
|
directional_attack.x += sign as f32;
|
|
}
|
|
}
|
|
InputMapAction::AttackYDir(sign) => {
|
|
if pressed {
|
|
directional_attack.y += sign as f32;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// gamepad does not have a separate axis for left and right, so sign here should be 1
|
|
GamepadBinding::Axis(axis, sign) => {
|
|
let value = pad.get(*axis).unwrap_or(0.0) * (*sign as f32);
|
|
match gamepad_element.action {
|
|
InputMapAction::AttackXDir(_) => directional_attack.x += value,
|
|
InputMapAction::AttackYDir(_) => directional_attack.y += value,
|
|
InputMapAction::MovementXDir(_) => movement.x += value,
|
|
InputMapAction::MovementYDir(_) => movement.y += value,
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
input_frame.movement = FGVec2::new(FGi32::from_num(movement.x), FGi32::from_num(movement.y));
|
|
input_frame.directional_attack = FGVec2::new(
|
|
FGi32::from_num(directional_attack.x),
|
|
FGi32::from_num(directional_attack.y),
|
|
);
|
|
|
|
input_frame
|
|
}
|