Simpler + Better Inputs (#82)
* switch to events for instantaneous inputs * input simplification + improvements * fix trail crash * fix clippy warnings * qualify `Trail` in fn signature
This commit is contained in:
@@ -6,7 +6,9 @@ use shared::{
|
||||
BackbackSwapEvent, Backpack, UiHeadState,
|
||||
backpack_ui::{BackpackUiState, HEAD_SLOTS},
|
||||
},
|
||||
control::ControlState,
|
||||
control::{
|
||||
BackpackLeftPressed, BackpackRightPressed, BackpackSwapPressed, BackpackTogglePressed,
|
||||
},
|
||||
protocol::{ClientToController, PlaySound},
|
||||
};
|
||||
|
||||
@@ -21,48 +23,41 @@ pub fn plugin(app: &mut App) {
|
||||
);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn swap_head_inputs(
|
||||
backpacks: Query<Ref<Backpack>>,
|
||||
clients: ClientToController,
|
||||
mut inputs: MessageReader<FromClient<ControlState>>,
|
||||
mut backpack_toggles: MessageReader<FromClient<BackpackTogglePressed>>,
|
||||
mut backpack_lefts: MessageReader<FromClient<BackpackLeftPressed>>,
|
||||
mut backpack_rights: MessageReader<FromClient<BackpackRightPressed>>,
|
||||
mut backpack_swaps: MessageReader<FromClient<BackpackSwapPressed>>,
|
||||
mut commands: Commands,
|
||||
mut state: Single<&mut BackpackUiState>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for controls in inputs.read() {
|
||||
let player = clients.get_controller(controls.client_id);
|
||||
let backpack = backpacks.get(player).unwrap();
|
||||
|
||||
for _ in backpack_toggles.read() {
|
||||
if state.count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
if controls.backpack_toggle {
|
||||
state.open = !state.open;
|
||||
commands.server_trigger(ToClients {
|
||||
mode: SendMode::Broadcast,
|
||||
message: PlaySound::Backpack { open: state.open },
|
||||
});
|
||||
}
|
||||
state.open = !state.open;
|
||||
commands.server_trigger(ToClients {
|
||||
mode: SendMode::Broadcast,
|
||||
message: PlaySound::Backpack { open: state.open },
|
||||
});
|
||||
}
|
||||
|
||||
for press in backpack_lefts.read() {
|
||||
let player = clients.get_controller(press.client_id);
|
||||
let backpack = backpacks.get(player).unwrap();
|
||||
|
||||
if !state.open {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut changed = false;
|
||||
if controls.backpack_left && state.current_slot > 0 {
|
||||
if state.current_slot > 0 {
|
||||
state.current_slot -= 1;
|
||||
changed = true;
|
||||
}
|
||||
if controls.backpack_right && state.current_slot < state.count.saturating_sub(1) {
|
||||
state.current_slot += 1;
|
||||
changed = true;
|
||||
}
|
||||
if controls.backpack_swap {
|
||||
commands.trigger(BackbackSwapEvent(state.current_slot));
|
||||
}
|
||||
|
||||
if changed {
|
||||
commands.server_trigger(ToClients {
|
||||
mode: SendMode::Broadcast,
|
||||
message: PlaySound::Selection,
|
||||
@@ -70,6 +65,33 @@ fn swap_head_inputs(
|
||||
sync(&backpack, &mut state, time.elapsed_secs());
|
||||
}
|
||||
}
|
||||
|
||||
for press in backpack_rights.read() {
|
||||
let player = clients.get_controller(press.client_id);
|
||||
let backpack = backpacks.get(player).unwrap();
|
||||
|
||||
if !state.open {
|
||||
return;
|
||||
}
|
||||
|
||||
if state.current_slot < state.count.saturating_sub(1) {
|
||||
state.current_slot += 1;
|
||||
|
||||
commands.server_trigger(ToClients {
|
||||
mode: SendMode::Broadcast,
|
||||
message: PlaySound::Selection,
|
||||
});
|
||||
sync(&backpack, &mut state, time.elapsed_secs());
|
||||
}
|
||||
}
|
||||
|
||||
for _ in backpack_swaps.read() {
|
||||
if !state.open {
|
||||
return;
|
||||
}
|
||||
|
||||
commands.trigger(BackbackSwapEvent(state.current_slot));
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_on_change(
|
||||
|
||||
Reference in New Issue
Block a user