From f34894087aee38d5d488c0e0ff2cb888f67fccc2 Mon Sep 17 00:00:00 2001 From: Shane Handley Date: Sat, 1 Aug 2020 20:21:16 +1000 Subject: [PATCH 1/2] Prevent inventory collect events from firing on keyup, which is causing item pickup actions to be called twice. --- voxygen/src/session.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index e531fa04a2..67a859e6fa 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -478,7 +478,7 @@ impl PlayState for SessionState { } } }, - Event::InputUpdate(GameInput::Interact, state) => { + Event::InputUpdate(GameInput::Interact, true) => { let mut client = self.client.borrow_mut(); // Collect terrain sprites @@ -493,7 +493,7 @@ impl PlayState for SessionState { .get(client.entity()) .copied(); - if let (Some(player_pos), true) = (player_pos, state) { + if let Some(player_pos) = player_pos { let entity = ( &client.state().ecs().entities(), &client.state().ecs().read_storage::(), From 1ae02244bc30805a13190d6c389ed9a02c8ee477 Mon Sep 17 00:00:00 2001 From: Shane Handley Date: Sun, 2 Aug 2020 15:25:57 +1000 Subject: [PATCH 2/2] Prevent keyholds for collect (e) --- voxygen/src/key_state.rs | 11 ++++--- voxygen/src/session.rs | 70 ++++++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/voxygen/src/key_state.rs b/voxygen/src/key_state.rs index 775168de66..84992bc8d6 100644 --- a/voxygen/src/key_state.rs +++ b/voxygen/src/key_state.rs @@ -14,13 +14,13 @@ pub struct KeyState { pub auto_walk: bool, pub swap_loadout: bool, pub respawn: bool, + pub collect: bool, pub analog_matrix: Vec2, } -impl KeyState { - #[allow(clippy::new_without_default)] // TODO: Pending review in #587 - pub fn new() -> KeyState { - KeyState { +impl Default for KeyState { + fn default() -> Self { + Self { right: false, left: false, up: false, @@ -34,10 +34,13 @@ impl KeyState { auto_walk: false, swap_loadout: false, respawn: false, + collect: false, analog_matrix: Vec2::zero(), } } +} +impl KeyState { pub fn dir_vec(&self) -> Vec2 { let dir = if self.analog_matrix == Vec2::zero() { Vec2::::new( diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 67a859e6fa..2588147a73 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -71,10 +71,11 @@ impl SessionState { let walk_forward_dir = scene.camera().forward_xy(); let walk_right_dir = scene.camera().right_xy(); + Self { scene, client, - key_state: KeyState::new(), + key_state: KeyState::default(), inputs: comp::ControllerInputs::default(), hud, selected_block: Block::new(BlockKind::Normal, Rgb::broadcast(255)), @@ -93,9 +94,7 @@ impl SessionState { self.hud.auto_walk(false); self.key_state.auto_walk = false; } -} -impl SessionState { /// Tick the session (and the client attached to it). fn tick(&mut self, dt: Duration, global_state: &mut GlobalState) -> Result { self.inputs.tick(dt); @@ -357,6 +356,7 @@ impl PlayState for SessionState { if state != self.key_state.toggle_sit => { self.key_state.toggle_sit = state; + if state { self.stop_auto_walk(); self.client.borrow_mut().toggle_sit(); @@ -478,41 +478,47 @@ impl PlayState for SessionState { } } }, - Event::InputUpdate(GameInput::Interact, true) => { - let mut client = self.client.borrow_mut(); + Event::InputUpdate(GameInput::Interact, state) + if state != self.key_state.collect => + { + self.key_state.collect = state; - // Collect terrain sprites - if let Some(select_pos) = self.scene.select_pos() { - client.collect_block(select_pos); - } + if state { + let mut client = self.client.borrow_mut(); - // Collect lootable entities - let player_pos = client - .state() - .read_storage::() - .get(client.entity()) - .copied(); + // Collect terrain sprites + if let Some(select_pos) = self.scene.select_pos() { + client.collect_block(select_pos); + } - if let Some(player_pos) = player_pos { - let entity = ( - &client.state().ecs().entities(), - &client.state().ecs().read_storage::(), - &client.state().ecs().read_storage::(), - ) - .join() - .filter(|(_, pos, _)| { - pos.0.distance_squared(player_pos.0) < MAX_PICKUP_RANGE_SQR - }) - .min_by_key(|(_, pos, _)| { - (pos.0.distance_squared(player_pos.0) * 1000.0) as i32 - }) - .map(|(entity, _, _)| entity); + // Collect lootable entities + let player_pos = client + .state() + .read_storage::() + .get(client.entity()) + .copied(); - if let Some(entity) = entity { - client.pick_up(entity); + if let Some(player_pos) = player_pos { + let entity = ( + &client.state().ecs().entities(), + &client.state().ecs().read_storage::(), + &client.state().ecs().read_storage::(), + ) + .join() + .filter(|(_, pos, _)| { + pos.0.distance_squared(player_pos.0) < MAX_PICKUP_RANGE_SQR + }) + .min_by_key(|(_, pos, _)| { + (pos.0.distance_squared(player_pos.0) * 1000.0) as i32 + }) + .map(|(entity, _, _)| entity); + + if let Some(entity) = entity { + client.pick_up(entity); + } } } - }, + } /*Event::InputUpdate(GameInput::Charge, state) => { self.inputs.charge.set_state(state); },*/