prev inputs

This commit is contained in:
griffi-gh 2023-01-28 22:37:01 +01:00
parent 1fef7e00cd
commit 665b3b841e
2 changed files with 12 additions and 7 deletions

View file

@ -3,24 +3,23 @@ use shipyard::{UniqueViewMut, UniqueView, View, IntoIter};
use crate::{ use crate::{
player::MainPlayer, player::MainPlayer,
world::{raycast::LookingAtBlock, ChunkStorage, block::Block}, world::{raycast::LookingAtBlock, ChunkStorage, block::Block},
input::Inputs input::{Inputs, PrevInputs}
}; };
pub fn block_placement_system( pub fn block_placement_system(
main_player: View<MainPlayer>, main_player: View<MainPlayer>,
raycast: View<LookingAtBlock>, raycast: View<LookingAtBlock>,
input: UniqueView<Inputs>, input: UniqueView<Inputs>,
prev_input: UniqueView<PrevInputs>,
mut world: UniqueViewMut<ChunkStorage> mut world: UniqueViewMut<ChunkStorage>
) { ) {
if input.action_a && input.action_b { let action_place = input.action_b && !prev_input.0.action_b;
return let action_break = input.action_a && !prev_input.0.action_a;
} if action_place ^ action_break {
if input.action_a || input.action_b {
//get raycast info //get raycast info
let Some(ray) = (&main_player, &raycast).iter().next().unwrap().1/**/.0 else { return }; let Some(ray) = (&main_player, &raycast).iter().next().unwrap().1/**/.0 else { return };
//update block //update block
let is_place = input.action_b; let place_position = if action_place {
let place_position = if is_place {
let position = (ray.position - ray.direction * 0.5).floor().as_ivec3(); let position = (ray.position - ray.direction * 0.5).floor().as_ivec3();
let Some(block) = world.get_block_mut(position) else { return }; let Some(block) = world.get_block_mut(position) else { return };
*block = Block::Dirt; *block = Block::Dirt;

View file

@ -13,6 +13,9 @@ pub struct Inputs {
pub action_b: bool, pub action_b: bool,
} }
#[derive(Unique, Clone, Copy, Default, Debug)]
pub struct PrevInputs(pub Inputs);
#[derive(Unique, Clone, Default, Debug)] #[derive(Unique, Clone, Default, Debug)]
pub struct RawInputState { pub struct RawInputState {
pub keyboard_state: HashSet<VirtualKeyCode, BuildNoHashHasher<u32>>, pub keyboard_state: HashSet<VirtualKeyCode, BuildNoHashHasher<u32>>,
@ -51,7 +54,9 @@ pub fn process_events(
pub fn update_input_states ( pub fn update_input_states (
raw_inputs: UniqueView<RawInputState>, raw_inputs: UniqueView<RawInputState>,
mut inputs: UniqueViewMut<Inputs>, mut inputs: UniqueViewMut<Inputs>,
mut prev_inputs: UniqueViewMut<PrevInputs>,
) { ) {
prev_inputs.0 = *inputs;
inputs.movement = Vec2::new( inputs.movement = Vec2::new(
raw_inputs.keyboard_state.contains(&VirtualKeyCode::D) as u32 as f32 - raw_inputs.keyboard_state.contains(&VirtualKeyCode::D) as u32 as f32 -
raw_inputs.keyboard_state.contains(&VirtualKeyCode::A) 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: AllStoragesView
) { ) {
storages.add_unique(Inputs::default()); storages.add_unique(Inputs::default());
storages.add_unique(PrevInputs::default());
storages.add_unique(RawInputState::default()); storages.add_unique(RawInputState::default());
} }