Release input on focus loss

master
Mark 2023-12-25 18:36:05 -08:00
parent 74e8fed1b6
commit ed03e8a523
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
3 changed files with 23 additions and 1 deletions

View File

@ -11,6 +11,8 @@ pub struct Game {
pub player: Ship, pub player: Ship,
pub system: System, pub system: System,
pub camera: Camera, pub camera: Camera,
paused: bool,
pub time_scale: f32,
} }
impl Game { impl Game {
@ -24,6 +26,8 @@ impl Game {
zoom: 500.0, zoom: 500.0,
}, },
system: System::new(&ct.systems[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) 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) { 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 { if self.input.key_thrust {
self.player.physicsbody.thrust(50.0 * t); self.player.physicsbody.thrust(50.0 * t);

View File

@ -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) { pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
let down = state == &ElementState::Pressed; let down = state == &ElementState::Pressed;
match key { match key {

View File

@ -54,6 +54,9 @@ async fn run(mut game: game::Game) -> Result<()> {
window_id, window_id,
} if window_id == gpu.window.id() => { } if window_id == gpu.window.id() => {
match event { match event {
WindowEvent::Focused(state) => {
game.set_paused(!state);
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: input: