Formatting

This commit is contained in:
2026-09-11 22:01:03 +02:00
parent 1be4b470c6
commit f3701bd8b0
17 changed files with 372 additions and 151 deletions
+14 -8
View File
@@ -177,7 +177,8 @@ pub fn postprocess_input(
{
input.frames_in_x_flick_deadzone = 1;
} else {
input.frames_in_x_flick_deadzone = input.frames_in_x_flick_deadzone.saturating_add(1);
input.frames_in_x_flick_deadzone =
input.frames_in_x_flick_deadzone.saturating_add(1);
}
}
@@ -190,7 +191,8 @@ pub fn postprocess_input(
{
input.frames_in_y_flick_deadzone = 1;
} else {
input.frames_in_y_flick_deadzone = input.frames_in_y_flick_deadzone.saturating_add(1);
input.frames_in_y_flick_deadzone =
input.frames_in_y_flick_deadzone.saturating_add(1);
}
}
@@ -228,9 +230,11 @@ pub fn postprocess_input(
input.set_lifetime(command_to_set, game_settings.input_common.input_buffer_size);
}
let mut y_flick_detected = y_abs >= axis_threshold && input.frames_in_y_flick_deadzone <= frame_threshold;
let mut x_flick_detected = x_abs >= axis_threshold && input.frames_in_x_flick_deadzone <= frame_threshold;
let mut y_flick_detected =
y_abs >= axis_threshold && input.frames_in_y_flick_deadzone <= frame_threshold;
let mut x_flick_detected =
x_abs >= axis_threshold && input.frames_in_x_flick_deadzone <= frame_threshold;
// if both flicks are detected, only one wins
if x_flick_detected && y_flick_detected {
@@ -239,7 +243,7 @@ pub fn postprocess_input(
}
let attack_detected = input.current_frame.attack && !input.prev_frame.attack;
// Tilt/smash up/down
if attack_detected {
let attack_type = if y_flick_detected {
@@ -270,11 +274,13 @@ pub fn postprocess_input(
FighterAttackCommandType::Neutral
};
input.set_lifetime(FighterCommands::Attack, game_settings.input_common.input_buffer_size);
input.set_lifetime(
FighterCommands::Attack,
game_settings.input_common.input_buffer_size,
);
input.set_attack_command_type(attack_type);
}
if x_flick_detected {
input.clear_command(FighterCommands::SmashMoveLeft);
input.clear_command(FighterCommands::SmashMoveRight);
+10 -10
View File
@@ -149,19 +149,19 @@ pub fn gc_event_system(
send_axis!(GamepadAxis::LeftStickY, now.left_stick.1);
}
if prev.right_stick != now.right_stick {
send_axis!(
GamepadAxis::RightStickX,
now.right_stick.0
);
send_axis!(
GamepadAxis::RightStickY,
now.right_stick.1
);
send_axis!(GamepadAxis::RightStickX, now.right_stick.0);
send_axis!(GamepadAxis::RightStickY, now.right_stick.1);
}
if prev.triggers != now.triggers {
// The analog triggers are represented as button-axis values, as we have no GamepadAxis to use.
send_button!(GamepadButton::LeftTrigger2, unsigned_axis_float(now.triggers.0));
send_button!(GamepadButton::RightTrigger2, unsigned_axis_float(now.triggers.1));
send_button!(
GamepadButton::LeftTrigger2,
unsigned_axis_float(now.triggers.0)
);
send_button!(
GamepadButton::RightTrigger2,
unsigned_axis_float(now.triggers.1)
);
}
macro_rules! digital {
+2 -2
View File
@@ -1,11 +1,11 @@
pub mod bridge;
pub mod poll;
pub use bridge::{GcPort, GcPorts, GC_PORT_COUNT};
pub use bridge::{GC_PORT_COUNT, GcPort, GcPorts};
pub use poll::{GcAdapterSnapshot, GcPortState};
use bevy::input::gamepad::{gamepad_connection_system, gamepad_event_processing_system};
use bevy::input::InputSystems;
use bevy::input::gamepad::{gamepad_connection_system, gamepad_event_processing_system};
use bevy::prelude::*;
#[derive(Default)]
+5 -8
View File
@@ -98,12 +98,9 @@ pub fn spawn_poll_thread() -> Result<GcAdapterSnapshot, gc_adapter_neo::UsbError
.name("gc-adapter-poll".into())
.spawn(move || {
// Flush whatever the adapter had buffered before we started
if let Err(payload) =
panic::catch_unwind(AssertUnwindSafe(|| adapter.refresh_inputs()))
if let Err(payload) = panic::catch_unwind(AssertUnwindSafe(|| adapter.refresh_inputs()))
{
error!(
"GameCube Controller Adapter USB refresh panicked on startup: {payload:?}"
);
error!("GameCube Controller Adapter USB refresh panicked on startup: {payload:?}");
}
#[cfg(debug_assertions)]
@@ -112,8 +109,7 @@ pub fn spawn_poll_thread() -> Result<GcAdapterSnapshot, gc_adapter_neo::UsbError
let mut rate_window_count: u32 = 0;
while !thread_shutdown.load(Ordering::Relaxed) {
let result =
panic::catch_unwind(AssertUnwindSafe(|| adapter.read_controllers()));
let result = panic::catch_unwind(AssertUnwindSafe(|| adapter.read_controllers()));
let controllers = match result {
Ok(Ok(controllers)) => controllers,
@@ -142,7 +138,8 @@ pub fn spawn_poll_thread() -> Result<GcAdapterSnapshot, gc_adapter_neo::UsbError
let mut states = [GcPortState::default(); 4];
for i in 0..4 {
states[i] = GcPortState::make_port_state(&controllers[i], adapter.calibration(i));
states[i] =
GcPortState::make_port_state(&controllers[i], adapter.calibration(i));
}
*thread_ports.lock().unwrap_or_else(|e| e.into_inner()) = states;
+11 -3
View File
@@ -21,8 +21,16 @@ pub fn sample_gamepad(pad: &Gamepad, input_map: &BaseInputMap) -> FighterInputFr
// 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::MovementXDir(sign) => {
if pressed {
movement.x += sign as f32
}
}
InputMapAction::MovementYDir(sign) => {
if pressed {
movement.y += sign as f32
}
}
}
}
// gamepad does not have a separate axis for left and right, so sign here should be 1
@@ -32,7 +40,7 @@ pub fn sample_gamepad(pad: &Gamepad, input_map: &BaseInputMap) -> FighterInputFr
InputMapAction::MovementXDir(_) => movement.x += value,
InputMapAction::MovementYDir(_) => movement.y += value,
_ => {}
}
}
}
}
}
-1
View File
@@ -33,7 +33,6 @@ pub fn sample_keyboard(key: &ButtonInput<KeyCode>, input_map: &BaseInputMap) ->
InputMapAction::Jump => input_frame.jump = action_strength != FGi32::ZERO,
InputMapAction::Shield => input_frame.shield = action_strength != FGi32::ZERO,
InputMapAction::Attack => input_frame.attack = action_strength != FGi32::ZERO,
}
}
+97 -28
View File
@@ -41,6 +41,10 @@ impl LocalInputAssignments {
}
None
}
pub fn get_from_slot(&self, slot: usize) -> Option<&LocalInputSource> {
self.0.iter().find(|(c_slot, _)| slot == *c_slot).map(|(_, source)| source)
}
}
use crate::game_settings::GameSettings;
@@ -92,8 +96,6 @@ impl Plugin for FighterInputPlugin {
key: KeyCode::KeyJ,
action: InputMapAction::Attack,
},
// Menu
],
gamepad: vec![
@@ -125,33 +127,92 @@ impl Plugin for FighterInputPlugin {
binding: GamepadBinding::Button(GamepadButton::East),
action: InputMapAction::Attack,
},
],
})
.insert_resource(MenuInputMap {
keyboard: vec![
MenuKeyboardInputMapElement { key: KeyCode::ArrowUp, action: MenuInputMapAction::MovementYDir(1) },
MenuKeyboardInputMapElement { key: KeyCode::ArrowDown, action: MenuInputMapAction::MovementYDir(-1) },
MenuKeyboardInputMapElement { key: KeyCode::ArrowLeft, action: MenuInputMapAction::MovementXDir(-1) },
MenuKeyboardInputMapElement { key: KeyCode::ArrowRight, action: MenuInputMapAction::MovementXDir(1) },
MenuKeyboardInputMapElement { key: KeyCode::Enter, action: MenuInputMapAction::Accept },
MenuKeyboardInputMapElement { key: KeyCode::Escape, action: MenuInputMapAction::Back },
MenuKeyboardInputMapElement { key: KeyCode::Space, action: MenuInputMapAction::Start },
MenuKeyboardInputMapElement { key: KeyCode::KeyW, action: MenuInputMapAction::MovementYDir(1) },
MenuKeyboardInputMapElement { key: KeyCode::KeyS, action: MenuInputMapAction::MovementYDir(-1) },
MenuKeyboardInputMapElement { key: KeyCode::KeyA, action: MenuInputMapAction::MovementXDir(-1) },
MenuKeyboardInputMapElement { key: KeyCode::KeyD, action: MenuInputMapAction::MovementXDir(1) },
MenuKeyboardInputMapElement {
key: KeyCode::ArrowUp,
action: MenuInputMapAction::MovementYDir(1),
},
MenuKeyboardInputMapElement {
key: KeyCode::ArrowDown,
action: MenuInputMapAction::MovementYDir(-1),
},
MenuKeyboardInputMapElement {
key: KeyCode::ArrowLeft,
action: MenuInputMapAction::MovementXDir(-1),
},
MenuKeyboardInputMapElement {
key: KeyCode::ArrowRight,
action: MenuInputMapAction::MovementXDir(1),
},
MenuKeyboardInputMapElement {
key: KeyCode::Enter,
action: MenuInputMapAction::Accept,
},
MenuKeyboardInputMapElement {
key: KeyCode::Escape,
action: MenuInputMapAction::Back,
},
MenuKeyboardInputMapElement {
key: KeyCode::Space,
action: MenuInputMapAction::Start,
},
MenuKeyboardInputMapElement {
key: KeyCode::KeyW,
action: MenuInputMapAction::MovementYDir(1),
},
MenuKeyboardInputMapElement {
key: KeyCode::KeyS,
action: MenuInputMapAction::MovementYDir(-1),
},
MenuKeyboardInputMapElement {
key: KeyCode::KeyA,
action: MenuInputMapAction::MovementXDir(-1),
},
MenuKeyboardInputMapElement {
key: KeyCode::KeyD,
action: MenuInputMapAction::MovementXDir(1),
},
],
gamepad: vec![
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::DPadUp), action: MenuInputMapAction::MovementYDir(1) },
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::DPadDown), action: MenuInputMapAction::MovementYDir(-1) },
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::DPadLeft), action: MenuInputMapAction::MovementXDir(-1) },
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::DPadRight), action: MenuInputMapAction::MovementXDir(1) },
MenuGamepadInputMapElement { binding: GamepadBinding::Axis(GamepadAxis::LeftStickX, 1), action: MenuInputMapAction::MovementXDir(1) },
MenuGamepadInputMapElement { binding: GamepadBinding::Axis(GamepadAxis::LeftStickY, 1), action: MenuInputMapAction::MovementYDir(1) },
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::South), action: MenuInputMapAction::Accept },
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::West), action: MenuInputMapAction::Back },
MenuGamepadInputMapElement { binding: GamepadBinding::Button(GamepadButton::Start), action: MenuInputMapAction::Start },
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::DPadUp),
action: MenuInputMapAction::MovementYDir(1),
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::DPadDown),
action: MenuInputMapAction::MovementYDir(-1),
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::DPadLeft),
action: MenuInputMapAction::MovementXDir(-1),
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::DPadRight),
action: MenuInputMapAction::MovementXDir(1),
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Axis(GamepadAxis::LeftStickX, 1),
action: MenuInputMapAction::MovementXDir(1),
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Axis(GamepadAxis::LeftStickY, 1),
action: MenuInputMapAction::MovementYDir(1),
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::South),
action: MenuInputMapAction::Accept,
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::West),
action: MenuInputMapAction::Back,
},
MenuGamepadInputMapElement {
binding: GamepadBinding::Button(GamepadButton::Start),
action: MenuInputMapAction::Start,
},
],
})
.init_resource::<LocalInputAssignments>()
@@ -183,16 +244,24 @@ fn read_local_inputs(
gamepads: Query<&Gamepad>,
assignments: Res<LocalInputAssignments>,
local_players: Res<LocalPlayers>,
game_settings: Res<GameSettings>
game_settings: Res<GameSettings>,
) {
let mut local_inputs = HashMap::new();
for handle in &local_players.0 {
let assigned = assignments.0.iter().find(|(player_handle, _)| player_handle == handle);
let assigned = assignments
.0
.iter()
.find(|(player_handle, _)| player_handle == handle);
let gamepad = match assigned {
Some((_, LocalInputSource::Gamepad(entity))) => gamepads.get(*entity).ok(),
Some((_, LocalInputSource::Keyboard)) => None,
None => gc_ports.entities.get(*handle).copied().flatten().and_then(|entity| gamepads.get(entity).ok()),
None => gc_ports
.entities
.get(*handle)
.copied()
.flatten()
.and_then(|entity| gamepads.get(entity).ok()),
};
let mut input_frame = match assigned {
@@ -201,8 +270,8 @@ fn read_local_inputs(
.map(|pad| gamepad::sample_gamepad(pad, &input_map))
.unwrap_or_default(),
None => match gamepad {
Some(pad) => gamepad::sample_gamepad(pad, &input_map),
None => keyboard::sample_keyboard(&key, &input_map),
Some(pad) => gamepad::sample_gamepad(pad, &input_map),
None => keyboard::sample_keyboard(&key, &input_map),
},
};