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::{
player::MainPlayer,
world::{raycast::LookingAtBlock, ChunkStorage, block::Block},
input::Inputs
input::{Inputs, PrevInputs}
};
pub fn block_placement_system(
main_player: View<MainPlayer>,
raycast: View<LookingAtBlock>,
input: UniqueView<Inputs>,
prev_input: UniqueView<PrevInputs>,
mut world: UniqueViewMut<ChunkStorage>
) {
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;

View file

@ -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<VirtualKeyCode, BuildNoHashHasher<u32>>,
@ -51,7 +54,9 @@ pub fn process_events(
pub fn update_input_states (
raw_inputs: UniqueView<RawInputState>,
mut inputs: UniqueViewMut<Inputs>,
mut prev_inputs: UniqueViewMut<PrevInputs>,
) {
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());
}