diff --git a/src/app.rs b/src/app.rs index 7b05d0e..9ce233c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,12 +4,13 @@ use egui_sfml::{egui, SfEgui}; use gamedebug_core::imm_dbg; use sfml::{ graphics::{Color, RenderTarget, RenderWindow}, - window::Event, + window::{Event, Key}, }; use crate::{ game::GameState, graphics, + input::KbInput, math::{wp_to_tp, WorldPos}, res::Res, }; @@ -21,6 +22,7 @@ pub struct App { pub game: GameState, pub res: Res, pub sf_egui: SfEgui, + pub kb_input: KbInput, } impl App { @@ -33,6 +35,7 @@ impl App { game: GameState::default(), res: Res::load()?, sf_egui, + kb_input: KbInput::default(), }) } @@ -41,6 +44,7 @@ impl App { self.do_event_handling(); self.do_update(); self.do_rendering(); + self.kb_input.clear_pressed(); gamedebug_core::inc_frame(); } } @@ -48,6 +52,7 @@ impl App { fn do_event_handling(&mut self) { while let Some(ev) = self.rw.poll_event() { self.sf_egui.add_event(&ev); + self.kb_input.update_from_event(&ev); match ev { Event::Closed => self.should_quit = true, _ => {} @@ -56,6 +61,25 @@ impl App { } fn do_update(&mut self) { + let spd = if self.kb_input.down(Key::LShift) { + 100 + } else if self.kb_input.down(Key::LControl) { + 1000 + } else { + 2 + }; + if self.kb_input.down(Key::Left) { + self.game.camera_offset.x -= spd; + } + if self.kb_input.down(Key::Right) { + self.game.camera_offset.x += spd; + } + if self.kb_input.down(Key::Up) { + self.game.camera_offset.y -= spd; + } + if self.kb_input.down(Key::Down) { + self.game.camera_offset.y += spd; + } let tp = self.game.camera_offset.tile_pos(); imm_dbg!(tp); imm_dbg!(tp.to_chunk_and_local()); diff --git a/src/input.rs b/src/input.rs new file mode 100644 index 0000000..b3dd1de --- /dev/null +++ b/src/input.rs @@ -0,0 +1,33 @@ +use fnv::FnvHashSet; +use sfml::window::{Event, Key}; + +#[derive(Default)] +pub struct KbInput { + down: FnvHashSet, + pressed: FnvHashSet, +} + +impl KbInput { + pub fn update_from_event(&mut self, ev: &Event) { + match ev { + Event::KeyPressed { code, .. } => { + self.pressed.insert(*code); + self.down.insert(*code); + } + Event::KeyReleased { code, .. } => { + self.down.remove(code); + } + _ => {} + } + } + /// Pressed event should be cleared every frame + pub fn clear_pressed(&mut self) { + self.pressed.clear(); + } + pub fn down(&self, key: Key) -> bool { + self.down.contains(&key) + } + pub fn pressed(&self, key: Key) -> bool { + self.pressed.contains(&key) + } +} diff --git a/src/main.rs b/src/main.rs index 1aea6c5..4c41222 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod app; mod game; mod graphics; +mod input; mod math; mod res; mod world;