From 6471203be63bb5dd7716aa9841014f7e9105d452 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Mon, 8 Apr 2024 12:56:26 +0200 Subject: fix: send inputs via messages instead of input It appears as if lightyear inputs are supposed to be keys that are being held down. This abstraction does not fit well with our inputs, which are one-time actions. In particular, I've found inputs being missed. We hope to improve upon this, by sending inputs as lightyear messages via a reliable, oredered channel. --- src/client.rs | 78 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 42 deletions(-) (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs index 007edc6..d111732 100644 --- a/src/client.rs +++ b/src/client.rs @@ -21,8 +21,6 @@ use bevy::prelude::*; use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle}; use lightyear::client::connection::ConnectionManager; use lightyear::client::events::*; -use lightyear::client::input::InputManager; -use lightyear::client::input::InputSystemSet; use lightyear::prelude::client::ClientConnection; use lightyear::prelude::client::NetClient; use lightyear::prelude::*; @@ -128,12 +126,7 @@ impl Plugin for ClientPlugin { move_camera.after(control_camera).after(lock_camera), ), ) - .add_systems( - FixedPreUpdate, - buffer_input - .in_set(InputSystemSet::BufferInputs) - .before(choose_attack), - ) + .add_systems(PreUpdate, player_input.before(choose_attack)) .add_systems(Last, (gizmos_hover_indicator, gizmos_attack_indicator)); } } @@ -224,10 +217,10 @@ fn setup( }); client.connect().unwrap(); connection_manager - .send_message::(SelectChampion(champion.0)) + .send_message::(SelectChampion(champion.0)) .unwrap(); connection_manager - .send_message::(SelectFaction(faction.0)) + .send_message::(SelectFaction(faction.0)) .unwrap(); } @@ -316,17 +309,16 @@ fn move_players(mut players: Query<(&mut Transform, &PlayerPosition), Changed, mut attack: ResMut, cameras: Query<(&Camera, &GlobalTransform)>, client_id: Res, keyboard_input: Res>, mouse_input: Res>, - mut input_manager: ResMut>, + mut connection_manager: ResMut>, windows: Query<&Window>, champion: Res, - tick_manager: Res, ) { if mouse_input.just_pressed(MouseButton::Left) { match attack.0 { @@ -340,10 +332,12 @@ fn buffer_input( return; }; let direction = world_position - position.0; - input_manager.add_input( - Inputs::Imperative(Imperative::AttackDirection(ability_slot, direction)), - tick_manager.tick(), - ); + connection_manager + .send_message::(Imperative::AttackDirection( + ability_slot, + direction, + )) + .unwrap(); attack.0 = None; } Ability::Targeted(_) => { @@ -356,10 +350,12 @@ fn buffer_input( ) else { return; }; - input_manager.add_input( - Inputs::Imperative(Imperative::AttackTarget(ability_slot, target_player)), - tick_manager.tick(), - ); + connection_manager + .send_message::(Imperative::AttackTarget( + ability_slot, + target_player, + )) + .unwrap(); attack.0 = None; } }, @@ -371,16 +367,17 @@ fn buffer_input( &players, &windows, ) { - input_manager.add_input( - Inputs::Imperative(Imperative::AttackTarget(AbilitySlot::A, target_player)), - tick_manager.tick(), - ); + connection_manager + .send_message::(Imperative::AttackTarget( + AbilitySlot::A, + target_player, + )) + .unwrap(); } else { if let Some(world_position) = cursor_world_position(&windows, &cameras) { - input_manager.add_input( - Inputs::Imperative(Imperative::WalkTo(world_position)), - tick_manager.tick(), - ); + connection_manager + .send_message::(Imperative::WalkTo(world_position)) + .unwrap(); } } } @@ -389,18 +386,17 @@ fn buffer_input( match attack.0 { None => { if let Some(world_position) = cursor_world_position(&windows, &cameras) { - input_manager.add_input( - Inputs::Imperative(Imperative::WalkTo(world_position)), - tick_manager.tick(), - ); + connection_manager + .send_message::(Imperative::WalkTo(world_position)) + .unwrap(); } } Some(_) => {} } } else if keyboard_input.just_pressed(KeyCode::KeyS) { - input_manager.add_input(Inputs::Imperative(Imperative::Idle), tick_manager.tick()); - } else { - input_manager.add_input(Inputs::None, tick_manager.tick()); + connection_manager + .send_message::(Imperative::Idle) + .unwrap(); } } @@ -409,8 +405,7 @@ fn choose_attack( keyboard_input: Res>, mouse_input: Res>, mut attack: ResMut, - mut input_manager: ResMut>, - tick_manager: Res, + mut connection_manager: ResMut>, ) { if keyboard_input.just_pressed(KeyCode::KeyA) { attack.0 = Some(AbilitySlot::A); @@ -436,10 +431,9 @@ fn choose_attack( match attack.0 { Some(ability_slot) => match champion.0.ability(ability_slot) { Ability::Activated(_) => { - input_manager.add_input( - Inputs::Activation(Activation::Activate(ability_slot)), - tick_manager.tick(), - ); + connection_manager + .send_message::(Activation::Activate(ability_slot)) + .unwrap(); attack.0 = None; } _ => {} -- cgit v1.2.3