Merge branch 'shandley/collect-block-keyup-fix' into 'master'

Prevent inventory collect events from firing on keyup

See merge request veloren/veloren!1252
This commit is contained in:
Imbris 2020-08-02 06:49:14 +00:00
commit aced5f9797
2 changed files with 45 additions and 36 deletions

View File

@ -14,13 +14,13 @@ pub struct KeyState {
pub auto_walk: bool, pub auto_walk: bool,
pub swap_loadout: bool, pub swap_loadout: bool,
pub respawn: bool, pub respawn: bool,
pub collect: bool,
pub analog_matrix: Vec2<f32>, pub analog_matrix: Vec2<f32>,
} }
impl KeyState { impl Default for KeyState {
#[allow(clippy::new_without_default)] // TODO: Pending review in #587 fn default() -> Self {
pub fn new() -> KeyState { Self {
KeyState {
right: false, right: false,
left: false, left: false,
up: false, up: false,
@ -34,10 +34,13 @@ impl KeyState {
auto_walk: false, auto_walk: false,
swap_loadout: false, swap_loadout: false,
respawn: false, respawn: false,
collect: false,
analog_matrix: Vec2::zero(), analog_matrix: Vec2::zero(),
} }
} }
}
impl KeyState {
pub fn dir_vec(&self) -> Vec2<f32> { pub fn dir_vec(&self) -> Vec2<f32> {
let dir = if self.analog_matrix == Vec2::zero() { let dir = if self.analog_matrix == Vec2::zero() {
Vec2::<f32>::new( Vec2::<f32>::new(

View File

@ -71,10 +71,11 @@ impl SessionState {
let walk_forward_dir = scene.camera().forward_xy(); let walk_forward_dir = scene.camera().forward_xy();
let walk_right_dir = scene.camera().right_xy(); let walk_right_dir = scene.camera().right_xy();
Self { Self {
scene, scene,
client, client,
key_state: KeyState::new(), key_state: KeyState::default(),
inputs: comp::ControllerInputs::default(), inputs: comp::ControllerInputs::default(),
hud, hud,
selected_block: Block::new(BlockKind::Normal, Rgb::broadcast(255)), selected_block: Block::new(BlockKind::Normal, Rgb::broadcast(255)),
@ -93,9 +94,7 @@ impl SessionState {
self.hud.auto_walk(false); self.hud.auto_walk(false);
self.key_state.auto_walk = false; self.key_state.auto_walk = false;
} }
}
impl SessionState {
/// Tick the session (and the client attached to it). /// Tick the session (and the client attached to it).
fn tick(&mut self, dt: Duration, global_state: &mut GlobalState) -> Result<TickAction, Error> { fn tick(&mut self, dt: Duration, global_state: &mut GlobalState) -> Result<TickAction, Error> {
self.inputs.tick(dt); self.inputs.tick(dt);
@ -357,6 +356,7 @@ impl PlayState for SessionState {
if state != self.key_state.toggle_sit => if state != self.key_state.toggle_sit =>
{ {
self.key_state.toggle_sit = state; self.key_state.toggle_sit = state;
if state { if state {
self.stop_auto_walk(); self.stop_auto_walk();
self.client.borrow_mut().toggle_sit(); self.client.borrow_mut().toggle_sit();
@ -478,41 +478,47 @@ impl PlayState for SessionState {
} }
} }
}, },
Event::InputUpdate(GameInput::Interact, state) => { Event::InputUpdate(GameInput::Interact, state)
let mut client = self.client.borrow_mut(); if state != self.key_state.collect =>
{
self.key_state.collect = state;
// Collect terrain sprites if state {
if let Some(select_pos) = self.scene.select_pos() { let mut client = self.client.borrow_mut();
client.collect_block(select_pos);
}
// Collect lootable entities // Collect terrain sprites
let player_pos = client if let Some(select_pos) = self.scene.select_pos() {
.state() client.collect_block(select_pos);
.read_storage::<comp::Pos>() }
.get(client.entity())
.copied();
if let (Some(player_pos), true) = (player_pos, state) { // Collect lootable entities
let entity = ( let player_pos = client
&client.state().ecs().entities(), .state()
&client.state().ecs().read_storage::<comp::Pos>(), .read_storage::<comp::Pos>()
&client.state().ecs().read_storage::<comp::Item>(), .get(client.entity())
) .copied();
.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 { if let Some(player_pos) = player_pos {
client.pick_up(entity); let entity = (
&client.state().ecs().entities(),
&client.state().ecs().read_storage::<comp::Pos>(),
&client.state().ecs().read_storage::<comp::Item>(),
)
.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) => { /*Event::InputUpdate(GameInput::Charge, state) => {
self.inputs.charge.set_state(state); self.inputs.charge.set_state(state);
},*/ },*/