From ed03e8a523f0624bce129a414f269f6bfb761b5d Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 25 Dec 2023 18:36:05 -0800 Subject: [PATCH] Release input on focus loss --- src/game/game.rs | 15 ++++++++++++++- src/game/inputstatus.rs | 6 ++++++ src/main.rs | 3 +++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/game/game.rs b/src/game/game.rs index 4c1e216..6320a98 100644 --- a/src/game/game.rs +++ b/src/game/game.rs @@ -11,6 +11,8 @@ pub struct Game { pub player: Ship, pub system: System, pub camera: Camera, + paused: bool, + pub time_scale: f32, } impl Game { @@ -24,6 +26,8 @@ impl Game { zoom: 500.0, }, system: System::new(&ct.systems[0]), + paused: false, + time_scale: 1.0, } } @@ -39,8 +43,17 @@ impl Game { self.input.process_scroll(delta, phase) } + pub fn set_paused(&mut self, pause: bool) { + if pause { + self.paused = true; + self.input.release_all() + } else { + self.paused = false; + } + } + pub fn update(&mut self) { - let t: f32 = self.last_update.elapsed().as_secs_f32(); + let t: f32 = self.last_update.elapsed().as_secs_f32() * self.time_scale; if self.input.key_thrust { self.player.physicsbody.thrust(50.0 * t); diff --git a/src/game/inputstatus.rs b/src/game/inputstatus.rs index 25a7249..a496dbd 100644 --- a/src/game/inputstatus.rs +++ b/src/game/inputstatus.rs @@ -19,6 +19,12 @@ impl InputStatus { } } + pub fn release_all(&mut self) { + self.key_left = false; + self.key_right = false; + self.key_thrust = false; + } + pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) { let down = state == &ElementState::Pressed; match key { diff --git a/src/main.rs b/src/main.rs index 57d4959..94b7129 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,9 @@ async fn run(mut game: game::Game) -> Result<()> { window_id, } if window_id == gpu.window.id() => { match event { + WindowEvent::Focused(state) => { + game.set_paused(!state); + } WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::KeyboardInput { input: