diff --git a/src/block_placement.rs b/src/block_placement.rs index 56d5f92..89cdf99 100644 --- a/src/block_placement.rs +++ b/src/block_placement.rs @@ -3,24 +3,23 @@ use shipyard::{UniqueViewMut, UniqueView, View, IntoIter}; use crate::{ player::MainPlayer, world::{raycast::LookingAtBlock, ChunkStorage, block::Block}, - input::Inputs + input::{Inputs, PrevInputs} }; pub fn block_placement_system( main_player: View, raycast: View, input: UniqueView, + prev_input: UniqueView, mut world: UniqueViewMut ) { - if input.action_a && input.action_b { - return - } - if input.action_a || input.action_b { + let action_place = input.action_b && !prev_input.0.action_b; + let action_break = input.action_a && !prev_input.0.action_a; + if action_place ^ action_break { //get raycast info let Some(ray) = (&main_player, &raycast).iter().next().unwrap().1/**/.0 else { return }; //update block - let is_place = input.action_b; - let place_position = if is_place { + let place_position = if action_place { let position = (ray.position - ray.direction * 0.5).floor().as_ivec3(); let Some(block) = world.get_block_mut(position) else { return }; *block = Block::Dirt; diff --git a/src/input.rs b/src/input.rs index 522b569..7f408e9 100644 --- a/src/input.rs +++ b/src/input.rs @@ -13,6 +13,9 @@ pub struct Inputs { pub action_b: bool, } +#[derive(Unique, Clone, Copy, Default, Debug)] +pub struct PrevInputs(pub Inputs); + #[derive(Unique, Clone, Default, Debug)] pub struct RawInputState { pub keyboard_state: HashSet>, @@ -51,7 +54,9 @@ pub fn process_events( pub fn update_input_states ( raw_inputs: UniqueView, mut inputs: UniqueViewMut, + mut prev_inputs: UniqueViewMut, ) { + prev_inputs.0 = *inputs; inputs.movement = Vec2::new( raw_inputs.keyboard_state.contains(&VirtualKeyCode::D) as u32 as f32 - raw_inputs.keyboard_state.contains(&VirtualKeyCode::A) as u32 as f32, @@ -67,6 +72,7 @@ pub fn init_input ( storages: AllStoragesView ) { storages.add_unique(Inputs::default()); + storages.add_unique(PrevInputs::default()); storages.add_unique(RawInputState::default()); }