From 8db7dffa7c5eeef20b7e006007ccab5d26231265 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Mon, 10 Apr 2023 19:08:50 +0200 Subject: [PATCH] Pause on escape --- Cargo.toml | 6 ++++++ src/gfx.rs | 56 ++++++++++++++++++++++++++++++------------------------ 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b38982..309b64d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,12 @@ name = "mt_client" version = "0.1.0" edition = "2021" +[profile.dev] +opt-level = 1 + +[profile.dev.package."*"] +opt-level = 3 + [dependencies] bytemuck = { version = "1.13.0", features = ["derive"] } cgmath = "0.17.0" diff --git a/src/gfx.rs b/src/gfx.rs index e3fb073..c86b351 100644 --- a/src/gfx.rs +++ b/src/gfx.rs @@ -22,11 +22,6 @@ pub async fn run( .build(&event_loop) .unwrap(); - window - .set_cursor_grab(CursorGrabMode::Locked) - .or_else(|_e| window.set_cursor_grab(CursorGrabMode::Confined)) - .unwrap(); - window.set_cursor_visible(false); let mut state = state::State::new(&window).await; @@ -37,6 +32,8 @@ pub async fn run( let mut last_frame = Instant::now(); + let mut game_paused = false; + event_loop.run_return(move |event, _, flow| match event { MainEventsCleared => window.request_redraw(), RedrawRequested(id) if id == window.id() => { @@ -78,21 +75,28 @@ pub async fn run( .. } => { use fps_camera::Actions; - use winit::event::{ElementState::*, VirtualKeyCode as Key}; + use winit::event::{ElementState, VirtualKeyCode as Key}; + + if key == &Key::Escape && key_state == &ElementState::Pressed { + game_paused = !game_paused; + window.set_cursor_visible(game_paused); + } - let actions = match key { - Key::W => Actions::MOVE_FORWARD, - Key::A => Actions::STRAFE_LEFT, - Key::S => Actions::MOVE_BACKWARD, - Key::D => Actions::STRAFE_RIGHT, - Key::Space => Actions::FLY_UP, - Key::LShift => Actions::FLY_DOWN, - _ => Actions::empty(), - }; + if !game_paused { + let actions = match key { + Key::W => Actions::MOVE_FORWARD, + Key::A => Actions::STRAFE_LEFT, + Key::S => Actions::MOVE_BACKWARD, + Key::D => Actions::STRAFE_RIGHT, + Key::Space => Actions::FLY_UP, + Key::LShift => Actions::FLY_DOWN, + _ => Actions::empty(), + }; - match key_state { - Pressed => state.camera.enable_actions(actions), - Released => state.camera.disable_action(actions), + match key_state { + ElementState::Pressed => state.camera.enable_actions(actions), + ElementState::Released => state.camera.disable_action(actions), + } } } _ => {} @@ -101,13 +105,15 @@ pub async fn run( event: MouseMotion { delta }, .. } => { - state.camera.update_mouse(delta.0 as f32, delta.1 as f32); - window - .set_cursor_position(winit::dpi::PhysicalPosition::new( - state.config.width / 2, - state.config.height / 2, - )) - .ok(); + if !game_paused { + state.camera.update_mouse(delta.0 as f32, delta.1 as f32); + window + .set_cursor_position(winit::dpi::PhysicalPosition::new( + state.config.width / 2, + state.config.height / 2, + )) + .ok(); + } } UserEvent(event) => match event { Close => *flow = ExitWithCode(0), -- 2.44.0