Release input on focus loss
parent
74e8fed1b6
commit
ed03e8a523
|
@ -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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue