Switch to replicon (#80)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use bevy::prelude::*;
|
||||
use lightyear::prelude::input::native::ActionState;
|
||||
use bevy_replicon::prelude::{ClientState, FromClient, SendMode, ServerTriggerExt, ToClients};
|
||||
use shared::{
|
||||
GameState,
|
||||
backpack::{
|
||||
@@ -7,7 +7,7 @@ use shared::{
|
||||
backpack_ui::{BackpackUiState, HEAD_SLOTS},
|
||||
},
|
||||
control::ControlState,
|
||||
protocol::PlaySound,
|
||||
protocol::{ClientToController, PlaySound},
|
||||
};
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
@@ -15,23 +15,34 @@ pub fn plugin(app: &mut App) {
|
||||
FixedUpdate,
|
||||
sync_on_change.run_if(in_state(GameState::Playing)),
|
||||
);
|
||||
app.add_systems(FixedUpdate, swap_head_inputs);
|
||||
app.add_systems(
|
||||
FixedUpdate,
|
||||
swap_head_inputs.run_if(in_state(ClientState::Disconnected)),
|
||||
);
|
||||
}
|
||||
|
||||
fn swap_head_inputs(
|
||||
player: Query<(&ActionState<ControlState>, Ref<Backpack>)>,
|
||||
backpacks: Query<Ref<Backpack>>,
|
||||
clients: ClientToController,
|
||||
mut inputs: MessageReader<FromClient<ControlState>>,
|
||||
mut commands: Commands,
|
||||
mut state: Single<&mut BackpackUiState>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (controls, backpack) in player.iter() {
|
||||
for controls in inputs.read() {
|
||||
let player = clients.get_controller(controls.client_id);
|
||||
let backpack = backpacks.get(player).unwrap();
|
||||
|
||||
if state.count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
if controls.backpack_toggle {
|
||||
state.open = !state.open;
|
||||
commands.trigger(PlaySound::Backpack { open: state.open });
|
||||
commands.server_trigger(ToClients {
|
||||
mode: SendMode::Broadcast,
|
||||
message: PlaySound::Backpack { open: state.open },
|
||||
});
|
||||
}
|
||||
|
||||
if !state.open {
|
||||
@@ -52,7 +63,10 @@ fn swap_head_inputs(
|
||||
}
|
||||
|
||||
if changed {
|
||||
commands.trigger(PlaySound::Selection);
|
||||
commands.server_trigger(ToClients {
|
||||
mode: SendMode::Broadcast,
|
||||
message: PlaySound::Selection,
|
||||
});
|
||||
sync(&backpack, &mut state, time.elapsed_secs());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user