From 567e6d8d8541a89b3b13d5beafc3f510f9221e86 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 24 Jan 2023 18:58:56 +0100 Subject: [PATCH] wip input system --- src/input.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 ++ 2 files changed, 50 insertions(+) diff --git a/src/input.rs b/src/input.rs index e69de29..a363882 100644 --- a/src/input.rs +++ b/src/input.rs @@ -0,0 +1,48 @@ +use glium::glutin::event::VirtualKeyCode; +use shipyard::{AllStoragesView, Unique}; +use hashbrown::HashMap; +use nohash_hasher::BuildNoHashHasher; + +#[repr(u8)] +#[derive(Clone, Copy, Debug)] +pub enum Action { + Move(f32, f32), + Look(f32, f32), +} + +#[derive(Unique)] +pub struct InputSystem { + pub keyboard_keymap: HashMap>, + mouse_map: Option A>, + keyboard_state: HashMap>, + mouse_delta: (f32, f32), + mouse_position: (f32, f32), +} +impl InputSystem { + pub fn new() -> Self { + Self { + keyboard_keymap: HashMap::with_hasher(BuildNoHashHasher::default()), + mouse_map: None, + keyboard_state: HashMap::with_hasher(BuildNoHashHasher::default()), + mouse_delta: (0., 0.), + mouse_position: (0., 0.), + } + } + pub fn map_to_mouse(&mut self, function: fn(f32, f32) -> A) { + self.mouse_map = Some(function); + } +} + +pub fn setup_input_system( + storages: AllStoragesView +) { + let mut input = InputSystem::new(); + input.keyboard_keymap.extend([ + (VirtualKeyCode::W, Action::Move(0., 1.)), + (VirtualKeyCode::A, Action::Move(-1., 0.)), + (VirtualKeyCode::S, Action::Move(0., -1.)), + (VirtualKeyCode::D, Action::Move(1., 0.)) + ]); + input.map_to_mouse(Action::Look); + storages.add_unique(input); +} diff --git a/src/main.rs b/src/main.rs index 0ae9e00..cf209f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,8 @@ pub(crate) mod prefabs; pub(crate) mod transform; pub(crate) mod settings; pub(crate) mod camera; +pub(crate) mod input; +pub(crate) mod fly_controller; use rendering::{Renderer, RenderTarget, BackgroundColor, clear_background}; use world::{loading::update_loaded_world_around_player, render::draw_world, init_world};